host client can now start round and send out shuffled cards, server can spread them to all connected clients

This commit is contained in:
2025-07-07 22:40:46 +02:00
parent 4ac3fbd962
commit 3122df893a
5 changed files with 76 additions and 14 deletions
+40
View File
@@ -21,6 +21,7 @@ class Lobby:
self.lobby_id = lobby_id
self.host_client_id = host_client_id
self.connected_clients = {}
self.current_round = 0
class Client:
@@ -68,6 +69,7 @@ class LobbyManager:
"response": "client_joined_lobby",
"new_client_id": client_id,
"new_client_name": client_name,
"connected_client_ids": self.get_lobby_client_ids(lobby_id),
"connected_client_names": self.get_lobby_client_names(lobby_id),
})
)
@@ -90,6 +92,7 @@ class LobbyManager:
"response": "client_left_lobby",
"status": "ok",
"client_name": client_name,
"connected_client_ids": self.get_lobby_client_ids(lobby_id),
"connected_client_names": self.get_lobby_client_names(lobby_id),
}),
)
@@ -136,6 +139,9 @@ class LobbyManager:
for client_id in lobby.connected_clients:
names.append(lobby.connected_clients[client_id].client_name)
return names
def get_lobby_client_ids(self, lobby_id) -> list[str]:
return list(self.get_lobby(lobby_id).connected_clients.keys())
# Checks if a client is connected to a given lobby under the given ID
def client_id_exists_in_lobby(self, lobby_id, client_id) -> bool:
@@ -163,6 +169,32 @@ class LobbyManager:
#endregion
#region gameplay
async def spread_cards(self, lobby_id, cards) -> None:
client_ids = self.get_lobby_client_ids(lobby_id)
trump = None
if cards["trump"]:
trump = cards["trump"]
self.get_lobby(lobby_id).current_round += 1
for client_id in client_ids:
client_cards = cards[client_id]
await self.send_message(
lobby_id,
client_id,
json.dumps({
"response": "new_round_started",
"current_round": self.get_lobby(lobby_id).current_round,
"cards": client_cards,
"trump": trump,
})
)
#endregion
#region miscellaneous
# Generates an ID and ensures uniqueness among all lobbies.
@@ -323,6 +355,14 @@ async def process_input(message: str, websocket) -> None:
})
)
print(f"Relayed chat message by {client_name}")
# Start game (host only)
elif data_object["response"] == "start_game":
await lobby_manager.spread_cards(
data_object["lobby_id"],
data_object["cards"],
)
if __name__ == '__main__':
run_local = False