server: edited lobby leaving logic slightly
This commit is contained in:
@@ -57,7 +57,7 @@ func _process(delta: float) -> void:
|
||||
|
||||
|
||||
func _listen_for_data() -> void:
|
||||
while not _stop_thread:
|
||||
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)
|
||||
@@ -78,6 +78,13 @@ func _handle_response(response: String) -> void:
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"client_joined_lobby":
|
||||
client_joined_lobby.emit.call_deferred(data["new_client_name"])
|
||||
"leave_lobby_request":
|
||||
leave_lobby()
|
||||
"leave_lobby":
|
||||
if data["status"] == "ok":
|
||||
print(data["client_id"] + " left")
|
||||
#if data["client_id"] == _client_id:
|
||||
#connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
disconnect_from_server()
|
||||
|
||||
+22
-3
@@ -76,10 +76,23 @@ class LobbyManager:
|
||||
def leave_lobby(self, lobby_id, client_id) -> None:
|
||||
try:
|
||||
del self.active_lobbies[lobby_id].connected_clients[client_id]
|
||||
self.broadcast_message(
|
||||
lobby_id,
|
||||
json.dumps({
|
||||
"response": "leave_lobby",
|
||||
"status": "ok",
|
||||
"client_id": client_id,
|
||||
}),
|
||||
)
|
||||
if self.get_lobby_player_count(lobby_id) == 0:
|
||||
self.delete_lobby(lobby_id)
|
||||
print(f"Deleted client {client_id} from lobby {lobby_id}")
|
||||
except KeyError:
|
||||
print(f"Client with ID {client_id} in lobby {lobby_id} not found, could not be deleted")
|
||||
|
||||
def get_lobby_player_count(self, lobby_id) -> int:
|
||||
return len(self.active_lobbies[lobby_id].connected_clients)
|
||||
|
||||
def get_lobby_host_client_id(self, lobby_id) -> str:
|
||||
return self.active_lobbies[lobby_id].host_client_id
|
||||
|
||||
@@ -175,9 +188,15 @@ class ClientConnection:
|
||||
if host_client_id == data_object["client_id"]:
|
||||
# client is host; close lobby
|
||||
lobby_clients = self.lobby_manager.get_lobby(lobby_id).connected_clients.copy()
|
||||
for client_id in lobby_clients:
|
||||
self.lobby_manager.leave_lobby(lobby_id, client_id)
|
||||
self.lobby_manager.delete_lobby(lobby_id)
|
||||
# for client_id in lobby_clients:
|
||||
self.lobby_manager.broadcast_message(
|
||||
lobby_id,
|
||||
json.dumps({
|
||||
"response": "leave_lobby_request",
|
||||
})
|
||||
)
|
||||
# self.lobby_manager.leave_lobby(lobby_id, client_id)
|
||||
# self.lobby_manager.delete_lobby(lobby_id)
|
||||
else:
|
||||
# client is not host; client leaves only
|
||||
self.lobby_manager.leave_lobby(lobby_id, data_object["client_id"])
|
||||
|
||||
Reference in New Issue
Block a user