diff --git a/server/model/game.py b/server/model/game.py index 1531171..1d5279c 100644 --- a/server/model/game.py +++ b/server/model/game.py @@ -87,7 +87,7 @@ class Game: if loser is not None: self.players.remove(loser) self.players.insert(0, loser) - self.message(MessageToPlayer.LoseRound, loser) + self.message(MessageToPlayer.LoseRound, extra=loser.name) return loser last_player = current_player diff --git a/server/test/test_lobby.py b/server/test/test_lobby.py index ee6d1a7..9292a35 100644 --- a/server/test/test_lobby.py +++ b/server/test/test_lobby.py @@ -1,4 +1,4 @@ -from typing import Optional, List +from typing import Optional, List, Any, Tuple from unittest import TestCase from server.game.manager import ClientManager @@ -23,10 +23,10 @@ class MockPlayer(Player): if bets is None: bets = [] self.bets: List[Optional[Hand]] = bets - self.messages: List[MessageToPlayer] = [] + self.messages: List[Tuple[MessageToPlayer, Any]] = [] def count(self, msg: MessageToPlayer) -> int: - return len([m for m in self.messages if m is msg]) + return len([m for (m, e) in self.messages if m is msg]) def announce(self, current_bet: Optional[Hand]) -> Announce: if self.bets: @@ -34,11 +34,11 @@ class MockPlayer(Player): else: return Announce(CARRE_ACE) - def receive(self, message: MessageToPlayer): - self.messages.append(message) + def receive(self, message: MessageToPlayer, extra: Optional[Any] = None): + self.messages.append((message, extra)) def print_msgs(self) -> str: - '|'.join([str(m) for m in self.messages]) + return '|'.join([f"{m}[{e}]" if e else str(m) for (m, e) in self.messages]) class MockManager(ClientManager): @@ -48,8 +48,8 @@ class MockManager(ClientManager): def send(self, to: Player, message: MessageToPlayer, extra=None): if isinstance(to, MockPlayer): - to.receive(message) - print(f"Sent {message} to {to}.") + to.receive(message, extra) + print(f"Sent {message} {'(' + str(extra) + ') ' if extra else ''}to {to}") class TestManager(TestCase): @@ -67,11 +67,10 @@ class TestManager(TestCase): def test_turn_messages(self): self.game.new_turn() - self.assertEqual(self.j1.count(MessageToPlayer.LoseRound), 6, f"j1 should lose 6 rounds.") - self.assertEqual(self.j2.count(MessageToPlayer.LoseRound), 6, f"j2 should lose 6 rounds.") - + self.assertGot(self.j1, MessageToPlayer.LoseRound, extra=self.j1.name, msg=f"J1 should hear round lost.") + self.assertGot(self.j2, MessageToPlayer.LoseRound, extra=self.j1.name, msg=f"J2 should hear round lost by j1.") for player in [self.j1, self.j2]: - self.assertIn(MessageToPlayer.Lose, player.messages, "Loser not announced") + self.assertGot(player, MessageToPlayer.LoseRound, msg="End of round not announced") def test_game_messages(self): self.game.new_game() @@ -89,5 +88,12 @@ class TestManager(TestCase): for player in [self.j1, self.j2]: self.assertEqual(player.count(MessageToPlayer.Announce), 7, f"{player} should see 7 announces: {player.print_msgs()}") - self.assertIn(MessageToPlayer.WinnerIs, player.messages, "Winner not announced") - self.assertIn(MessageToPlayer.Win, player.messages, "Win not told") + self.assertGot(player, MessageToPlayer.WinnerIs, self.j1, "Winner J1 not announced") + self.assertIn(MessageToPlayer.Win, self.j1.messages, "Win not told") + self.assertIn(MessageToPlayer.Lose, self.j2.messages, "Lose not told") + + def assertGot(self, player: MockPlayer, type: MessageToPlayer, extra=None, msg: str = "message not received"): + self.assertIn(type, [t for (t, _) in player.messages], msg) + if extra: + matching = [m for (m, e) in player.messages if e == extra and m == type] + self.assertTrue(matching, f"No message {type} with extra {extra}")