server: sending connected client names instead of count on every (dis)connect
This commit is contained in:
+13
-4
@@ -80,7 +80,7 @@ class LobbyManager:
|
||||
"response": "client_joined_lobby",
|
||||
"new_client_id": client_id,
|
||||
"new_client_name": client_name,
|
||||
"connected_client_count": lobby_manager.get_lobby_player_count(lobby_id),
|
||||
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -95,7 +95,7 @@ class LobbyManager:
|
||||
"response": "client_left_lobby",
|
||||
"status": "ok",
|
||||
"client_name": client_name,
|
||||
"connected_client_count": self.get_lobby_player_count(lobby_id),
|
||||
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
||||
}),
|
||||
)
|
||||
print(f"Deleted client {client_id} from lobby {lobby_id}")
|
||||
@@ -110,6 +110,13 @@ class LobbyManager:
|
||||
|
||||
def get_lobby_host_client_id(self, lobby_id) -> str:
|
||||
return self.active_lobbies[lobby_id].host_client_id
|
||||
|
||||
def get_lobby_client_names(self, lobby_id) -> list[str]:
|
||||
names = []
|
||||
lobby = self.get_lobby(lobby_id)
|
||||
for client_id in lobby.connected_clients:
|
||||
names.append(lobby.connected_clients[client_id].client_name)
|
||||
return names
|
||||
|
||||
async def send_message(self, lobby_id, client_id, message) -> None:
|
||||
await self.active_lobbies[lobby_id].connected_clients[client_id].websocket.send(message)
|
||||
@@ -200,11 +207,13 @@ async def process_input(message: str, websocket) -> None:
|
||||
"response": "leave_lobby_request",
|
||||
})
|
||||
)
|
||||
if lobby_manager.get_lobby_player_count(lobby_id) == 0:
|
||||
lobby_manager.delete_lobby(lobby_id)
|
||||
else:
|
||||
# client is not host; client leaves only
|
||||
await lobby_manager.leave_lobby(lobby_id, data_object["client_id"])
|
||||
|
||||
# Delete empty lobby
|
||||
if lobby_manager.get_lobby_player_count(lobby_id) == 0:
|
||||
lobby_manager.delete_lobby(lobby_id)
|
||||
|
||||
elif data_object["response"] == "send_chat_message":
|
||||
print("relaying chat message")
|
||||
|
||||
Reference in New Issue
Block a user