added gameover packet
This commit is contained in:
parent
aabda1cc90
commit
84ed6c9c32
90
game.go
90
game.go
@ -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"`
|
||||
@ -73,6 +49,7 @@ type game struct {
|
||||
register chan *player
|
||||
unregister chan *player
|
||||
turn int
|
||||
players_remaining int
|
||||
width, height float32
|
||||
spectators map[*Spectator]bool
|
||||
sregister chan *Spectator
|
||||
@ -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,25 +260,11 @@ func (g *game) run() {
|
||||
}
|
||||
|
||||
// UPDATE GAME STATE
|
||||
players_remaining := g.tick(payload)
|
||||
if end, data := g.gameOver(); end {
|
||||
g.sendGameOver(data)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
g.tick(payload)
|
||||
|
||||
t1 = time.Now()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
40
protocol.go
40
protocol.go
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user