server: moved code generation from client to server to avoid code collision
This commit is contained in:
+50
-12
@@ -1,10 +1,12 @@
|
||||
import json
|
||||
import random
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
|
||||
# Establishes connections to TCP clients.
|
||||
def listen() -> None:
|
||||
print(generate_code())
|
||||
lobby_manager = LobbyManager()
|
||||
threads = []
|
||||
while True:
|
||||
@@ -21,6 +23,13 @@ def listen() -> None:
|
||||
def start_listen_client(connection, address, lobby_manager):
|
||||
ClientConnection(connection, address, lobby_manager)
|
||||
|
||||
def generate_code() -> str:
|
||||
result = ""
|
||||
for i in range(6):
|
||||
result += chr(random.randint(0, 25) + 65)
|
||||
return result
|
||||
|
||||
|
||||
class Lobby:
|
||||
|
||||
def __init__(self, lobby_id, host_client_id):
|
||||
@@ -44,6 +53,26 @@ class LobbyManager:
|
||||
def get_lobby(self, lobby_id) -> Lobby:
|
||||
return self.active_lobbies[lobby_id]
|
||||
|
||||
def lobby_id_exists(self, lobby_id) -> bool:
|
||||
return lobby_id in self.active_lobbies
|
||||
|
||||
def client_id_exists_in_lobby(self, lobby_id, client_id) -> bool:
|
||||
if not self.lobby_id_exists(lobby_id):
|
||||
return False
|
||||
return client_id in self.active_lobbies[lobby_id].connected_clients
|
||||
|
||||
def get_unique_lobby_id(self) -> str:
|
||||
id = generate_code()
|
||||
if self.lobby_id_exists(id):
|
||||
return self.get_unique_lobby_id()
|
||||
return id
|
||||
|
||||
def get_unique_client_id(self, lobby_id) -> str:
|
||||
id = generate_code()
|
||||
if self.client_id_exists_in_lobby(lobby_id, id):
|
||||
return self.get_unique_client_id(lobby_id)
|
||||
return id
|
||||
|
||||
def create_lobby(self, lobby_id, host_client_id) -> None:
|
||||
self.active_lobbies[lobby_id] = Lobby(lobby_id, host_client_id)
|
||||
print(f"Created lobby {lobby_id}")
|
||||
@@ -122,8 +151,8 @@ class ClientConnection:
|
||||
while True:
|
||||
data = self.connection.recv(1)
|
||||
received_data += data.decode("utf-8")
|
||||
print(received_data)
|
||||
if received_data[-1] == "}":
|
||||
print(received_data)
|
||||
self.process_input(received_data)
|
||||
received_data = ""
|
||||
|
||||
@@ -139,42 +168,51 @@ class ClientConnection:
|
||||
data_object = json.loads(data)
|
||||
|
||||
if data_object["response"] == "create_lobby":
|
||||
print(data_object["client_id"] + " is creating lobby " + data_object["lobby_id"])
|
||||
new_lobby_id = self.lobby_manager.get_unique_lobby_id()
|
||||
new_client_id = self.lobby_manager.get_unique_client_id(new_lobby_id)
|
||||
print(new_client_id + " is creating lobby " + new_lobby_id)
|
||||
|
||||
self.lobby_manager.create_lobby(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
new_lobby_id,
|
||||
new_client_id,
|
||||
)
|
||||
self.lobby_manager.join_lobby(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
new_lobby_id,
|
||||
new_client_id,
|
||||
data_object["client_name"],
|
||||
self.address,
|
||||
self.connection,
|
||||
)
|
||||
self.lobby_manager.send_message(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
new_lobby_id,
|
||||
new_client_id,
|
||||
json.dumps({
|
||||
"response": "create_lobby",
|
||||
"status": "ok",
|
||||
"lobby_id": new_lobby_id,
|
||||
"client_id": new_client_id,
|
||||
}),
|
||||
)
|
||||
|
||||
elif data_object["response"] == "join_lobby":
|
||||
print("joining lobby")
|
||||
lobby_id = data_object["lobby_id"]
|
||||
new_client_id = self.lobby_manager.get_unique_client_id(lobby_id)
|
||||
|
||||
self.lobby_manager.join_lobby(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
lobby_id,
|
||||
new_client_id,
|
||||
data_object["client_name"],
|
||||
self.address,
|
||||
self.connection,
|
||||
)
|
||||
self.lobby_manager.send_message(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
lobby_id,
|
||||
new_client_id,
|
||||
json.dumps({
|
||||
"response": "join_lobby",
|
||||
"status": "ok",
|
||||
"client_id": new_client_id,
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user