def run(self): logger.debug('Running...') self.commandsManager.start() self.commandsManager.addCommand(self.startCommand) self.play() self.reportCollector.winners = self.winners() self.commandsManager.addCommand(self.endCommand) self.commandsManager.stop() logger.debug('ok')
def executeCommand(self, command): logger.debug('Executing command: ' + command.summary()) self.game.permission.acquire() self.validateRegisteredCommand(command) command.process(self.game) self.game.notifyExecutedCommand(command) self.game.permission.release() self.executedCommands.append(command) logger.info('> ' + command.summary()) if isinstance(command, PlayerCommand): logger.info(command.player.context.summary())
def start(self): logger.debug('\nStart game') startTime = time.clock() try: self.thread = threading2.KThread(target=self.run, name='Thread-Game') self.thread.daemon = True self.thread.start() self.thread.joinWithTimeout(self.configurations.timeForGame) if self.thread.isExpired(): raise errors.TimeoutGameError() except Exception, e: logger.debug('Error in game: ' + e.message) self.reportCollector.exception = e raise e
def playerPlays(self, commandsManager, player): logger.debug('Player playing: ' + player.name) self.commandsManager.addCommand(self.playerplaysCommand) startTime = time.clock() thread = threading2.KThread(target=player.play, args=[commandsManager], name='Thread-Player-' + player.name) try: thread.daemon = True thread.start() thread.joinWithTimeout(self.configurations.timeForPlay) finally: endTime = time.clock() self.reportCollector.addPlaysDuration(player, float(endTime - startTime)) logger.debug('Total of %s-strategy iteration: %ss' % (player.name, str(endTime - startTime))) if thread.isExpired(): raise errors.TimeoutStrategyError(player.name + ' - ' + str(endTime - startTime) + 's') logger.debug('Player stop playing: ' + player.name)
def notifyExecutedCommand(self, command): logger.debug('Notifing observers') self.reportCollector.addCommand(command, self.observers) self.notifyObservers(CommandEvent(self, command))
def stop(self): logger.debug('Game stopped') self.thread.kill()
startTime = time.clock() try: self.thread = threading2.KThread(target=self.run, name='Thread-Game') self.thread.daemon = True self.thread.start() self.thread.joinWithTimeout(self.configurations.timeForGame) if self.thread.isExpired(): raise errors.TimeoutGameError() except Exception, e: logger.debug('Error in game: ' + e.message) self.reportCollector.exception = e raise e finally: endTime = time.clock() self.reportCollector.durationTime = (endTime - startTime) logger.debug('End game') logger.info('Winners: ' + ' '.join(winner.name for winner in self.winners())) def stop(self): logger.debug('Game stopped') self.thread.kill() # Template Method def run(self): logger.debug('Running...') self.commandsManager.start() self.commandsManager.addCommand(self.startCommand) self.play() self.reportCollector.winners = self.winners() self.commandsManager.addCommand(self.endCommand) self.commandsManager.stop()