From fb0e6d92d771fc0b2acac48e2d57d1663dcd30b1 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 4 Sep 2013 23:23:05 -0700 Subject: [PATCH] Sorted out extra load turns out we were never exiting the loop even when all bots left. Games now shut down when there ever has been bots and they all leave (or are killed, I think). --- game.go | 13 +++++++++++-- http.go | 7 ++++--- player.go | 2 ++ protocol.go | 18 +++--------------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/game.go b/game.go index 21fe8a2..46587af 100644 --- a/game.go +++ b/game.go @@ -37,7 +37,7 @@ func NewGame(id string, width, height float64) *game { spectators: make(map[*Spectator]bool), sregister: make(chan *Spectator), sunregister: make(chan *Spectator), - kill: make(chan bool), + kill: make(chan bool, 128), } g.robot_id = make(chan int) @@ -52,13 +52,18 @@ func NewGame(id string, width, height float64) *game { } func (g *game) run() { + started := false for { select { case <-g.kill: - log.Printf("%s: received kill signal, dying gracefully", g.id) + log.Printf("game %s: received kill signal, dying gracefully", g.id) + gameLock.Lock() + delete(games, g.id) + gameLock.Unlock() return case p := <-g.register: g.players[p] = true + started = true case p := <-g.unregister: delete(g.players, p) close(p.send) @@ -68,6 +73,10 @@ func (g *game) run() { delete(g.spectators, s) close(s.send) case <-time.Tick(time.Duration(*tick) * time.Millisecond): + if started && len(g.players) == 0 { + g.kill <- true + } + g.turn++ t0 := time.Now() diff --git a/http.go b/http.go index 7cb9984..f12278d 100644 --- a/http.go +++ b/http.go @@ -57,14 +57,15 @@ func addPlayer(ws *websocket.Conn) { if !ok { log.Println("ERROR: game not found") + websocket.JSON.Send(ws, bot.NewFailure("game 404")) return } id := idg.Hash() - conf, err := Negociate(ws, id, 400, 800) + conf, err := Negociate(ws, id, game.width, game.height) if err != nil { - websocket.JSON.Send(ws, NewFailure(err.Error())) + websocket.JSON.Send(ws, bot.NewFailure(err.Error())) } if conf != nil { @@ -85,7 +86,7 @@ func addPlayer(ws *websocket.Conn) { }() go p.sender() p.recv() - log.Printf("%v has been disconnect from this game\n", p.Robot.Id) + log.Printf("game %s: %v has been disconnect from this game\n", gid, p.Robot.Id) } else { s := &Spectator{ send: make(chan *bot.Boardstate), diff --git a/player.go b/player.go index 9ee9ac5..5c54011 100644 --- a/player.go +++ b/player.go @@ -29,6 +29,7 @@ func (p *player) sender() { } } p.ws.Close() + log.Printf("player %s: sender close", p.Robot.Id) } func (p *player) recv() { @@ -51,6 +52,7 @@ func (p *player) recv() { log.Printf("%+v", p.Robot.Stats) } } + log.Printf("player %s: recv close", p.Robot.Id) p.ws.Close() } diff --git a/protocol.go b/protocol.go index d04214f..350d5a4 100644 --- a/protocol.go +++ b/protocol.go @@ -6,24 +6,12 @@ import ( "errors" ) -type Failure struct { - Reason string `json:"reason"` - Type string `json:"type"` -} - -func NewFailure(reason string) *Failure { - return &Failure{ - Reason: reason, - Type: "failure", - } -} - func Negociate(ws *websocket.Conn, id string, width, height float64) (*bot.Config, error) { var err error err = websocket.JSON.Send(ws, bot.NewIdRequest(id)) if err != nil { - return nil, errors.New("generic servr error") + return nil, errors.New("generic server error") } var clientid bot.ClientID @@ -34,7 +22,7 @@ func Negociate(ws *websocket.Conn, id string, width, height float64) (*bot.Confi if v, msg := clientid.Valid(); !v { websocket.JSON.Send( ws, - NewFailure(msg), + bot.NewFailure(msg), ) return nil, errors.New(msg) } @@ -42,7 +30,7 @@ func Negociate(ws *websocket.Conn, id string, width, height float64) (*bot.Confi gameParam := bot.NewGameParam(width, height) err = websocket.JSON.Send(ws, gameParam) if err != nil { - websocket.JSON.Send(ws, NewFailure("generic server error")) + websocket.JSON.Send(ws, bot.NewFailure("generic server error")) return nil, err } switch clientid.Type {