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).
This commit is contained in:
sm
2013-09-04 23:23:05 -07:00
parent 3e805d3da7
commit fb0e6d92d7
4 changed files with 20 additions and 20 deletions
+11 -2
View File
@@ -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()