server: added broadcast for players entering the lobby. client: added text output for connection actions

This commit is contained in:
2025-06-30 22:07:43 +02:00
parent 07a37a5602
commit d128f39e6a
4 changed files with 31 additions and 3 deletions
+13 -2
View File
@@ -11,6 +11,11 @@ extends Control
@export var name_input: LineEdit @export var name_input: LineEdit
@export var lobby_code_input: LineEdit @export var lobby_code_input: LineEdit
@export var output: Label
func _write_line(text: String) -> void:
output.text += text + "\n"
func _on_create_lobby_button_pressed() -> void: func _on_create_lobby_button_pressed() -> void:
client.create_lobby(name_input.text.strip_edges()) client.create_lobby(name_input.text.strip_edges())
@@ -55,9 +60,15 @@ func _on_web_client_tcp_disconnected_from_server() -> void:
lobby_code_input.editable = false lobby_code_input.editable = false
lobby_code_input.text = "" lobby_code_input.text = ""
func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void: func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void:
lobby_code_input.text = lobby_id lobby_code_input.text = lobby_id
_write_line("you connected to lobby {lobby_id}".format({"lobby_id": lobby_id}))
func _on_web_client_tcp_disconnected_from_lobby() -> void: func _on_web_client_tcp_disconnected_from_lobby() -> void:
pass # Replace with function body. _write_line("you disconnected from the lobby")
func _on_web_client_tcp_client_joined_lobby(client_name: String) -> 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 ""
}))
+6
View File
@@ -22,6 +22,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)
func _process(delta: float) -> void: func _process(delta: float) -> void:
_tcp.poll() _tcp.poll()
_new_status = _tcp.get_status() _new_status = _tcp.get_status()
@@ -60,6 +62,8 @@ func _handle_response(response: String) -> void:
"join_lobby": "join_lobby":
if data["status"] == "ok": if data["status"] == "ok":
connected_to_lobby.emit.call_deferred(_lobby_id) connected_to_lobby.emit.call_deferred(_lobby_id)
"client_joined_lobby":
client_joined_lobby.emit.call_deferred(data["new_client_name"])
func _exit_tree() -> void: func _exit_tree() -> void:
disconnect_from_server() disconnect_from_server()
@@ -97,6 +101,8 @@ func leave_lobby() -> void:
_client_id = "" _client_id = ""
_client_name = "" _client_name = ""
disconnected_from_lobby.emit()
func _send_message(message: String) -> void: func _send_message(message: String) -> void:
_tcp.put_data(message.to_utf8_buffer()) _tcp.put_data(message.to_utf8_buffer())
+3 -1
View File
@@ -12,7 +12,7 @@ font_weight = 500
[node name="WebClientTCP" type="Node" parent="."] [node name="WebClientTCP" type="Node" parent="."]
script = ExtResource("1_feb5d") script = ExtResource("1_feb5d")
[node name="ClientInterface" type="Control" parent="." node_paths=PackedStringArray("client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "name_input", "lobby_code_input")] [node name="ClientInterface" type="Control" parent="." node_paths=PackedStringArray("client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "name_input", "lobby_code_input", "output")]
layout_mode = 3 layout_mode = 3
anchors_preset = 15 anchors_preset = 15
anchor_right = 1.0 anchor_right = 1.0
@@ -27,6 +27,7 @@ join_lobby_button = NodePath("HBoxContainer/VBoxContainer/JoinLobbyButton")
leave_lobby_button = NodePath("HBoxContainer/VBoxContainer/LeaveLobbyButton") leave_lobby_button = NodePath("HBoxContainer/VBoxContainer/LeaveLobbyButton")
name_input = NodePath("HBoxContainer/VBoxContainer/NameInput") name_input = NodePath("HBoxContainer/VBoxContainer/NameInput")
lobby_code_input = NodePath("HBoxContainer/VBoxContainer/LobbyCodeInput") lobby_code_input = NodePath("HBoxContainer/VBoxContainer/LobbyCodeInput")
output = NodePath("HBoxContainer/Output")
[node name="ColorRect" type="ColorRect" parent="ClientInterface"] [node name="ColorRect" type="ColorRect" parent="ClientInterface"]
layout_mode = 1 layout_mode = 1
@@ -83,6 +84,7 @@ size_flags_vertical = 1
theme_override_fonts/font = SubResource("SystemFont_80nbo") theme_override_fonts/font = SubResource("SystemFont_80nbo")
autowrap_mode = 2 autowrap_mode = 2
[connection signal="client_joined_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_client_joined_lobby"]
[connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"]
[connection signal="connected_to_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connected_to_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_server"]
[connection signal="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] [connection signal="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"]
+9
View File
@@ -64,6 +64,15 @@ class LobbyManager:
) )
print(f"Client {client_id} joined lobby {lobby_id}") print(f"Client {client_id} joined lobby {lobby_id}")
self.broadcast_message(
lobby_id,
json.dumps({
"response": "client_joined_lobby",
"new_client_id": client_id,
"new_client_name": client_name,
})
)
def leave_lobby(self, lobby_id, client_id) -> None: def leave_lobby(self, lobby_id, client_id) -> None:
try: try:
del self.active_lobbies[lobby_id].connected_clients[client_id] del self.active_lobbies[lobby_id].connected_clients[client_id]