From 08773d963ced9e6c2a3e5c2d0bd192cf714e4365 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Tue, 8 Jul 2025 10:42:34 +0200 Subject: [PATCH] server: added more gameplay commands and determine playing order on start_game command --- components/web_client_tcp.gd | 14 +++++++- game.tscn | 2 ++ game_client.gd | 37 ++++++++++++++++--- server_tcp.py | 70 +++++++++++++++++++++++++++++++++--- 4 files changed, 113 insertions(+), 10 deletions(-) diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index b102f15..93f66d5 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -117,6 +117,10 @@ func _handle_response(response: String) -> void: "new_round_started": print(data) + "client_submitted_bet": + print(data) + "client_played_card": + print(data) func _handle_join(data) -> void: match data["status"]: @@ -173,7 +177,15 @@ func start_game() -> void: _send_message(JSON.stringify({ "response": "start_game", "lobby_id": _lobby_id, - "cards": CardManager.get_shuffled_cards(_connected_client_ids, 30), + "cards": CardManager.get_shuffled_cards(_connected_client_ids, 1), + })) + +func start_next_round(round_count: int) -> void: + _send_message(JSON.stringify({ + "response": "start_next_round", + "lobby_id": _lobby_id, + "round": round_count, + "cards": CardManager.get_shuffled_cards(_connected_client_ids, round_count), })) func _send_message(message: String) -> void: diff --git a/game.tscn b/game.tscn index e0e28f0..0752c6f 100644 --- a/game.tscn +++ b/game.tscn @@ -156,7 +156,9 @@ grow_vertical = 2 text = "Start Game!" [connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_joined_lobby"] +[connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_joined_lobby"] [connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_left_lobby"] +[connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_left_lobby"] [connection signal="connected_to_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connection_error_occurred" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] diff --git a/game_client.gd b/game_client.gd index a2293a7..193a1e8 100644 --- a/game_client.gd +++ b/game_client.gd @@ -5,15 +5,30 @@ extends Node @export var start_game_button: Button var _is_host: bool = false +var _current_player_count: int = 0 +var _current_round: int = 0 +var _max_rounds: int = 0 func _set_host(is_host: bool) -> void: _is_host = is_host - if is_host: - start_game_button.show() - else: - start_game_button.hide() + +func _start_next_round() -> void: + _current_round += 1 + web_client.start_next_round(_current_round) + +func _get_max_rounds(player_count: int) -> int: + match player_count: + 3: + return 20 + 4: + return 15 + 5: + return 12 + _: # 6 + return 10 func _on_start_game_button_pressed() -> void: + _current_round = 1 web_client.start_game() func _on_create_lobby_button_pressed() -> void: @@ -21,3 +36,17 @@ func _on_create_lobby_button_pressed() -> void: func _on_join_lobby_button_pressed() -> void: _set_host(false) + +func _update_player_count(player_count: int) -> void: + _current_player_count = player_count + _max_rounds = _get_max_rounds(player_count) + if _is_host and player_count >= 3: + start_game_button.show() + else: + start_game_button.hide() + +func _on_web_client_tcp_client_joined_lobby(client_name: String, connected_client_names: Array[String]) -> void: + _update_player_count(connected_client_names.size()) + +func _on_web_client_tcp_client_left_lobby(client_name: String, connected_client_names: Array[String]) -> void: + _update_player_count(connected_client_names.size()) diff --git a/server_tcp.py b/server_tcp.py index d47df04..546e169 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -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__':