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:
parent
3e805d3da7
commit
fb0e6d92d7
13
game.go
13
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()
|
||||
|
||||
|
7
http.go
7
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),
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
18
protocol.go
18
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user