import logging import config from lamps.Lamp import CREDIT from itertools import cycle from lamps.LampGroup import * import asyncio class GameState: players = None credits = config.BALLS_PER_GAME isPlaying = False isStarted = False isIdle = asyncio.Event() def __init__(self, playerStateFactory, highscore, specialDisplay, currentPlayerID = 0): CREDIT.activate() self.highscore = highscore self.playerStateFactory = playerStateFactory self.dummyPlayer = playerStateFactory.createDummyPlayerState() self._currentPlayer = self.dummyPlayer self.gameStateID = currentPlayerID self.highscore = highscore self.specialDisplay = specialDisplay self.specialDisplay.printCredits(self.credits) self.startIdleLoop() def nextPlayer(self): self.currentPlayer = next(self.players) def startGame(self, playerCount): self.isStarted = True self.isPlaying = True self.createPlayers(playerCount) self.nextPlayer() logging.info("Game Started") def endGame(self): self.writeHighscore() self.players = None self.isPlaying = False self.isStarted = False logging.info("Game ended") def startIdleLoop(self): asyncio.run(self.idleLoop()) pass def stopIdleLoop(self): self.isIdle.set() self.deactivate_all() pass def deactivate_all(self): all_cabinet_lamps = [BONUS_LAMPS, BONUS_MULTIPLIER_LAMPS, CHAMP_LAMPS, UPPER_PLAYFIELD_TIME_LAMPS, TUNNEL_NUMBER_LAMPS, TUNNEL_LAMPS, TUNNEL_SCORE_LAMPS] for lamp_group in all_cabinet_lamps: lamp_group.deactivate() async def idleLoop(self): all_cabinet_lamps = [BONUS_LAMPS, BONUS_MULTIPLIER_LAMPS, CHAMP_LAMPS, UPPER_PLAYFIELD_TIME_LAMPS, TUNNEL_NUMBER_LAMPS, TUNNEL_LAMPS, TUNNEL_SCORE_LAMPS] while True: for _ in range(20): random_group = random.choice(all_cabinet_lamps) random_group.toggleRandom(0.1) for lamps in all_cabinet_lamps: for _ in lamps.lamps: lamps.cycle() if self.isIdle.is_set(): return time.sleep(0.05) if self.isIdle.is_set(): return def createPlayers(self, playerCount): players = [] for i in range(playerCount): player = self.playerStateFactory.createPlayerState(i) players.append(player) self.players = cycle(players) @property def currentPlayer(self): return self._currentPlayer @currentPlayer.setter def currentPlayer(self, player): logging.info("Set active player to player " + str(player.id)) self._currentPlayer = self.dummyPlayer player.activate() self._currentPlayer = player def writeHighscore(self): for player in self.players: if player.hasHighscore: with open(config.HIGHSCORE_FILE, "w+") as file: file.write(player.score)