From a2bab58d3f3abe5aebf3f021b3e7f8574c533a72 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Mon, 7 Jul 2025 09:58:34 +0200 Subject: [PATCH] server: sending connected client names instead of count on every (dis)connect --- cards/card_manager.tscn | 17 ++++++------ components/client_interface.gd | 6 ++--- components/web_client_tcp.gd | 18 ++++++------- game.tscn | 47 +++++++++++++++++----------------- server_tcp.py | 17 +++++++++--- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/cards/card_manager.tscn b/cards/card_manager.tscn index 23b263b..9f46145 100644 --- a/cards/card_manager.tscn +++ b/cards/card_manager.tscn @@ -235,6 +235,14 @@ card_value = 3 color = 2 metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" +[sub_resource type="Resource" id="Resource_gxifu"] +script = ExtResource("2_vmpa3") +card_id = 29 +card_type = 0 +card_value = 4 +color = 2 +metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" + [sub_resource type="Resource" id="Resource_rcngd"] script = ExtResource("2_vmpa3") card_id = 30 @@ -475,15 +483,8 @@ card_value = 1 color = 4 metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" -[sub_resource type="Resource" id="Resource_gxifu"] -script = ExtResource("2_vmpa3") -card_id = 29 -card_type = 0 -card_value = 4 -color = 2 -metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" - [node name="CardManager" type="Node"] +editor_description = "desc" script = ExtResource("1_de166") full_deck = Dictionary[int, ExtResource("2_vmpa3")]({ 0: SubResource("Resource_hq606"), diff --git a/components/client_interface.gd b/components/client_interface.gd index c29c5f2..24e8753 100644 --- a/components/client_interface.gd +++ b/components/client_interface.gd @@ -23,7 +23,7 @@ func _on_create_lobby_button_pressed() -> void: client.create_lobby(name_input.text.strip_edges()) func _on_connect_button_pressed() -> void: - client.connect_to_server($MarginContainer/VBoxContainer/URLInput.text) + client.connect_to_server($MarginContainer/VBoxContainer/ServerInputs/URLInput.text) func _on_join_lobby_button_pressed() -> void: client.join_lobby(lobby_code_input.text.strip_edges(), name_input.text.strip_edges()) @@ -54,7 +54,7 @@ func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void: func _on_web_client_tcp_disconnected_from_lobby() -> void: _write_line("You disconnected from the lobby") -func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void: +func _on_web_client_tcp_client_joined_lobby(client_name: String, currently_connected_client_names: Array) -> void: _write_line("{client_name} joined the lobby{you}".format({ "client_name": client_name, "you": " (that's you!)" if client_name == name_input.text.strip_edges() else "" @@ -66,7 +66,7 @@ func _on_web_client_tcp_received_chat_message(client_name: String, message: Stri "message": message, })) -func _on_web_client_tcp_client_left_lobby(client_name: String) -> void: +func _on_web_client_tcp_client_left_lobby(client_name: String, currently_connected_client_names: Array) -> void: _write_line("{client_name} left the lobby".format({"client_name": client_name})) func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void: diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index 9823d48..384dff0 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -17,7 +17,7 @@ var cached_response: String var _thread: Thread var _stop_thread: bool = false -var _players_in_lobby: int = 0 +var _currently_connected_client_names: Array[String] = [] signal connected_to_server() signal connection_error_occurred() @@ -26,8 +26,8 @@ signal disconnected_from_server() signal connected_to_lobby(lobby_id: String) signal disconnected_from_lobby() -signal client_joined_lobby(client_name: String) -signal client_left_lobby(client_name: String) +signal client_joined_lobby(client_name: String, currently_connected_client_names: Array[String]) +signal client_left_lobby(client_name: String, currently_connected_client_names: Array[String]) signal received_chat_message(client_name: String, message: String) @@ -100,18 +100,18 @@ func _handle_response(response: String) -> void: connected_to_lobby.emit.call_deferred(_lobby_id) connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED) "client_joined_lobby": - client_joined_lobby.emit.call_deferred(data["new_client_name"]) - _players_in_lobby = data["connected_client_count"] - print(_players_in_lobby) + _currently_connected_client_names.assign(data["connected_client_names"]) + client_joined_lobby.emit.call_deferred(data["new_client_name"], _currently_connected_client_names) + print(_currently_connected_client_names) "leave_lobby_request": leave_lobby() "leave_lobby": if data["status"] == "ok": client_left_lobby.emit.call_deferred(data["client_name"]) "client_left_lobby": - _players_in_lobby = data["connected_client_count"] - print(_players_in_lobby) - client_left_lobby.emit.call_deferred(data["client_name"]) + _currently_connected_client_names.assign(data["connected_client_names"]) + print(_currently_connected_client_names) + client_left_lobby.emit.call_deferred(data["client_name"], _currently_connected_client_names) "receive_chat_message": received_chat_message.emit.call_deferred(data["client_name"], data["message"]) diff --git a/game.tscn b/game.tscn index 0cf3fe4..706888b 100644 --- a/game.tscn +++ b/game.tscn @@ -26,14 +26,14 @@ layout_mode = 2 color = Color(0.26, 0.2236, 0.230273, 1) script = ExtResource("2_e2o6t") client = NodePath("../../WebClientTCP") -connect_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/ConnectButton") -create_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/CreateLobbyButton") -join_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/JoinLobbyButton") -leave_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/LeaveLobbyButton") +connect_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/ConnectButton") +create_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/CreateLobbyButton") +join_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/JoinLobbyButton") +leave_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/LeaveLobbyButton") chat_message_input = NodePath("MarginContainer/VBoxContainer/ChatContainer/MessageInput") send_chat_message_button = NodePath("MarginContainer/VBoxContainer/ChatContainer/SendMessageButton") -name_input = NodePath("MarginContainer/VBoxContainer/VBoxContainer/NameInput") -lobby_code_input = NodePath("MarginContainer/VBoxContainer/VBoxContainer/LobbyCodeInput") +name_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/NameInput") +lobby_code_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/LobbyCodeInput") output = NodePath("MarginContainer/VBoxContainer/ScrollContainer/Output") [node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/ClientInterface"] @@ -59,39 +59,40 @@ bbcode_enabled = true text = "[i]Magician Online![/i]" fit_content = true -[node name="URLInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] -layout_mode = 2 -placeholder_text = "URL" - -[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] +[node name="ServerInputs" type="VBoxContainer" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] custom_minimum_size = Vector2(300, 0) layout_mode = 2 -[node name="ConnectButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="URLInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] +layout_mode = 2 +text = "127.0.0.1:9974" +placeholder_text = "URL" + +[node name="ConnectButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 text = "Connect to Server" -[node name="NameInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="NameInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 placeholder_text = "Your Name" editable = false -[node name="CreateLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="CreateLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 disabled = true text = "Create Lobby" -[node name="LobbyCodeInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="LobbyCodeInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 placeholder_text = "Lobby Code" editable = false -[node name="JoinLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="JoinLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 disabled = true text = "Join Lobby" -[node name="LeaveLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer"] +[node name="LeaveLobbyButton" type="Button" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs"] layout_mode = 2 disabled = true text = "Disconnect" @@ -152,11 +153,11 @@ text = "Start Game!" [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_chat_message" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_received_chat_message"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/ConnectButton" to="HBoxContainer/ClientInterface" method="_on_connect_button_pressed"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/CreateLobbyButton" to="HBoxContainer/ClientInterface" method="_on_create_lobby_button_pressed"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/CreateLobbyButton" to="HBoxContainer/GameUI" method="_on_create_lobby_button_pressed"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/JoinLobbyButton" to="HBoxContainer/ClientInterface" method="_on_join_lobby_button_pressed"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/JoinLobbyButton" to="HBoxContainer/GameUI" method="_on_join_lobby_button_pressed"] -[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/VBoxContainer/LeaveLobbyButton" to="HBoxContainer/ClientInterface" method="_on_disconnect_lobby_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/ConnectButton" to="HBoxContainer/ClientInterface" method="_on_connect_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/CreateLobbyButton" to="HBoxContainer/ClientInterface" method="_on_create_lobby_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/CreateLobbyButton" to="HBoxContainer/GameUI" method="_on_create_lobby_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/JoinLobbyButton" to="HBoxContainer/ClientInterface" method="_on_join_lobby_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/JoinLobbyButton" to="HBoxContainer/GameUI" method="_on_join_lobby_button_pressed"] +[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/LeaveLobbyButton" to="HBoxContainer/ClientInterface" method="_on_disconnect_lobby_button_pressed"] [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ChatContainer/SendMessageButton" to="HBoxContainer/ClientInterface" method="_on_send_message_button_pressed"] [connection signal="pressed" from="HBoxContainer/GameUI/StartGameButton" to="HBoxContainer/GameUI" method="_on_start_game_button_pressed"] diff --git a/server_tcp.py b/server_tcp.py index 054be66..aed8b66 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -80,7 +80,7 @@ class LobbyManager: "response": "client_joined_lobby", "new_client_id": client_id, "new_client_name": client_name, - "connected_client_count": lobby_manager.get_lobby_player_count(lobby_id), + "connected_client_names": self.get_lobby_client_names(lobby_id), }) ) @@ -95,7 +95,7 @@ class LobbyManager: "response": "client_left_lobby", "status": "ok", "client_name": client_name, - "connected_client_count": self.get_lobby_player_count(lobby_id), + "connected_client_names": self.get_lobby_client_names(lobby_id), }), ) print(f"Deleted client {client_id} from lobby {lobby_id}") @@ -110,6 +110,13 @@ class LobbyManager: def get_lobby_host_client_id(self, lobby_id) -> str: return self.active_lobbies[lobby_id].host_client_id + + def get_lobby_client_names(self, lobby_id) -> list[str]: + names = [] + lobby = self.get_lobby(lobby_id) + for client_id in lobby.connected_clients: + names.append(lobby.connected_clients[client_id].client_name) + return names async def send_message(self, lobby_id, client_id, message) -> None: await self.active_lobbies[lobby_id].connected_clients[client_id].websocket.send(message) @@ -200,11 +207,13 @@ async def process_input(message: str, websocket) -> None: "response": "leave_lobby_request", }) ) - if lobby_manager.get_lobby_player_count(lobby_id) == 0: - lobby_manager.delete_lobby(lobby_id) else: # client is not host; client leaves only await lobby_manager.leave_lobby(lobby_id, data_object["client_id"]) + + # Delete empty lobby + if lobby_manager.get_lobby_player_count(lobby_id) == 0: + lobby_manager.delete_lobby(lobby_id) elif data_object["response"] == "send_chat_message": print("relaying chat message")