server: added more gameplay commands and determine playing order on start_game command

This commit is contained in:
2025-07-08 10:42:34 +02:00
parent 3122df893a
commit 08773d963c
4 changed files with 113 additions and 10 deletions
+65 -5
View File
@@ -21,7 +21,8 @@ class Lobby:
self.lobby_id = lobby_id
self.host_client_id = host_client_id
self.connected_clients = {}
self.current_round = 0
self.client_playing_order = []
self.currently_playing_client = 0 # index of client_playing_order
class Client:
@@ -171,15 +172,13 @@ class LobbyManager:
#region gameplay
async def spread_cards(self, lobby_id, cards) -> None:
async def spread_cards(self, lobby_id, round, cards, playing_order) -> 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(
@@ -187,11 +186,37 @@ class LobbyManager:
client_id,
json.dumps({
"response": "new_round_started",
"current_round": self.get_lobby(lobby_id).current_round,
"current_round": round,
"cards": client_cards,
"trump": trump,
"playing_order": playing_order,
})
)
async def spread_bet(self, lobby_id, client_id, bet) -> None:
await self.broadcast_message(
lobby_id,
json.dumps({
"response": "client_played_card",
"client_id": client_id,
"bet": bet,
"next_client": None, # TODO!
})
)
async def spread_played_card(self, lobby_id, client_id, played_card) -> None:
await self.broadcast_message(
lobby_id,
json.dumps({
"response": "client_played_card",
"client_id": client_id,
"card": played_card,
"next_client": None, # TODO!
})
)
def get_next_client(self, lobby_id, client_id) -> str:
pass
#endregion
@@ -358,10 +383,45 @@ async def process_input(message: str, websocket) -> None:
# Start game (host only)
elif data_object["response"] == "start_game":
lobby_id = data_object["lobby_id"]
lobby = lobby_manager.get_lobby(lobby_id)
lobby.client_playing_order = list(lobby.connected_clients.keys())
random.shuffle(lobby.client_playing_order)
await lobby_manager.spread_cards(
data_object["lobby_id"],
1,
data_object["cards"],
lobby.client_playing_order,
)
print(f"Lobby {lobby_id} is starting the game, playing order is: {lobby.client_playing_order}")
# Start next round (host only)
elif data_object["response"] == "start_next_round":
lobby_id = data_object["lobby_id"]
await lobby_manager.spread_cards(
lobby_id,
data_object["round"],
data_object["cards"],
None,
)
elif data_object["response"] == "submit_bet":
await lobby_manager.spread_bet(
data_object["lobby_id"],
data_object["client_id"],
data_object["bet"],
)
elif data_object["response"] == "play_card":
await lobby_manager.spread_played_card(
data_object["lobby_id"],
data_object["client_id"],
data_object["played_card"],
)
if __name__ == '__main__':