server: fixed bug that caused host client to disconnect when leaving lobby if they were the only connected client in the lobby

This commit is contained in:
2025-07-04 14:24:28 +02:00
parent b468a391f5
commit af763c89b3
6 changed files with 26 additions and 37 deletions
+5 -7
View File
@@ -115,8 +115,6 @@ class LobbyManager:
"client_name": client_name,
}),
)
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")
@@ -150,7 +148,7 @@ class ClientConnection:
# Listens to a connected TCP client.
def listen_client(self) -> None:
try:
print(f"connected to {self.address[0]}:{self.address[1]}")
print(f"Connected to {self.address[0]}:{self.address[1]}")
received_data = ""
while True:
@@ -165,6 +163,8 @@ class ClientConnection:
break
except IndexError:
print("Client force-disconnected")
except ConnectionResetError:
print("Client force-disconnected")
finally:
self.connection.close()
print(f"Closing connection to {self.address[0]}:{self.address[1]}")
@@ -230,8 +230,6 @@ 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, data_object["client_id"])
self.lobby_manager.broadcast_message(
lobby_id,
@@ -239,8 +237,8 @@ class ClientConnection:
"response": "leave_lobby_request",
})
)
# self.lobby_manager.leave_lobby(lobby_id, client_id)
# self.lobby_manager.delete_lobby(lobby_id)
if self.lobby_manager.get_lobby_player_count(lobby_id) == 0:
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"])