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"
"runtime/pprof"
"time"
"bitbucket.org/hackerbots/botserv"
"bitbucket.org/hackerbots/botserv"
"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) {
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) {

16
game.go
View File

@ -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) {

View File

@ -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 {

View File

@ -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")
}