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