import json import socket import time from Classes.Game.Player import Player class GameManager: __players:dict __playingPlayer:Player __state:str __round:str __cards:list def __init__(self, logger): self.__players = {} self.__playingPlayer = None self.__state = "waiting" self.__round = "none" self.logger = logger self.__cards = [] pass def getLogger(self): return self.logger # card management def spawnCard(self, card, owner, x, y): # self.logger.info("spawning card") self.__cards.append(card) payload = { "event":"PlacedCard", "owner": owner, "card": card, "x": x, "y": y, } for userAddr in self.__players.keys(): try: self.logger.info(f"send to client {self.__players[userAddr]['player'].getID() != owner}") if self.__players[userAddr]["player"].getID() != owner: self.__players[userAddr]["socket"].send(json.dumps(payload).encode()) except: pass # game round management # this section manages the flow of rounds this should inherit itself # ============================================================================= # this function iterates all def progressRound(self): # phases # - playerPrep => playing player switches, gets a mana point and gets verified if self.__playingPlayer != None: for player in self.__players: if self.__playingPlayer != player: self.__playingPlayer = player else: self.__playingPlayer = next(iter(self.__players)) # - playerDraw => player draws a card # - playerPlay => player can place cards and active effects # - playerEnd => player ends his turn and the code reiterates with the remaining player pass # game state management # this section mostly only used by the networking and event handling classes # other parts should never need to interface with this unless really required # ============================================================================= def startGame(self): self.__state = "running" players = list(self.__players.values()) print("game starts") self.logger.info("game manager is starting the game") for userAddr, player_data in self.__players.items(): try: user = self.__players[userAddr]["player"] user.addMana(1000) user.adjustHP(1000) user.shuffleDeck() cards = self.__players[userAddr]["deck"] user.setHand(cards[:5]) # iterates until the enemy player is not anymore equal to current player enemy = next(player_data["player"] for player_data in players if player_data["player"] != user) payload = { "event": "startgame", "player": { "mana": user.getMana(), "hp": user.getHP(), "hand": user.getHand() }, "enemy": { "id": enemy.getID(), "name": enemy.getName(), "hp": enemy.getHP(), }, } print(f"user {player_data["socket"]}") player_data["socket"].send(json.dumps(payload).encode()) except Exception as e: self.logger.error(f"failed to start game due to error: {e}") pass # handles notifying all players that the game starts pass def stopGame(self): # handles notifying all players that the game stops # handles stoping the game itself and notifies server to stop itself pass # player management # the network manager will create a player instance # ============================================================= # gets all player known to the game manager and returns them def getPlayers(self) -> dict: return self.__players # 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, deck) -> dict: self.logger.info(f"creating user with id: {player.getID}") self.__players[clientAddr] = { "player": player, "deck": deck, "socket":socket } self.logger.info(f"new length of user dictionary: {len(self.__players)}") payload = { "event":"loginresponse", "status": "success", "id": player.getID(), "name": player.getName() } socket.send(json.dumps(payload).encode()) # counts participating players and starts the game if enough have joined if len(self.__players) >= 2: time.sleep(1) self.logger.info("2 players have join game starts") self.startGame() return self.__players