client & server: added error handling for lobby connection issues

This commit is contained in:
2025-07-07 10:57:39 +02:00
parent dffda0d4fe
commit f51444874a
4 changed files with 76 additions and 22 deletions
+54 -17
View File
@@ -6,6 +6,8 @@ import ssl
import asyncio
from websockets.asyncio.server import serve
MAX_LOBBY_CLIENT_COUNT = 6
# Generates a 6-character code from the letters A-Z
def generate_code() -> str:
result = ""
@@ -42,7 +44,15 @@ class LobbyManager:
print(f"Created lobby {lobby_id}")
# Joins a client to an existing lobby
async def join_lobby(self, lobby_id, client_id, client_name, websocket) -> None:
async def join_lobby(self, lobby_id, client_id, client_name, websocket) -> str:
if not self.lobby_id_exists(lobby_id):
# Cannot join; lobby does not exist
return "lobby_not_found"
if self.get_lobby_player_count(lobby_id) >= MAX_LOBBY_CLIENT_COUNT:
# Lobby full! Cannot join
return "lobby_full"
# Add client to lobby
self.active_lobbies[lobby_id].connected_clients[client_id] = Client(
client_id,
@@ -61,6 +71,8 @@ class LobbyManager:
"connected_client_names": self.get_lobby_client_names(lobby_id),
})
)
return "ok"
# Removes a client from a lobby
async def leave_lobby(self, lobby_id, client_id) -> None:
@@ -201,7 +213,9 @@ async def process_input(message: str, websocket) -> None:
new_client_id,
)
# Join requesting client to new lobby
# Join requesting client to new lobby.
# No error handling here, since lobby was just created, so it
# must exist and be empty
await lobby_manager.join_lobby(
new_lobby_id,
new_client_id,
@@ -226,28 +240,51 @@ async def process_input(message: str, websocket) -> None:
# Join client to existing lobby
elif data_object["response"] == "join_lobby":
lobby_id = data_object["lobby_id"]
new_client_id = lobby_manager.get_unique_client_id(lobby_id)
client_id = lobby_manager.get_unique_client_id(lobby_id)
client_name = data_object["client_name"]
# Join client to lobby
await lobby_manager.join_lobby(
response = await lobby_manager.join_lobby(
lobby_id,
new_client_id,
data_object["client_name"],
client_id,
client_name,
websocket,
)
# Send success confirmation
await lobby_manager.send_message(
lobby_id,
new_client_id,
json.dumps({
"response": "join_lobby",
"status": "ok",
"client_id": new_client_id,
}),
)
match response:
case "lobby_not_found":
await websocket.send(
json.dumps({
"response": "join_lobby_response",
"status": "lobby_not_found",
})
)
print(f"Client {client_name} could not connect; lobby {lobby_id} does not exist")
case "lobby_full":
await websocket.send(
json.dumps({
"response": "join_lobby_response",
"status": "lobby_full",
})
)
print(f"Client {new_client_id} joined lobby {lobby_id}")
print(f"Client {client_name} could not connect; lobby {lobby_id} is full")
case "ok":
# Send success confirmation
await lobby_manager.send_message(
lobby_id,
client_id,
json.dumps({
"response": "join_lobby_response",
"status": "ok",
"client_id": client_id,
}),
)
print(f"Client {client_id} joined lobby {lobby_id}")
# Disconnect client from lobby
elif data_object["response"] == "leave_lobby":