From 81beee8b434ef7ec8ef4f6a9d9b5e901fbc83d3d Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sat, 29 Mar 2014 01:12:55 -0700 Subject: [PATCH] Put delete of game from controller in right place Added some debugging --- bserv/main.go | 2 +- control.go | 3 +++ game.go | 16 ++++++++-------- player.go | 4 ++-- protocol.go | 1 + 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bserv/main.go b/bserv/main.go index 2b83e2c..b8066a4 100644 --- a/bserv/main.go +++ b/bserv/main.go @@ -9,8 +9,8 @@ import ( "os" "runtime/pprof" "time" - "bitbucket.org/hackerbots/botserv" + "bitbucket.org/hackerbots/botserv" "code.google.com/p/go.net/websocket" ) diff --git a/control.go b/control.go index a5708b7..795d19a 100644 --- a/control.go +++ b/control.go @@ -178,6 +178,7 @@ func (c *Controller) BW(w http.ResponseWriter, req *http.Request) { } } +// StopGame is the only mechanism to decrease the number of running games in a Controller func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) { key, err := c.getGameId(req.URL.Path) if err != nil { @@ -193,6 +194,7 @@ func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) { return } g.kill <- true + delete(c.Games.M, key) message := struct { Ok bool `json:"ok"` Message string `json:"message"` @@ -203,6 +205,7 @@ func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) { if err := json.NewEncoder(w).Encode(message); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } + log.Printf("returning from StopGame") } func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) { diff --git a/game.go b/game.go index 8593db5..68c29a0 100644 --- a/game.go +++ b/game.go @@ -87,8 +87,6 @@ type Game struct { stats GameStats mode GameMode bw *bandwidth.Bandwidth - Conf Config - Games *MapLock Verbose bool } @@ -124,7 +122,7 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m spectators: make(map[*Spectator]bool), sregister: make(chan *Spectator), sunregister: make(chan *Spectator), - kill: make(chan bool, maxPlayer), + kill: make(chan bool), repair_hp: 5, repair_rate: 3.0, tick_duration: tick, @@ -297,20 +295,18 @@ func (g *Game) sendUpdate(payload *Boardstate) { func (g *Game) run() { var t0, t1 time.Time - ticker := time.NewTicker(time.Duration(g.Conf.Tick) * time.Millisecond) + ticker := time.NewTicker(time.Duration(g.tick_duration) * time.Millisecond) for { select { case <-g.kill: log.Printf("game %s: received kill signal, dying gracefully", g.id) - close(g.bw.Quit) - g.Games.Lock() + g.bw.Quit <- true for player := range g.players { close(player.send) } - delete(g.Games.M, g.id) - g.Games.Unlock() return case p := <-g.register: + log.Println("registering player:", p.Id) g.players[p] = true g.stats.PlayerStats[p.Id] = &PlayerStats{ BotStats: make(map[string]*BotStats), @@ -320,11 +316,14 @@ func (g *Game) run() { r.gameStats = g.stats.PlayerStats[p.Id].BotStats[r.Name] } case p := <-g.unregister: + log.Println("unregistering player:", p.Id) delete(g.players, p) close(p.send) case s := <-g.sregister: + log.Println("registering spectator:", s.Id) g.spectators[s] = true case s := <-g.sunregister: + log.Println("unregistering spectator:", s.Id) delete(g.spectators, s) close(s.send) case <-ticker.C: @@ -362,6 +361,7 @@ func (g *Game) run() { } } } + log.Println("run done") } func (g *Game) sendGameOver(eg *GameOver) { diff --git a/player.go b/player.go index 50b606a..bf1204a 100644 --- a/player.go +++ b/player.go @@ -80,7 +80,7 @@ func (pt *protoTalker) sender() { } } pt.counter.Close() - log.Printf("%s: spectator sender close", pt.Id) + log.Printf("%s: sender close", pt.Id) } type player struct { @@ -97,6 +97,7 @@ func NewPlayer(id string, ws *websocket.Conn, bw *bandwidth.Bandwidth, encoding } func (p *player) recv() { + log.Println("starting recv") for { var msgs map[string]Instruction err := p.dec.Decode(&msgs) @@ -106,7 +107,6 @@ func (p *player) recv() { } for _, r := range p.Robots { - msg, ok := msgs[r.Id] if !ok { diff --git a/protocol.go b/protocol.go index 2ebbf2b..61cf014 100644 --- a/protocol.go +++ b/protocol.go @@ -344,4 +344,5 @@ encodingLoops: s.recv() log.Printf("game %s: spectator %+v has been disconnected from this game", gid.Id, s) } + log.Printf("exiting AddPlayer") }