server: sending connected client names instead of count on every (dis)connect

This commit is contained in:
2025-07-07 09:58:34 +02:00
parent 63869dc530
commit a2bab58d3f
5 changed files with 58 additions and 47 deletions
+9 -8
View File
@@ -235,6 +235,14 @@ card_value = 3
color = 2 color = 2
metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" 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"] [sub_resource type="Resource" id="Resource_rcngd"]
script = ExtResource("2_vmpa3") script = ExtResource("2_vmpa3")
card_id = 30 card_id = 30
@@ -475,15 +483,8 @@ card_value = 1
color = 4 color = 4
metadata/_custom_type_script = "uid://dx4cl2rk0a8a0" 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"] [node name="CardManager" type="Node"]
editor_description = "desc"
script = ExtResource("1_de166") script = ExtResource("1_de166")
full_deck = Dictionary[int, ExtResource("2_vmpa3")]({ full_deck = Dictionary[int, ExtResource("2_vmpa3")]({
0: SubResource("Resource_hq606"), 0: SubResource("Resource_hq606"),
+3 -3
View File
@@ -23,7 +23,7 @@ func _on_create_lobby_button_pressed() -> void:
client.create_lobby(name_input.text.strip_edges()) client.create_lobby(name_input.text.strip_edges())
func _on_connect_button_pressed() -> void: 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: func _on_join_lobby_button_pressed() -> void:
client.join_lobby(lobby_code_input.text.strip_edges(), name_input.text.strip_edges()) 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: func _on_web_client_tcp_disconnected_from_lobby() -> void:
_write_line("You disconnected from the lobby") _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({ _write_line("{client_name} joined the lobby{you}".format({
"client_name": client_name, "client_name": client_name,
"you": " (that's you!)" if client_name == name_input.text.strip_edges() else "" "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, "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})) _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: func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void:
+9 -9
View File
@@ -17,7 +17,7 @@ var cached_response: String
var _thread: Thread var _thread: Thread
var _stop_thread: bool = false var _stop_thread: bool = false
var _players_in_lobby: int = 0 var _currently_connected_client_names: Array[String] = []
signal connected_to_server() signal connected_to_server()
signal connection_error_occurred() signal connection_error_occurred()
@@ -26,8 +26,8 @@ signal disconnected_from_server()
signal connected_to_lobby(lobby_id: String) signal connected_to_lobby(lobby_id: String)
signal disconnected_from_lobby() signal disconnected_from_lobby()
signal client_joined_lobby(client_name: String) signal client_joined_lobby(client_name: String, currently_connected_client_names: Array[String])
signal client_left_lobby(client_name: String) signal client_left_lobby(client_name: String, currently_connected_client_names: Array[String])
signal received_chat_message(client_name: String, message: 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) connected_to_lobby.emit.call_deferred(_lobby_id)
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED) connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
"client_joined_lobby": "client_joined_lobby":
client_joined_lobby.emit.call_deferred(data["new_client_name"]) _currently_connected_client_names.assign(data["connected_client_names"])
_players_in_lobby = data["connected_client_count"] client_joined_lobby.emit.call_deferred(data["new_client_name"], _currently_connected_client_names)
print(_players_in_lobby) print(_currently_connected_client_names)
"leave_lobby_request": "leave_lobby_request":
leave_lobby() leave_lobby()
"leave_lobby": "leave_lobby":
if data["status"] == "ok": if data["status"] == "ok":
client_left_lobby.emit.call_deferred(data["client_name"]) client_left_lobby.emit.call_deferred(data["client_name"])
"client_left_lobby": "client_left_lobby":
_players_in_lobby = data["connected_client_count"] _currently_connected_client_names.assign(data["connected_client_names"])
print(_players_in_lobby) print(_currently_connected_client_names)
client_left_lobby.emit.call_deferred(data["client_name"]) client_left_lobby.emit.call_deferred(data["client_name"], _currently_connected_client_names)
"receive_chat_message": "receive_chat_message":
received_chat_message.emit.call_deferred(data["client_name"], data["message"]) received_chat_message.emit.call_deferred(data["client_name"], data["message"])
+24 -23
View File
@@ -26,14 +26,14 @@ layout_mode = 2
color = Color(0.26, 0.2236, 0.230273, 1) color = Color(0.26, 0.2236, 0.230273, 1)
script = ExtResource("2_e2o6t") script = ExtResource("2_e2o6t")
client = NodePath("../../WebClientTCP") client = NodePath("../../WebClientTCP")
connect_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/ConnectButton") connect_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/ConnectButton")
create_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/CreateLobbyButton") create_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/CreateLobbyButton")
join_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/JoinLobbyButton") join_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/JoinLobbyButton")
leave_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/LeaveLobbyButton") leave_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/LeaveLobbyButton")
chat_message_input = NodePath("MarginContainer/VBoxContainer/ChatContainer/MessageInput") chat_message_input = NodePath("MarginContainer/VBoxContainer/ChatContainer/MessageInput")
send_chat_message_button = NodePath("MarginContainer/VBoxContainer/ChatContainer/SendMessageButton") send_chat_message_button = NodePath("MarginContainer/VBoxContainer/ChatContainer/SendMessageButton")
name_input = NodePath("MarginContainer/VBoxContainer/VBoxContainer/NameInput") name_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/NameInput")
lobby_code_input = NodePath("MarginContainer/VBoxContainer/VBoxContainer/LobbyCodeInput") lobby_code_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/LobbyCodeInput")
output = NodePath("MarginContainer/VBoxContainer/ScrollContainer/Output") output = NodePath("MarginContainer/VBoxContainer/ScrollContainer/Output")
[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/ClientInterface"] [node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/ClientInterface"]
@@ -59,39 +59,40 @@ bbcode_enabled = true
text = "[i]Magician Online![/i]" text = "[i]Magician Online![/i]"
fit_content = true fit_content = true
[node name="URLInput" type="LineEdit" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] [node name="ServerInputs" type="VBoxContainer" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"]
layout_mode = 2
placeholder_text = "URL"
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"]
custom_minimum_size = Vector2(300, 0) custom_minimum_size = Vector2(300, 0)
layout_mode = 2 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 layout_mode = 2
text = "Connect to Server" 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 layout_mode = 2
placeholder_text = "Your Name" placeholder_text = "Your Name"
editable = false 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 layout_mode = 2
disabled = true disabled = true
text = "Create Lobby" 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 layout_mode = 2
placeholder_text = "Lobby Code" placeholder_text = "Lobby Code"
editable = false 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 layout_mode = 2
disabled = true disabled = true
text = "Join Lobby" 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 layout_mode = 2
disabled = true disabled = true
text = "Disconnect" 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_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="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="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/ServerInputs/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/ServerInputs/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/ServerInputs/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/ServerInputs/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/ServerInputs/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/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/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"] [connection signal="pressed" from="HBoxContainer/GameUI/StartGameButton" to="HBoxContainer/GameUI" method="_on_start_game_button_pressed"]
+13 -4
View File
@@ -80,7 +80,7 @@ class LobbyManager:
"response": "client_joined_lobby", "response": "client_joined_lobby",
"new_client_id": client_id, "new_client_id": client_id,
"new_client_name": client_name, "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", "response": "client_left_lobby",
"status": "ok", "status": "ok",
"client_name": client_name, "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}") print(f"Deleted client {client_id} from lobby {lobby_id}")
@@ -111,6 +111,13 @@ class LobbyManager:
def get_lobby_host_client_id(self, lobby_id) -> str: def get_lobby_host_client_id(self, lobby_id) -> str:
return self.active_lobbies[lobby_id].host_client_id 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: async def send_message(self, lobby_id, client_id, message) -> None:
await self.active_lobbies[lobby_id].connected_clients[client_id].websocket.send(message) await self.active_lobbies[lobby_id].connected_clients[client_id].websocket.send(message)
@@ -200,12 +207,14 @@ async def process_input(message: str, websocket) -> None:
"response": "leave_lobby_request", "response": "leave_lobby_request",
}) })
) )
if lobby_manager.get_lobby_player_count(lobby_id) == 0:
lobby_manager.delete_lobby(lobby_id)
else: else:
# client is not host; client leaves only # client is not host; client leaves only
await lobby_manager.leave_lobby(lobby_id, data_object["client_id"]) 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": elif data_object["response"] == "send_chat_message":
print("relaying chat message") print("relaying chat message")
await lobby_manager.broadcast_message( await lobby_manager.broadcast_message(