diff --git a/server/game/lobby.py b/server/game/lobby.py index b542a88..c7ca5c6 100644 --- a/server/game/lobby.py +++ b/server/game/lobby.py @@ -1,5 +1,6 @@ import asyncio import json +import sys from sys import stderr from typing import List, Dict, Optional @@ -91,13 +92,14 @@ class LobbyManager(ClientManager): game = Game(players, manager=self) self.games.append(game) + print(f"Game started : {' vs '.join([p.name for p in players])}") + winner = await game.new_game() + print(f"Game ended : winner is {winner.name}! {len(players)} players back waiting in lobby.") + for p in players: self.metadata[p.name].ready = False - print(f"Game started : {' vs '.join([p.name for p in players])}") - await game.new_game() - - async def reset(self): + async def reset(self) -> str: players = len(self.players) games = len(self.games) msg = f"Resetting! sorry for the {players} players / {games} games..." @@ -110,12 +112,18 @@ class LobbyManager(ClientManager): self.players.clear() self.metadata.clear() self.games.clear() - print(f"Reset done, {players} players, {games} games.") return msg - async def send(self, to: Player, message: MessageToPlayer, extras=None): - sid = self.metadata[to.name].sid - game = self.game_with(to) + async def send(self, to: Player, message: MessageToPlayer, extras=None) -> None: + sid: str = "?" + + try: + sid = self.metadata[to.name].sid + except KeyError as e: + print(f"Error: Cannot send message to player {to.name}", file=stderr) + return # TODO: Warn other players and kill game + + game: Game = self.game_with(to) data = to.hand.json() # Start with "cards": [] data = json.loads(data) # JSONEncode and decode, starting with dict would miss `json()` encoders diff --git a/server/game/message.py b/server/game/message.py index 57678c1..7d6eb3c 100644 --- a/server/game/message.py +++ b/server/game/message.py @@ -9,6 +9,7 @@ class MessageToPlayer(Enum): GiveHand = "GIVE_HAND" WaitTurn = "WAITING_TURN" YourTurn = "YOUR_TURN" + WrongAnnounce = "WRONG_ANNOUNCE" Announce = "ANNOUNCE" LoseRound = "LOSE_ROUND" Win = "WINNER" diff --git a/server/model/game.py b/server/model/game.py index 8f05c01..04a02f7 100644 --- a/server/model/game.py +++ b/server/model/game.py @@ -1,5 +1,6 @@ import itertools from collections import defaultdict +from time import sleep from typing import List, Dict, Optional from server.game.manager import ClientManager @@ -57,8 +58,9 @@ class Game: winner = self.players[0] await self.message(MessageToPlayer.Win, winner) - await self.message(MessageToPlayer.WinnerIs, extras=winner) + await self.message(MessageToPlayer.WinnerIs, extras={"winner": winner.name}) print(f"Game over - {winner.name} wins with {len(winner.hand)} cards!") + return winner async def new_turn(self) -> Player: """ @@ -149,7 +151,8 @@ class Game: if not self.current_bet: # First player, has to bet something print("Game: First awaiting current bet") - await self.message(MessageToPlayer.YourTurn, current_player, extras=self.current_bet) + message = MessageToPlayer.YourTurn + await self.message(message, current_player, extras=self.current_bet) while not self.current_bet: # Ask a valid bet print("Game: While no bet, awaiting") announce = await current_player.announce(self.current_bet) @@ -161,6 +164,8 @@ class Game: extras={"player": current_player.name, "announce": announce.json()}) else: print(f"You cannot say Menteur on first round, {current_player}!") + message = MessageToPlayer.WrongAnnounce + sleep(5) else: # Next player, announce or menteur @@ -170,7 +175,7 @@ class Game: announce = Announce() # MENTEUR obligatoire else: print("Game: Awaiting bet") - await self.message(MessageToPlayer.YourTurn, current_player, extras=self.current_bet) + await self.message(message, current_player, extras=self.current_bet) announce = await current_player.announce(self.current_bet) if announce.bet: diff --git a/server/ws.py b/server/ws.py index 3b45c9b..e8bdd65 100644 --- a/server/ws.py +++ b/server/ws.py @@ -32,7 +32,9 @@ class ClientPlayer(Player): sleep(5) print(f"Client announced: {metadata.last_announce.bet}!") - return metadata.last_announce + got_announce = metadata.last_announce + metadata.fresh_announce = False + return got_announce @sio.event