Skip to content

Commit

Permalink
bugfix: prevent game from sending infinite GameOver messages
Browse files Browse the repository at this point in the history
  • Loading branch information
minaorangina committed Mar 4, 2021
1 parent 6fbe6c9 commit 520de52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion engine/gameengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions engine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -89,7 +90,7 @@ type shed struct {
CurrentTurnIdx int
Stage Stage
ExpectedCommand protocol.Cmd
GameOver bool
gameOver bool
unseenDecision *protocol.InboundMessage
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 520de52

Please sign in to comment.