Commit 60956f99 authored by ceddral's avatar ceddral
Browse files

game: allow leaving at the end of a game

parent d32bd3d3
......@@ -24,6 +24,8 @@ type GameStateWithPiece interface {
}
func (g *Game) broadcastState(state GameState) {
g.Lock()
defer g.Unlock()
for _, p := range g.players {
select {
case p.gameEvent <- state:
......@@ -97,6 +99,7 @@ type GamePlayer struct {
tokens map[Token]int
inputEvent chan []byte
gameEvent chan GameState
gameKill chan struct{}
scr *tg.Screen
menu GameMenu
menuv *tg.View
......@@ -306,6 +309,7 @@ func NewGame(pns []string, extensions []string) (g *Game) {
tokens: make(map[Token]int),
inputEvent: glp.inputEvent,
gameEvent: make(chan GameState, 100),
gameKill: make(chan struct{}, 1),
scr: glp.scr,
menuv: tg.NewView(glp.scr, 0, 0, 30, glp.scr.H),
boardv: tg.NewView(glp.scr, 30, 0, glp.scr.W-30, glp.scr.H),
......@@ -415,6 +419,11 @@ func (g *Game) Loop(p *GamePlayer) {
}
if anyStateAnyRoleAction(state, p, key) { break }
}
select { // check kill before doing anything that may lock
case <-p.gameKill:
return
default:
}
p.menu.Redraw(state)
g.board.Redraw(p, state)
if fullRedraw {
......@@ -838,5 +847,21 @@ func (s *StateFinished) agentTransition(p *GamePlayer) {
})
}
func (s *StateFinished) patientTransition(p *GamePlayer) {}
func (s *StateFinished) agentAction(p *GamePlayer, key Key) bool { return false }
func (s *StateFinished) patientAction(p *GamePlayer, key Key) bool { return false }
func (s *StateFinished) agentAction(p *GamePlayer, key Key) bool { return s.action(p, key) }
func (s *StateFinished) patientAction(p *GamePlayer, key Key) bool { return s.action(p, key) }
func (s *StateFinished) action(p *GamePlayer, key Key) bool {
if key == keyReturn {
s.g.Lock()
delete(s.g.players, p.name)
s.g.Unlock()
p.gameKill <- struct{}{}
p.scr.Clear()
gs.Lock()
glp := gs.players[p.name]
glp.context = glgl
glp.context.Register(glp)
gs.Unlock()
return true
}
return false
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment