feat: Send other players waiting

parent 52760898
...@@ -5,8 +5,9 @@ from pydantic.main import BaseModel ...@@ -5,8 +5,9 @@ from pydantic.main import BaseModel
from socketio import AsyncServer from socketio import AsyncServer
from server.game.manager import ClientManager 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.game import Game
from server.model.known import CARRE_ACE
from server.model.players import Player, Announce from server.model.players import Player, Announce
...@@ -35,9 +36,9 @@ class LobbyManager(ClientManager): ...@@ -35,9 +36,9 @@ class LobbyManager(ClientManager):
def players_ready(self): def players_ready(self):
return [self.lobby[k] for k, m in self.metadata.items() if m.ready] 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.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.") 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." return f"Bienvenu, {player}! Il y a {len(self.lobby)} joueurs en ligne."
...@@ -64,18 +65,37 @@ class LobbyManager(ClientManager): ...@@ -64,18 +65,37 @@ class LobbyManager(ClientManager):
def handle_message(self, sid, data): def handle_message(self, sid, data):
sender: Optional[Player] = None sender: Optional[Player] = None
sanitized = str(data) message = None
extras = None
print(f"Lobby| Received message from {sid}: {data}.") print(f"Lobby| Received message from {sid}: {data}.")
for player in self.players: for name, metadata in self.metadata.items():
if player.name in sanitized: if metadata.sid == sid:
sender = player sender = self.lobby[name]
if sender: if sender:
print(f"Lobby| Found sender: {sender.name}") print(f"Lobby| Found sender: {sender.name}")
message = MessageToPlayer.Waiting for option in MessageFromPlayer:
extras = [p.name for p in self.players] if option.value in data:
body = {"message": message.name} 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: if extras:
body["extras"] = extras body["extras"] = extras
return json.dumps(body) return json.dumps(body)
...@@ -4,6 +4,7 @@ from enum import Enum ...@@ -4,6 +4,7 @@ from enum import Enum
class MessageToPlayer(Enum): class MessageToPlayer(Enum):
Waiting = "WAITING_ROOM" Waiting = "WAITING_ROOM"
Ready = "READY_ROOM" Ready = "READY_ROOM"
NewGame = "NEW_GAME"
GiveHand = "GIVE_HAND" GiveHand = "GIVE_HAND"
WaitTurn = "WAITING_TURN" WaitTurn = "WAITING_TURN"
YourTurn = "YOUR_TURN" YourTurn = "YOUR_TURN"
...@@ -15,7 +16,7 @@ class MessageToPlayer(Enum): ...@@ -15,7 +16,7 @@ class MessageToPlayer(Enum):
class MessageFromPlayer(Enum): class MessageFromPlayer(Enum):
Wating = "WAITING" Waiting = "WAITING"
Ready = "READY" Ready = "READY"
Bet = "BET" Bet = "BET"
Menteur = "MENTEUR" Menteur = "MENTEUR"
...@@ -28,7 +28,7 @@ class ClientPlayer(Player): ...@@ -28,7 +28,7 @@ class ClientPlayer(Player):
async def connect(sid, environ): async def connect(sid, environ):
print("[WS] Connect ", sid, environ) print("[WS] Connect ", sid, environ)
player = ClientPlayer(lobby) 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) await sio.emit('messageChannel', reply, room=sid)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment