cleaned up code

This commit is contained in:
steev 2024-01-14 18:41:45 +01:00
parent 15cce52aea
commit 96df962227
2 changed files with 28 additions and 4 deletions

View File

@ -87,13 +87,17 @@ class GameManager:
# creates a player and handles counting all players and if conditions met starting the game
# returns the new dict in which the new player now is added
def addPlayers(self, player:Player, socket:socket, clientAddr) -> dict:
self.__gameManager.getLogger().info(f"creating user with id: {player.getID}")
self.__players[clientAddr] = {
player: player,
socket:socket
}
self.__gameManager.getLogger().info(f"new length of user dictionary: {len(self.__players)}")
# counts participating players and starts the game if enough have joined
if len(self.__players) == 2:
self.__gameManager.getLogger().info("2 players have join game starts")
self.startGame(socket)
return self.__players

View File

@ -46,13 +46,16 @@ class NetworkManager:
self.__gameManager.getLogger().info(f"Connected with {client_address}")
self.__gameManager.getPlayers()[client_address] = client_tcp_socket
self.__eventHandler[client_address] = TCPEventHandler(client_tcp_socket)
client_handler_thread = threading.Thread(
target=self.receive,
args=(client_tcp_socket, client_address)
)
self.__gameManager.getLogger().info(f"starting client handler thread for client at address {client_address}")
client_handler_thread.daemon = True
client_handler_thread.start()
except Exception as e:
self.__gameManager.getLogger().error(f"tcp socket failed to accept connection due to error: {e}")
pass
@ -66,9 +69,16 @@ class NetworkManager:
self.__gameManager.getLogger().info(f"Connection with {client_address} closed.")
break
try:
message = data.decode()
messageJson = json.loads(message)
self.__gameManager.getLogger().info(f"decoded message {messageJson}")
user = messageJson.get("user")
self.__gameManager.getLogger().info(f"user in message {user}")
except Exception as ex:
self.__gameManager.getLogger().info(f"decoding incoming packet failed due to exception: {ex}")
# creates a user and counts how many currently are connected to the server
# if enough users for a round are connected the server has to start the game
@ -76,19 +86,29 @@ class NetworkManager:
if messageJson["event"] == "login":
self.logger.__gameManager.getLogger().info("user logging in")
self.logger.__gameManager.getLogger().info("task passed off to gameManager")
self.__gameManager.addPlayers(Player(messageJson["username"], messageJson["deck"]), client_socket, client_address)
user = self.__gameManager.addPlayers(Player(messageJson["username"], messageJson["deck"]), client_socket, client_address)
self.__gameManager.getLogger().info(f"connected users {len(self.__gameManager.getPlayers())}")
self.__gameManager.getLogger().info(f"confirming login for user")
self.send({
"event":"loginresponse",
"id": user["player"].getID(),
})
self.__eventHandler[client_address].handleTCPEvents(messageJson, self.__gameManager, client_address)
self.__gameManager.getLogger().info(f"Received message from {client_address}: {message}")
except socket.error as e:
if e.errno == 10054:
self.__gameManager.getLogger().error(f"Connection with {client_address} forcibly closed by remote host.")
break
self.__gameManager.getLogger().error(f"Socket error receiving data from {client_address}: {e}")
except json.JSONDecodeError as e:
self.__gameManager.getLogger().error(f"JSON decoding error receiving data from {client_address}: {e}")
except Exception as e:
self.__gameManager.getLogger().error(f"Error receiving data from {client_address}: {e}")