From 2f3f4dbc1471191f846b43f1417b956bc769232c Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Tue, 15 Jul 2025 11:51:02 +0200 Subject: [PATCH] added bet mechanism (currently without checks for invalid bets) --- components/web_client_tcp.gd | 4 +++- game.tscn | 15 ++++++++++----- game_client.gd | 5 +++++ server_tcp.py | 31 ++++++++++++++++++++++++------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index d1128e1..fb95c83 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -39,6 +39,8 @@ signal connection_status_changed(new_status: ConnectionStatus) signal client_order_received(client_ids: Array[String]) signal cards_received(cards: Dictionary) +signal received_bet_request(disallowed_bet: int) + enum ConnectionStatus { DISCONNECTED, ATTEMPTING_CONNECTION, @@ -133,7 +135,7 @@ func _handle_response(response: String) -> void: "client_submitted_bet": print(data) "request_bet": - print(data) + received_bet_request.emit.call_deferred(data["disallowed_bet"]) "begin_playing_cards": print(data) "request_card": diff --git a/game.tscn b/game.tscn index da9b59e..7c59dbf 100644 --- a/game.tscn +++ b/game.tscn @@ -183,11 +183,15 @@ disabled = true text = "Play Card" [node name="BetContainer" type="VBoxContainer" parent="HBoxContainer/GameUI"] -layout_mode = 0 -offset_left = 218.0 -offset_top = 833.0 -offset_right = 445.0 -offset_bottom = 899.0 +layout_mode = 1 +anchors_preset = 2 +anchor_top = 1.0 +anchor_bottom = 1.0 +offset_left = 100.0 +offset_top = -180.0 +offset_right = 327.0 +offset_bottom = -114.0 +grow_vertical = 0 [node name="BetInput" type="LineEdit" parent="HBoxContainer/GameUI/BetContainer"] layout_mode = 2 @@ -247,6 +251,7 @@ transform = Transform3D(-0.5, 0, 0.866025, 0, 1, 0, -0.866025, 0, -0.5, 3.2, 0.4 [connection signal="connection_status_changed" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connection_status_changed"] [connection signal="disconnected_from_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_disconnected_from_lobby"] [connection signal="disconnected_from_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_disconnected_from_server"] +[connection signal="received_bet_request" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_received_bet_request"] [connection signal="received_chat_message" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_received_chat_message"] [connection signal="received_client_id" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_received_client_id"] [connection signal="received_connection_message" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_received_connection_message"] diff --git a/game_client.gd b/game_client.gd index a95b9ed..f3f8916 100644 --- a/game_client.gd +++ b/game_client.gd @@ -58,4 +58,9 @@ func _on_player_hand_card_selected(card_id: int) -> void: play_card_button.disabled = card_id == -1 func _on_submit_bet_button_pressed() -> void: + bet_container.hide() web_client.submit_bet(int(bet_input.text.strip_edges()), _current_round) + +func _on_web_client_tcp_received_bet_request(disallowed_bet: int) -> void: + bet_input.text = "" + bet_container.show() diff --git a/server_tcp.py b/server_tcp.py index 96d96ed..6e24033 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -182,8 +182,6 @@ class LobbyManager: cards.pop("trump") for client_id in client_ids: - # client_cards = cards[client_id] - print(cards) await self.send_message( lobby_id, client_id, @@ -196,6 +194,16 @@ class LobbyManager: }) ) + async def request_bet(self, lobby_id, client_id, disallowed_bet) -> None: + await self.send_message( + lobby_id, + client_id, + json.dumps({ + "response": "request_bet", + "disallowed_bet": disallowed_bet, + }) + ) + async def spread_bet(self, lobby_id, client_id, bet) -> None: await self.broadcast_message( lobby_id, @@ -398,6 +406,12 @@ async def process_input(message: str, websocket) -> None: print(f"Lobby {lobby_id} is starting the game, playing order is: {lobby.client_playing_order}") + await lobby_manager.request_bet( + lobby_id, + lobby_manager.get_lobby(lobby_id).client_playing_order[0], + -1, + ) + # Start next round (host only) elif data_object["response"] == "start_next_round": lobby_id = data_object["lobby_id"] @@ -412,6 +426,12 @@ async def process_input(message: str, websocket) -> None: None, ) + await lobby_manager.request_bet( + lobby_id, + lobby_manager.get_lobby(lobby_id).client_playing_order[0], + -1, + ) + elif data_object["response"] == "submit_bet": lobby_id = data_object["lobby_id"] client_id = data_object["client_id"] @@ -454,13 +474,10 @@ async def process_input(message: str, websocket) -> None: total_bet = sum(lobby_manager.get_lobby(lobby_id).client_bets.values()) disallowed_bet = total_bet - data_object["round"] - await lobby_manager.send_message( + await lobby_manager.request_bet( lobby_id, lobby_manager.get_lobby(lobby_id).client_playing_order[last_client_index + 1], - json.dumps({ - "response": "request_bet", - "disallowed_bet": disallowed_bet, - }), + disallowed_bet, ) elif data_object["response"] == "play_card":