octachrome / treason

A clone of the card game Coup written in Node.js
Other
139 stars 79 forks source link

"Player has won!" message changes if player leaves game #51

Open equationcrunchor opened 4 years ago

equationcrunchor commented 4 years ago

It appears that after the game ends, the game remembers the position of the player that won (e.g. the fourth player). However, if a player leaves, changing the order of players, the name in the banner message will change to the player who is now in that position, regardless of who actually won.

octachrome commented 4 years ago

Thanks for the bug report. Not sure when I'll get around to fixing this, I don't know how simple it will be.

k-hendricks commented 2 years ago

I noticed the same issue today. Looking through your code (which is incredibly clear and well-documented btw), I think one could just add an if statement on line 208 of game.js that checks if the winnerid is set when a player is leaving in the state WAITING_FOR_PLAYERS.

if (state.state.winnerIdx != null) {
  if (state.state.winnerIdx == playerIdx) {
    setState({
                  name: stateNames.WAITING_FOR_PLAYERS,
                  winnerIdx: null,
                  playerIdx: null
              });
   }
   else if (state.state.winnerIdx > playerIdx) {
      setState({
                      name: stateNames.WAITING_FOR_PLAYERS,
                      winnerIdx: state.state.winnerIdx - 1,
                      playerIdx: null
                  });
   }
}

So if the player who is leaving is the winner, the winner gets set to null (but their win will still be recorded in the game history), or if the leaving player's index is lower than the winner, the winner index is decremented.

I understand if you'd rather add a new state that differentiates between WAITING_FOR_PLAYERS and WAITING_TO_PLAY_AGAIN which would obviously be a larger change.