server: moved send_message to LobbyManager and added broadcast_message
This commit is contained in:
+36
-14
@@ -30,10 +30,11 @@ class Lobby:
|
||||
|
||||
class Client:
|
||||
|
||||
def __init__(self, client_id, client_name, address):
|
||||
def __init__(self, client_id, client_name, address, connection):
|
||||
self.client_id = client_id
|
||||
self.client_name = client_name
|
||||
self.address = address
|
||||
self.connection = connection
|
||||
|
||||
class LobbyManager:
|
||||
|
||||
@@ -54,8 +55,13 @@ class LobbyManager:
|
||||
except KeyError:
|
||||
print(f"Lobby with ID {lobby_id} not found, could not be deleted")
|
||||
|
||||
def join_lobby(self, lobby_id, client_id, client_name, address) -> None:
|
||||
self.active_lobbies[lobby_id].connected_clients[client_id] = Client(client_id, client_name, address)
|
||||
def join_lobby(self, lobby_id, client_id, client_name, address, connection) -> None:
|
||||
self.active_lobbies[lobby_id].connected_clients[client_id] = Client(
|
||||
client_id,
|
||||
client_name,
|
||||
address,
|
||||
connection,
|
||||
)
|
||||
print(f"Client {client_id} joined lobby {lobby_id}")
|
||||
|
||||
def leave_lobby(self, lobby_id, client_id) -> None:
|
||||
@@ -68,12 +74,21 @@ class LobbyManager:
|
||||
def get_lobby_host_client_id(self, lobby_id) -> str:
|
||||
return self.active_lobbies[lobby_id].host_client_id
|
||||
|
||||
def send_message(self, lobby_id, client_id, message) -> None:
|
||||
self.active_lobbies[lobby_id].connected_clients[client_id].connection.sendall(str.encode(message))
|
||||
|
||||
def broadcast_message(self, lobby_id, message) -> None:
|
||||
connected_clients = self.active_lobbies[lobby_id].connected_clients
|
||||
for client_id in connected_clients:
|
||||
connected_clients[client_id].connection.sendall(str.encode(message))
|
||||
|
||||
class ClientConnection:
|
||||
|
||||
def __init__(self, connection, address, lobby_manager):
|
||||
self.connection = connection
|
||||
self.address = address
|
||||
self.lobby_manager = lobby_manager
|
||||
|
||||
self.listen_client()
|
||||
|
||||
# Listens to a connected TCP client.
|
||||
@@ -112,11 +127,16 @@ class ClientConnection:
|
||||
data_object["client_id"],
|
||||
data_object["client_name"],
|
||||
self.address,
|
||||
self.connection,
|
||||
)
|
||||
self.lobby_manager.send_message(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
json.dumps({
|
||||
"response": "create_lobby",
|
||||
"status": "ok",
|
||||
}),
|
||||
)
|
||||
self.send_message(json.dumps({
|
||||
"response": "create_lobby",
|
||||
"status": "ok",
|
||||
}))
|
||||
|
||||
elif data_object["response"] == "join_lobby":
|
||||
print("joining lobby")
|
||||
@@ -125,11 +145,16 @@ class ClientConnection:
|
||||
data_object["client_id"],
|
||||
data_object["client_name"],
|
||||
self.address,
|
||||
self.connection,
|
||||
)
|
||||
self.lobby_manager.send_message(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
json.dumps({
|
||||
"response": "join_lobby",
|
||||
"status": "ok",
|
||||
}),
|
||||
)
|
||||
self.send_message(json.dumps({
|
||||
"response": "join_lobby",
|
||||
"status": "ok",
|
||||
}))
|
||||
|
||||
# send message to all connected clients
|
||||
|
||||
@@ -148,9 +173,6 @@ class ClientConnection:
|
||||
# client is not host; client leaves only
|
||||
self.lobby_manager.leave_lobby(lobby_id, data_object["client_id"])
|
||||
|
||||
def send_message(self, message: str) -> None:
|
||||
self.connection.sendall(str.encode(message))
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: ./server.py PORT")
|
||||
|
||||
Reference in New Issue
Block a user