diff --git a/server/game/lobby.py b/server/game/lobby.py index 7a3ae36..21c1600 100644 --- a/server/game/lobby.py +++ b/server/game/lobby.py @@ -5,8 +5,9 @@ from pydantic.main import BaseModel from socketio import AsyncServer from server.game.manager import ClientManager -from server.game.message import MessageToPlayer +from server.game.message import MessageToPlayer, MessageFromPlayer from server.model.game import Game +from server.model.known import CARRE_ACE from server.model.players import Player, Announce @@ -35,9 +36,9 @@ class LobbyManager(ClientManager): def players_ready(self): return [self.lobby[k] for k, m in self.metadata.items() if m.ready] - async def add_player(self, player: Player, is_ready: bool = False) -> str: + async def new_player(self, player: Player, sid: str) -> str: self.lobby[player.name] = player - self.metadata[player.name] = Metadata(ready=is_ready) + self.metadata[player.name] = Metadata(sid=sid) print(f"Added {player} to a lobby with {len(self.lobby)} players.") return f"Bienvenu, {player}! Il y a {len(self.lobby)} joueurs en ligne." @@ -64,18 +65,37 @@ class LobbyManager(ClientManager): def handle_message(self, sid, data): sender: Optional[Player] = None - sanitized = str(data) + message = None + extras = None + print(f"Lobby| Received message from {sid}: {data}.") - for player in self.players: - if player.name in sanitized: - sender = player + for name, metadata in self.metadata.items(): + if metadata.sid == sid: + sender = self.lobby[name] if sender: print(f"Lobby| Found sender: {sender.name}") - message = MessageToPlayer.Waiting - extras = [p.name for p in self.players] - body = {"message": message.name} + for option in MessageFromPlayer: + if option.value in data: + if option == MessageFromPlayer.Waiting: + self.metadata[sender.name].ready = False + extras = [p.name for p in self.players] + print(f"MSG|Player ready {extras}") + elif option == MessageFromPlayer.Ready: + self.wants_to_play(player=sender) + message = MessageToPlayer.Ready + extras = [p.name for p in self.players_ready] + elif option == MessageFromPlayer.Bet: + # FIXME vraie annonce, pas juste carre d'as lol + self.announces(player=sender, + announce=Announce(bet=CARRE_ACE)) + # TODO: connect with current game, return appropriate message + elif option == MessageFromPlayer.Menteur: + self.announces(player=sender, announce=Announce()) + # TODO: connect with current game, return appropriate message + + body = {"message": message.name if message else "none"} if extras: body["extras"] = extras return json.dumps(body) diff --git a/server/game/message.py b/server/game/message.py index bd037b1..221cd52 100644 --- a/server/game/message.py +++ b/server/game/message.py @@ -4,6 +4,7 @@ from enum import Enum class MessageToPlayer(Enum): Waiting = "WAITING_ROOM" Ready = "READY_ROOM" + NewGame = "NEW_GAME" GiveHand = "GIVE_HAND" WaitTurn = "WAITING_TURN" YourTurn = "YOUR_TURN" @@ -15,7 +16,7 @@ class MessageToPlayer(Enum): class MessageFromPlayer(Enum): - Wating = "WAITING" + Waiting = "WAITING" Ready = "READY" Bet = "BET" Menteur = "MENTEUR" diff --git a/server/ws.py b/server/ws.py index 037f852..1eb22a2 100644 --- a/server/ws.py +++ b/server/ws.py @@ -28,7 +28,7 @@ class ClientPlayer(Player): async def connect(sid, environ): print("[WS] Connect ", sid, environ) player = ClientPlayer(lobby) - reply: str = await lobby.add_player(player) + reply: str = await lobby.new_player(player, sid) await sio.emit('messageChannel', reply, room=sid)