added gameover packet

This commit is contained in:
Stephen McQuay 2013-11-13 22:24:54 -08:00
parent aabda1cc90
commit 84ed6c9c32
4 changed files with 97 additions and 73 deletions

122
game.go
View File

@ -14,30 +14,6 @@ type BotHealth struct {
Health int `json:"health"`
}
type Boardstate struct {
MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
Obstacles []Obstacle `json:"obj"`
Reset bool `json:"reset"`
Type string `json:"type"`
Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"`
Messages []string `json:"messages"`
}
func NewBoardstate() *Boardstate {
return &Boardstate{
MyRobots: []Robot{},
OtherRobots: []OtherRobot{},
Projectiles: []Projectile{},
Splosions: []Splosion{},
AllBots: []BotHealth{},
Type: "boardstate",
}
}
type Scanner struct {
Id string `json:"id"`
Type string `json:"type"`
@ -65,22 +41,23 @@ func (ml *MapLock) add(g *game) {
}
type game struct {
id string
players map[*player]bool
projectiles map[*Projectile]bool
splosions map[*Splosion]bool
obstacles []Obstacle
register chan *player
unregister chan *player
turn int
width, height float32
spectators map[*Spectator]bool
sregister chan *Spectator
sunregister chan *Spectator
kill chan bool
repair_hp int
repair_rate float32
tick_duration int
id string
players map[*player]bool
projectiles map[*Projectile]bool
splosions map[*Splosion]bool
obstacles []Obstacle
register chan *player
unregister chan *player
turn int
players_remaining int
width, height float32
spectators map[*Spectator]bool
sregister chan *Spectator
sunregister chan *Spectator
kill chan bool
repair_hp int
repair_rate float32
tick_duration int
}
func NewGame(id string, width, height float32, tick int) *game {
@ -106,10 +83,8 @@ func NewGame(id string, width, height float32, tick int) *game {
return g
}
func (g *game) tick(payload *Boardstate) int {
robots_remaining := 0
players_remaining := 0
func (g *game) tick(payload *Boardstate) {
g.players_remaining = 0
payload.Obstacles = g.obstacles
// Update Players
@ -139,8 +114,7 @@ func (g *game) tick(payload *Boardstate) int {
}
if living_robots > 0 {
players_remaining++
robots_remaining += living_robots
g.players_remaining++
}
}
@ -162,8 +136,6 @@ func (g *game) tick(payload *Boardstate) int {
}
payload.Splosions = append(payload.Splosions, *s)
}
return players_remaining
}
func (g *game) sendUpdate(payload *Boardstate) {
@ -180,7 +152,6 @@ func (g *game) sendUpdate(payload *Boardstate) {
player_payload.Obstacles = payload.Obstacles
player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset
for _, r := range p.Robots {
player_payload.MyRobots = append(player_payload.MyRobots, *r)
player_payload.OtherRobots = append(
@ -289,26 +260,12 @@ func (g *game) run() {
}
// UPDATE GAME STATE
players_remaining := g.tick(payload)
// Determine end game?
if players_remaining <= 1 && len(g.players) > 1 {
g.obstacles = GenerateObstacles(5, g.width, g.height)
log.Printf("game %s: game over", g.id)
for p := range g.players {
for _, r := range p.Robots {
if r.Health > 0 {
log.Printf("Robot %v Survived", r.Id)
}
r.reset(g)
}
}
payload.Reset = true
} else {
payload.Reset = false
if end, data := g.gameOver(); end {
g.sendGameOver(data)
}
g.tick(payload)
t1 = time.Now()
if *verbose {
log.Printf("Turn Processes %v\n", t1.Sub(t0))
@ -324,3 +281,34 @@ func (g *game) run() {
}
}
}
func (g *game) gameOver() (bool, *GameOver) {
over := false
stats := NewGameOver()
if g.players_remaining <= 1 && len(g.players) > 1 {
g.obstacles = GenerateObstacles(conf.Obstacles, g.width, g.height)
log.Printf("game %s: game over", g.id)
for p := range g.players {
for _, r := range p.Robots {
if r.Health > 0 {
log.Printf("Robot %v Survived", r.Id)
}
r.reset(g)
}
}
over = true
}
return over, stats
}
func (g *game) sendGameOver(eg *GameOver) {
log.Printf("sending out game over message: %+v", eg)
for p := range g.players {
p.send <- eg
}
for s := range g.spectators {
s.send <- eg
}
}

View File

@ -8,13 +8,13 @@ import (
type player struct {
ws *websocket.Conn
Robots []*Robot
send chan *Boardstate
send chan Message
Instruction Instruction
}
func (p *player) sender() {
for things := range p.send {
err := websocket.JSON.Send(p.ws, *things)
err := websocket.JSON.Send(p.ws, things)
if err != nil {
break
}

View File

@ -101,6 +101,42 @@ func NewHandshake(id string, success bool) *Handshake {
}
}
type Message interface {
}
type Boardstate struct {
MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
Obstacles []Obstacle `json:"obj"`
Type string `json:"type"`
Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"`
Messages []string `json:"messages"`
}
func NewBoardstate() *Boardstate {
return &Boardstate{
MyRobots: []Robot{},
OtherRobots: []OtherRobot{},
Projectiles: []Projectile{},
Splosions: []Splosion{},
AllBots: []BotHealth{},
Type: "boardstate",
}
}
type GameOver struct {
Type string `json:"type"`
}
func NewGameOver() *GameOver {
return &GameOver{
Type: "gameover",
}
}
type Failure struct {
Reason string `json:"reason"`
Type string `json:"type"`
@ -199,7 +235,7 @@ func addPlayer(ws *websocket.Conn) {
p := &player{
Robots: []*Robot{},
send: make(chan *Boardstate),
send: make(chan Message),
ws: ws,
}
@ -226,7 +262,7 @@ func addPlayer(ws *websocket.Conn) {
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robots[0].Id)
case "spectator":
s := &Spectator{
send: make(chan *Boardstate),
send: make(chan Message),
ws: ws,
}
game.sregister <- s

View File

@ -7,12 +7,12 @@ import (
type Spectator struct {
ws *websocket.Conn
send chan *Boardstate
send chan Message
}
func (s *Spectator) sender() {
for things := range s.send {
err := websocket.JSON.Send(s.ws, *things)
err := websocket.JSON.Send(s.ws, things)
if err != nil {
break
}