From 520de5266f3888323ab3d316b9d8275a51780b63 Mon Sep 17 00:00:00 2001 From: Mina Date: Thu, 4 Mar 2021 12:56:28 +0000 Subject: [PATCH] bugfix: prevent game from sending infinite GameOver messages --- engine/gameengine.go | 2 +- engine/utils.go | 4 ++++ game/game.go | 17 +++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/engine/gameengine.go b/engine/gameengine.go index 5ea7986..ed191ef 100644 --- a/engine/gameengine.go +++ b/engine/gameengine.go @@ -202,7 +202,7 @@ func (ge *gameEngine) Listen() { case msgs := <-ge.outboundCh: ge.messagePlayers(msgs) - if ge.game.AwaitingResponse() == protocol.Null { + if !ge.game.GameOver() && ge.game.AwaitingResponse() == protocol.Null { ge.sendToGame(nil) } diff --git a/engine/utils.go b/engine/utils.go index d9ebb55..db7457f 100644 --- a/engine/utils.go +++ b/engine/utils.go @@ -36,6 +36,10 @@ func (g *SpyGame) ReceiveResponse(messages []protocol.InboundMessage) ([]protoco return nil, nil } +func (g *SpyGame) GameOver() bool { + return false +} + func namesToPlayers(names []string) Players { ps := []Player{} for _, n := range names { diff --git a/game/game.go b/game/game.go index 6b9758f..18fbf10 100644 --- a/game/game.go +++ b/game/game.go @@ -76,6 +76,7 @@ type Game interface { Next() ([]protocol.OutboundMessage, error) ReceiveResponse([]protocol.InboundMessage) ([]protocol.OutboundMessage, error) AwaitingResponse() protocol.Cmd + GameOver() bool } type shed struct { @@ -89,7 +90,7 @@ type shed struct { CurrentTurnIdx int Stage Stage ExpectedCommand protocol.Cmd - GameOver bool + gameOver bool unseenDecision *protocol.InboundMessage } @@ -165,6 +166,10 @@ func (s *shed) AwaitingResponse() protocol.Cmd { return s.ExpectedCommand } +func (s *shed) GameOver() bool { + return s.gameOver +} + func (s *shed) Start(playerInfo []protocol.PlayerInfo) error { if s == nil { return ErrNilGame @@ -206,7 +211,7 @@ func (s *shed) Next() ([]protocol.OutboundMessage, error) { if s.ExpectedCommand != protocol.Null { return nil, ErrGameAwaitingResponse } - if s.GameOver { + if s.gameOver { return s.buildGameOverMessages(), nil } @@ -277,7 +282,7 @@ func (s *shed) ReceiveResponse(inboundMsgs []protocol.InboundMessage) ([]protoco if s.ExpectedCommand == protocol.Null { return nil, ErrGameUnexpectedResponse } - if s.GameOver { + if s.gameOver { return s.buildGameOverMessages(), nil } @@ -399,8 +404,8 @@ func (s *shed) ReceiveResponse(inboundMsgs []protocol.InboundMessage) ([]protoco s.ExpectedCommand = protocol.Null s.moveToFinishedPlayers() // handles the next turn - if s.gameIsOver() { - s.GameOver = true + if s.onePlayerLeft() { + s.gameOver = true // move the remaining player s.moveToFinishedPlayers() return s.buildGameOverMessages(), nil @@ -573,7 +578,7 @@ func (s *shed) playerHasFinished() bool { len(pc.Unseen) == 0 } -func (s *shed) gameIsOver() bool { +func (s *shed) onePlayerLeft() bool { return len(s.ActivePlayers) == 1 }