Commit f0d3d138 authored by ceddral's avatar ceddral
Browse files

gamelobby: restrict actions to agent player

parent 48be6f05
......@@ -16,6 +16,7 @@ type GameLobbyPlayer struct {
type GameLobby struct {
lock *sync.Mutex
agent *GameLobbyPlayer
players map[string]*GameLobbyPlayer
playerOrder []string
}
......@@ -50,6 +51,7 @@ func (gl *GameLobby) Register(glp *Player) {
}
gl.players[glp.name] = p
gl.playerOrder = append(gl.playerOrder, p.name)
gl.agent = gl.players[gl.playerOrder[0]]
go gl.Loop(p)
gl.Update()
gl.Unlock()
......@@ -59,6 +61,11 @@ func (gl *GameLobby) Register(glp *Player) {
func (gl *GameLobby) Unregister(name string) {
gl.Lock()
gl.unregisterUnlocked(name)
if len(gl.playerOrder) > 0 {
gl.agent = gl.players[gl.playerOrder[0]]
} else {
gl.agent = nil
}
gl.Unlock()
glp := gs.players[name]
glp.Close()
......@@ -116,16 +123,29 @@ func (gl *GameLobby) StartGame() {
NewGame(ps, []string{"base", "ext1"})
}
func (gl *GameLobby) agentAction(p *GameLobbyPlayer, key Key) {
switch key {
case keyReturn:
go gl.StartGame()
}
}
func (gl *GameLobby) patientAction(p *GameLobbyPlayer, key Key) {
}
func (gl *GameLobby) Loop(p *GameLobbyPlayer) {
for {
select {
case <-p.lobbyEvent:
case input := <-p.inputEvent:
key := toKey(input)
switch key {
case keyReturn:
// TODO only allow starting game for player #1
go gl.StartGame()
gl.Lock()
a := gl.agent
gl.Unlock()
if a == p {
gl.agentAction(p, key)
} else {
gl.patientAction(p, key)
}
}
select { // check kill before doing anything that may lock
......
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