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

90
game.go
View File

@ -14,30 +14,6 @@ type BotHealth struct {
Health int `json:"health"` 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 { type Scanner struct {
Id string `json:"id"` Id string `json:"id"`
Type string `json:"type"` Type string `json:"type"`
@ -73,6 +49,7 @@ type game struct {
register chan *player register chan *player
unregister chan *player unregister chan *player
turn int turn int
players_remaining int
width, height float32 width, height float32
spectators map[*Spectator]bool spectators map[*Spectator]bool
sregister chan *Spectator sregister chan *Spectator
@ -106,10 +83,8 @@ func NewGame(id string, width, height float32, tick int) *game {
return g return g
} }
func (g *game) tick(payload *Boardstate) int { func (g *game) tick(payload *Boardstate) {
robots_remaining := 0 g.players_remaining = 0
players_remaining := 0
payload.Obstacles = g.obstacles payload.Obstacles = g.obstacles
// Update Players // Update Players
@ -139,8 +114,7 @@ func (g *game) tick(payload *Boardstate) int {
} }
if living_robots > 0 { if living_robots > 0 {
players_remaining++ g.players_remaining++
robots_remaining += living_robots
} }
} }
@ -162,8 +136,6 @@ func (g *game) tick(payload *Boardstate) int {
} }
payload.Splosions = append(payload.Splosions, *s) payload.Splosions = append(payload.Splosions, *s)
} }
return players_remaining
} }
func (g *game) sendUpdate(payload *Boardstate) { func (g *game) sendUpdate(payload *Boardstate) {
@ -180,7 +152,6 @@ func (g *game) sendUpdate(payload *Boardstate) {
player_payload.Obstacles = payload.Obstacles player_payload.Obstacles = payload.Obstacles
player_payload.AllBots = payload.AllBots player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset
for _, r := range p.Robots { for _, r := range p.Robots {
player_payload.MyRobots = append(player_payload.MyRobots, *r) player_payload.MyRobots = append(player_payload.MyRobots, *r)
player_payload.OtherRobots = append( player_payload.OtherRobots = append(
@ -289,25 +260,11 @@ func (g *game) run() {
} }
// UPDATE GAME STATE // UPDATE GAME STATE
players_remaining := g.tick(payload) if end, data := g.gameOver(); end {
g.sendGameOver(data)
}
// Determine end game? g.tick(payload)
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
}
t1 = time.Now() t1 = time.Now()
if *verbose { if *verbose {
@ -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 { type player struct {
ws *websocket.Conn ws *websocket.Conn
Robots []*Robot Robots []*Robot
send chan *Boardstate send chan Message
Instruction Instruction Instruction Instruction
} }
func (p *player) sender() { func (p *player) sender() {
for things := range p.send { for things := range p.send {
err := websocket.JSON.Send(p.ws, *things) err := websocket.JSON.Send(p.ws, things)
if err != nil { if err != nil {
break 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 { type Failure struct {
Reason string `json:"reason"` Reason string `json:"reason"`
Type string `json:"type"` Type string `json:"type"`
@ -199,7 +235,7 @@ func addPlayer(ws *websocket.Conn) {
p := &player{ p := &player{
Robots: []*Robot{}, Robots: []*Robot{},
send: make(chan *Boardstate), send: make(chan Message),
ws: ws, 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) log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robots[0].Id)
case "spectator": case "spectator":
s := &Spectator{ s := &Spectator{
send: make(chan *Boardstate), send: make(chan Message),
ws: ws, ws: ws,
} }
game.sregister <- s game.sregister <- s

View File

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