cleaned up code
This commit is contained in:
parent
15cce52aea
commit
96df962227
@ -87,13 +87,17 @@ class GameManager:
|
|||||||
# creates a player and handles counting all players and if conditions met starting the game
|
# 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
|
# returns the new dict in which the new player now is added
|
||||||
def addPlayers(self, player:Player, socket:socket, clientAddr) -> dict:
|
def addPlayers(self, player:Player, socket:socket, clientAddr) -> dict:
|
||||||
|
|
||||||
|
self.__gameManager.getLogger().info(f"creating user with id: {player.getID}")
|
||||||
self.__players[clientAddr] = {
|
self.__players[clientAddr] = {
|
||||||
player: player,
|
player: player,
|
||||||
socket:socket
|
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
|
# counts participating players and starts the game if enough have joined
|
||||||
if len(self.__players) == 2:
|
if len(self.__players) == 2:
|
||||||
|
self.__gameManager.getLogger().info("2 players have join game starts")
|
||||||
self.startGame(socket)
|
self.startGame(socket)
|
||||||
|
|
||||||
return self.__players
|
return self.__players
|
@ -46,13 +46,16 @@ class NetworkManager:
|
|||||||
self.__gameManager.getLogger().info(f"Connected with {client_address}")
|
self.__gameManager.getLogger().info(f"Connected with {client_address}")
|
||||||
self.__gameManager.getPlayers()[client_address] = client_tcp_socket
|
self.__gameManager.getPlayers()[client_address] = client_tcp_socket
|
||||||
self.__eventHandler[client_address] = TCPEventHandler(client_tcp_socket)
|
self.__eventHandler[client_address] = TCPEventHandler(client_tcp_socket)
|
||||||
|
|
||||||
client_handler_thread = threading.Thread(
|
client_handler_thread = threading.Thread(
|
||||||
target=self.receive,
|
target=self.receive,
|
||||||
args=(client_tcp_socket, client_address)
|
args=(client_tcp_socket, client_address)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.__gameManager.getLogger().info(f"starting client handler thread for client at address {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.daemon = True
|
||||||
client_handler_thread.start()
|
client_handler_thread.start()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.__gameManager.getLogger().error(f"tcp socket failed to accept connection due to error: {e}")
|
self.__gameManager.getLogger().error(f"tcp socket failed to accept connection due to error: {e}")
|
||||||
pass
|
pass
|
||||||
@ -66,9 +69,16 @@ class NetworkManager:
|
|||||||
self.__gameManager.getLogger().info(f"Connection with {client_address} closed.")
|
self.__gameManager.getLogger().info(f"Connection with {client_address} closed.")
|
||||||
break
|
break
|
||||||
|
|
||||||
message = data.decode()
|
try:
|
||||||
messageJson = json.loads(message)
|
|
||||||
user = messageJson.get("user")
|
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
|
# 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
|
# 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":
|
if messageJson["event"] == "login":
|
||||||
self.logger.__gameManager.getLogger().info("user logging in")
|
self.logger.__gameManager.getLogger().info("user logging in")
|
||||||
self.logger.__gameManager.getLogger().info("task passed off to gameManager")
|
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"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.__eventHandler[client_address].handleTCPEvents(messageJson, self.__gameManager, client_address)
|
||||||
self.__gameManager.getLogger().info(f"Received message from {client_address}: {message}")
|
self.__gameManager.getLogger().info(f"Received message from {client_address}: {message}")
|
||||||
|
|
||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
|
|
||||||
if e.errno == 10054:
|
if e.errno == 10054:
|
||||||
self.__gameManager.getLogger().error(f"Connection with {client_address} forcibly closed by remote host.")
|
self.__gameManager.getLogger().error(f"Connection with {client_address} forcibly closed by remote host.")
|
||||||
break
|
break
|
||||||
|
|
||||||
self.__gameManager.getLogger().error(f"Socket error receiving data from {client_address}: {e}")
|
self.__gameManager.getLogger().error(f"Socket error receiving data from {client_address}: {e}")
|
||||||
|
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
self.__gameManager.getLogger().error(f"JSON decoding error receiving data from {client_address}: {e}")
|
self.__gameManager.getLogger().error(f"JSON decoding error receiving data from {client_address}: {e}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.__gameManager.getLogger().error(f"Error receiving data from {client_address}: {e}")
|
self.__gameManager.getLogger().error(f"Error receiving data from {client_address}: {e}")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user