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),
|
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),
|
kill: make(chan bool, 128),
|
||||||
}
|
}
|
||||||
|
|
||||||
g.robot_id = make(chan int)
|
g.robot_id = make(chan int)
|
||||||
@ -52,13 +52,18 @@ func NewGame(id string, width, height float64) *game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *game) run() {
|
func (g *game) run() {
|
||||||
|
started := false
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-g.kill:
|
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
|
return
|
||||||
case p := <-g.register:
|
case p := <-g.register:
|
||||||
g.players[p] = true
|
g.players[p] = true
|
||||||
|
started = true
|
||||||
case p := <-g.unregister:
|
case p := <-g.unregister:
|
||||||
delete(g.players, p)
|
delete(g.players, p)
|
||||||
close(p.send)
|
close(p.send)
|
||||||
@ -68,6 +73,10 @@ func (g *game) run() {
|
|||||||
delete(g.spectators, s)
|
delete(g.spectators, s)
|
||||||
close(s.send)
|
close(s.send)
|
||||||
case <-time.Tick(time.Duration(*tick) * time.Millisecond):
|
case <-time.Tick(time.Duration(*tick) * time.Millisecond):
|
||||||
|
if started && len(g.players) == 0 {
|
||||||
|
g.kill <- true
|
||||||
|
}
|
||||||
|
|
||||||
g.turn++
|
g.turn++
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
|
7
http.go
7
http.go
@ -57,14 +57,15 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Println("ERROR: game not found")
|
log.Println("ERROR: game not found")
|
||||||
|
websocket.JSON.Send(ws, bot.NewFailure("game 404"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := idg.Hash()
|
id := idg.Hash()
|
||||||
|
|
||||||
conf, err := Negociate(ws, id, 400, 800)
|
conf, err := Negociate(ws, id, game.width, game.height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
websocket.JSON.Send(ws, NewFailure(err.Error()))
|
websocket.JSON.Send(ws, bot.NewFailure(err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf != nil {
|
if conf != nil {
|
||||||
@ -85,7 +86,7 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
}()
|
}()
|
||||||
go p.sender()
|
go p.sender()
|
||||||
p.recv()
|
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 {
|
} else {
|
||||||
s := &Spectator{
|
s := &Spectator{
|
||||||
send: make(chan *bot.Boardstate),
|
send: make(chan *bot.Boardstate),
|
||||||
|
@ -29,6 +29,7 @@ func (p *player) sender() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.ws.Close()
|
p.ws.Close()
|
||||||
|
log.Printf("player %s: sender close", p.Robot.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) recv() {
|
func (p *player) recv() {
|
||||||
@ -51,6 +52,7 @@ func (p *player) recv() {
|
|||||||
log.Printf("%+v", p.Robot.Stats)
|
log.Printf("%+v", p.Robot.Stats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("player %s: recv close", p.Robot.Id)
|
||||||
p.ws.Close()
|
p.ws.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
protocol.go
18
protocol.go
@ -6,24 +6,12 @@ import (
|
|||||||
"errors"
|
"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) {
|
func Negociate(ws *websocket.Conn, id string, width, height float64) (*bot.Config, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
err = websocket.JSON.Send(ws, bot.NewIdRequest(id))
|
err = websocket.JSON.Send(ws, bot.NewIdRequest(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("generic servr error")
|
return nil, errors.New("generic server error")
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientid bot.ClientID
|
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 {
|
if v, msg := clientid.Valid(); !v {
|
||||||
websocket.JSON.Send(
|
websocket.JSON.Send(
|
||||||
ws,
|
ws,
|
||||||
NewFailure(msg),
|
bot.NewFailure(msg),
|
||||||
)
|
)
|
||||||
return nil, errors.New(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)
|
gameParam := bot.NewGameParam(width, height)
|
||||||
err = websocket.JSON.Send(ws, gameParam)
|
err = websocket.JSON.Send(ws, gameParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
websocket.JSON.Send(ws, NewFailure("generic server error"))
|
websocket.JSON.Send(ws, bot.NewFailure("generic server error"))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
switch clientid.Type {
|
switch clientid.Type {
|
||||||
|
Loading…
Reference in New Issue
Block a user