Put delete of game from controller in right place

Added some debugging
This commit is contained in:
Stephen McQuay 2014-03-29 01:12:55 -07:00
parent c8efd34080
commit 81beee8b43
5 changed files with 15 additions and 11 deletions

View File

@ -9,8 +9,8 @@ import (
"os" "os"
"runtime/pprof" "runtime/pprof"
"time" "time"
"bitbucket.org/hackerbots/botserv"
"bitbucket.org/hackerbots/botserv"
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
) )

View File

@ -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) { func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) {
key, err := c.getGameId(req.URL.Path) key, err := c.getGameId(req.URL.Path)
if err != nil { if err != nil {
@ -193,6 +194,7 @@ func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) {
return return
} }
g.kill <- true g.kill <- true
delete(c.Games.M, key)
message := struct { message := struct {
Ok bool `json:"ok"` Ok bool `json:"ok"`
Message string `json:"message"` 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 { if err := json.NewEncoder(w).Encode(message); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
log.Printf("returning from StopGame")
} }
func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) { func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) {

16
game.go
View File

@ -87,8 +87,6 @@ type Game struct {
stats GameStats stats GameStats
mode GameMode mode GameMode
bw *bandwidth.Bandwidth bw *bandwidth.Bandwidth
Conf Config
Games *MapLock
Verbose bool Verbose bool
} }
@ -124,7 +122,7 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m
spectators: make(map[*Spectator]bool), spectators: make(map[*Spectator]bool),
sregister: make(chan *Spectator), sregister: make(chan *Spectator),
sunregister: make(chan *Spectator), sunregister: make(chan *Spectator),
kill: make(chan bool, maxPlayer), kill: make(chan bool),
repair_hp: 5, repair_hp: 5,
repair_rate: 3.0, repair_rate: 3.0,
tick_duration: tick, tick_duration: tick,
@ -297,20 +295,18 @@ func (g *Game) sendUpdate(payload *Boardstate) {
func (g *Game) run() { func (g *Game) run() {
var t0, t1 time.Time 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 { for {
select { select {
case <-g.kill: case <-g.kill:
log.Printf("game %s: received kill signal, dying gracefully", g.id) log.Printf("game %s: received kill signal, dying gracefully", g.id)
close(g.bw.Quit) g.bw.Quit <- true
g.Games.Lock()
for player := range g.players { for player := range g.players {
close(player.send) close(player.send)
} }
delete(g.Games.M, g.id)
g.Games.Unlock()
return return
case p := <-g.register: case p := <-g.register:
log.Println("registering player:", p.Id)
g.players[p] = true g.players[p] = true
g.stats.PlayerStats[p.Id] = &PlayerStats{ g.stats.PlayerStats[p.Id] = &PlayerStats{
BotStats: make(map[string]*BotStats), BotStats: make(map[string]*BotStats),
@ -320,11 +316,14 @@ func (g *Game) run() {
r.gameStats = g.stats.PlayerStats[p.Id].BotStats[r.Name] r.gameStats = g.stats.PlayerStats[p.Id].BotStats[r.Name]
} }
case p := <-g.unregister: case p := <-g.unregister:
log.Println("unregistering player:", p.Id)
delete(g.players, p) delete(g.players, p)
close(p.send) close(p.send)
case s := <-g.sregister: case s := <-g.sregister:
log.Println("registering spectator:", s.Id)
g.spectators[s] = true g.spectators[s] = true
case s := <-g.sunregister: case s := <-g.sunregister:
log.Println("unregistering spectator:", s.Id)
delete(g.spectators, s) delete(g.spectators, s)
close(s.send) close(s.send)
case <-ticker.C: case <-ticker.C:
@ -362,6 +361,7 @@ func (g *Game) run() {
} }
} }
} }
log.Println("run done")
} }
func (g *Game) sendGameOver(eg *GameOver) { func (g *Game) sendGameOver(eg *GameOver) {

View File

@ -80,7 +80,7 @@ func (pt *protoTalker) sender() {
} }
} }
pt.counter.Close() pt.counter.Close()
log.Printf("%s: spectator sender close", pt.Id) log.Printf("%s: sender close", pt.Id)
} }
type player struct { type player struct {
@ -97,6 +97,7 @@ func NewPlayer(id string, ws *websocket.Conn, bw *bandwidth.Bandwidth, encoding
} }
func (p *player) recv() { func (p *player) recv() {
log.Println("starting recv")
for { for {
var msgs map[string]Instruction var msgs map[string]Instruction
err := p.dec.Decode(&msgs) err := p.dec.Decode(&msgs)
@ -106,7 +107,6 @@ func (p *player) recv() {
} }
for _, r := range p.Robots { for _, r := range p.Robots {
msg, ok := msgs[r.Id] msg, ok := msgs[r.Id]
if !ok { if !ok {

View File

@ -344,4 +344,5 @@ encodingLoops:
s.recv() s.recv()
log.Printf("game %s: spectator %+v has been disconnected from this game", gid.Id, s) log.Printf("game %s: spectator %+v has been disconnected from this game", gid.Id, s)
} }
log.Printf("exiting AddPlayer")
} }