fixed variable errors

This commit is contained in:
steev 2024-01-14 21:13:03 +01:00
parent b98b9821dc
commit 160fce0484
16 changed files with 42 additions and 13 deletions

View File

@ -66,7 +66,7 @@ class GameManager:
tcpSocket.send(json.dumps(payload).encode()) tcpSocket.send(json.dumps(payload).encode())
except Exception as e: except Exception as e:
print(f"failed to start game due to error: {e}") self.logger.error(f"failed to start game due to error: {e}")
break break
# handles notifying all players that the game starts # handles notifying all players that the game starts
pass pass
@ -88,16 +88,16 @@ class GameManager:
# 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.logger.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)}") self.logger.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.logger.info("2 players have join game starts")
self.startGame(socket) self.startGame(socket)
return self.__players return self.__players

View File

@ -6,7 +6,7 @@ import threading
from Classes.Game.Player import Player from Classes.Game.Player import Player
from Classes.System.GameManager import GameManager from Classes.System.GameManager import GameManager
from Classes.System.Network.EventHandler import UDPEventHandler, TCPEventHandler from Classes.System.Network.EventHandler import TCPEventHandler
from Classes.System.World import World from Classes.System.World import World
class NetworkManager: class NetworkManager:
@ -84,16 +84,16 @@ class NetworkManager:
# 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
if user not in self.__gameManager.getPlayers(): if user not in self.__gameManager.getPlayers():
if messageJson["event"] == "login": if messageJson["event"] == "login":
self.logger.__gameManager.getLogger().info("user logging in") self.__gameManager.getLogger().info("user logging in")
self.logger.__gameManager.getLogger().info("task passed off to gameManager") self.__gameManager.getLogger().info("task passed off to gameManager")
user = 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.__gameManager.getLogger().info(f"confirming login for user")
self.send({ self.send({
"event":"loginresponse", "event":"loginresponse",
"id": user["player"].getID(), "id": user[client_address]["player"].getID(),
}) }, client_address)
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}")
@ -109,8 +109,8 @@ class NetworkManager:
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"UknownError receiving data from {client_address} due to error: {e}")
def broadcast(self, payload:dict): def broadcast(self, payload:dict):
for user in self.__gameManager.getPlayers().values(): for user in self.__gameManager.getPlayers().values():

View File

@ -0,0 +1,29 @@
19:08:13,964 root INFO starting up server
19:08:13,964 root INFO starting up game manager
19:08:13,965 root INFO preparing to start server
19:08:13,965 root INFO starting up network manager
19:08:13,965 root INFO starting up network manager
19:08:13,965 root INFO starting up tcp server
19:08:13,967 root INFO starting up thread for client socket accepting
19:08:18,567 root INFO Connected with ('127.0.0.1', 54324)
19:08:18,567 root INFO starting client handler thread for client at address ('127.0.0.1', 54324)
19:08:18,580 root INFO decoded message {'event': 'login', 'username': 'player', 'deck': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
19:08:18,588 root INFO user in message None
19:08:18,588 root INFO user logging in
19:08:18,589 root INFO task passed off to gameManager
19:08:18,589 root INFO creating user with id: <bound method Player.getID of <Classes.Game.Player.Player object at 0x000001E4CF425BD0>>
19:08:18,589 root INFO new length of user dictionary: 1
19:08:18,589 root INFO connected users 1
19:08:18,589 root INFO confirming login for user
19:08:18,590 root INFO incommingevent {'event': 'login', 'username': 'player', 'deck': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
19:08:18,590 root INFO Received message from ('127.0.0.1', 54324): {"event": "login", "username": "player", "deck": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
19:10:33,867 root ERROR Connection with ('127.0.0.1', 54324) forcibly closed by remote host.
19:10:36,58 root INFO Connected with ('127.0.0.1', 54355)
19:10:36,58 root INFO starting client handler thread for client at address ('127.0.0.1', 54355)
19:10:36,60 root INFO decoded message {'event': 'login', 'username': 'player', 'deck': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
19:10:36,60 root INFO user in message None
19:10:36,60 root INFO user logging in
19:10:36,62 root INFO task passed off to gameManager
19:10:36,63 root INFO creating user with id: <bound method Player.getID of <Classes.Game.Player.Player object at 0x000001E4CF425C10>>
19:10:36,63 root INFO new length of user dictionary: 2
19:10:36,63 root INFO 2 players have join game starts