client: now displays when clients leave the lobby

This commit is contained in:
2025-07-04 11:41:57 +02:00
parent 4b4285ca99
commit 2aa6d710cf
4 changed files with 17 additions and 6 deletions
+3
View File
@@ -88,6 +88,9 @@ 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:
_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:
match new_status:
+5 -3
View File
@@ -23,6 +23,7 @@ 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 received_chat_message(client_name: String, message: String)
@@ -36,7 +37,7 @@ enum ConnectionStatus {
JOINED,
}
func _process(delta: float) -> void:
func _process(_delta: float) -> void:
_tcp.poll()
_new_status = _tcp.get_status()
# Ensure status change is only processed once
@@ -60,7 +61,6 @@ func _process(delta: float) -> void:
func _listen_for_data() -> void:
while not _stop_thread and not _tcp.get_status() == StreamPeerTCP.Status.STATUS_ERROR:
#received_data += String.chr(_tcp.get_data(1)[0])
received_data += _tcp.get_utf8_string(1)
print(received_data)
if received_data.ends_with("}"):
@@ -87,7 +87,9 @@ func _handle_response(response: String) -> void:
leave_lobby()
"leave_lobby":
if data["status"] == "ok":
print(data["client_id"] + " left")
client_left_lobby.emit.call_deferred(data["client_name"])
"client_left_lobby":
client_left_lobby.emit.call_deferred(data["client_name"])
"receive_chat_message":
received_chat_message.emit.call_deferred(data["client_name"], data["message"])
+1
View File
@@ -113,6 +113,7 @@ layout_mode = 2
text = "Send"
[connection signal="client_joined_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_client_joined_lobby"]
[connection signal="client_left_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_client_left_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="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"]
+8 -3
View File
@@ -104,13 +104,15 @@ class LobbyManager:
def leave_lobby(self, lobby_id, client_id) -> None:
try:
# get client name BEFORE deleting the client...
client_name = self.get_client_name(lobby_id, client_id)
del self.active_lobbies[lobby_id].connected_clients[client_id]
self.broadcast_message(
lobby_id,
json.dumps({
"response": "leave_lobby",
"response": "client_left_lobby",
"status": "ok",
"client_id": client_id,
"client_name": client_name,
}),
)
if self.get_lobby_player_count(lobby_id) == 0:
@@ -122,6 +124,9 @@ class LobbyManager:
def get_lobby_player_count(self, lobby_id) -> int:
return len(self.active_lobbies[lobby_id].connected_clients)
def get_client_name(self, lobby_id, client_id) -> str:
return self.active_lobbies[lobby_id].connected_clients[client_id].client_name
def get_lobby_host_client_id(self, lobby_id) -> str:
return self.active_lobbies[lobby_id].host_client_id
@@ -247,7 +252,7 @@ class ClientConnection:
json.dumps({
"response": "receive_chat_message",
"message": data_object["message"],
"client_name": self.lobby_manager.active_lobbies[data_object["lobby_id"]].connected_clients[data_object["client_id"]].client_name,
"client_name": self.lobby_manager.get_client_name(data_object["lobby_id"], data_object["client_id"]),
})
)