def __init__(self): self.start_time = None self.gui = None self.session = None self.state = None self.running = True self.game_url = None self.config = Config() self.bot = Curses_ui_bot() # Our bot self.states = [] self.delay = 0.5 # Delay in s between turns in replay mode self.victory = 0 self.time_out = 0
def replay(self): """Replay last game""" # Restart with a new bot self.bot = Curses_ui_bot() for i in range(self.config.number_of_games): # start a new game if self.bot.running: self.restart_game() gold = 0 winner = "Noone" for player in self.bot.game.heroes: if int(player.gold) > gold: winner = player.name gold = int(player.gold) self.pprint("**** " + winner + " wins. ****") self.pprint("Game finished: " + str(i + 1) + "/" + str(self.config.number_of_games))
def start_game(self): """Starts a game with all the required parameters""" self.running = True # Delete prévious game states self.states = [] # Restart game with brand new bot self.bot = Curses_ui_bot() # Default move is no move ! direction = "Stay" # Create a requests session that will be used throughout the game self.pprint('Connecting...') self.session = requests.session() if self.config.game_mode == 'arena': self.pprint('Waiting for other players to join...') try: # Get the initial state # May raise error if self.get_new_state() returns # no data or inconsistent data (network problem) self.state = self.get_new_game_state() self.states.append(self.state) self.pprint("Playing at: " + self.state['viewUrl']) except (KeyError, TypeError) as e: # We can not play a game without a state self.pprint("Error: Please verify your settings.") self.pprint("Settings:", self.config.__dict__) self.running = False return for i in range(self.config.number_of_turns + 1): if self.running: # Choose a move self.start_time = time.time() try: while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: line = sys.stdin.read(1) if line.strip() == "q": self.running = False self.bot.running = False break elif line.strip() == "p": self.gui.pause() elif line.strip() == "s": self.save_game() if self.bot.running: direction = self.bot.move(self.state) self.display_game() except Exception as e: # Super error trap ! if self.gui.log_win: self.pprint("Error at client.start_game:", str(e)) self.pprint( "If your code or your settings are not responsible of this error, please report this error to:" ) self.pprint("[email protected].") self.gui.pause() self.running = False return if not self.is_game_over(): # Send the move and receive the updated game state self.game_url = self.state['playUrl'] self.state = self.send_move(direction) self.states.append(self.state) # Clean up the session self.session.close()
def start_ui(self): """Start the curses UI""" self.bot = Curses_ui_bot() self.running = True self.game_url = None self.states = [] self.state = None self.gui = ui.tui() choice = self.gui.ask_main_menu() if choice == '1': # Load config then play game self.load_config() self.gui.draw_game_windows() self.play() elif choice == '2': # Setup game self.config = Config() choice = self.gui.ask_game_mode() if choice == '1': # Arena mode config self.config.game_mode = "arena" self.config.number_of_turns = 300 self.config.number_of_games = self.gui.ask_number_games() elif choice == '2': # Training mode config self.config.game_mode = "training" self.config.number_of_games = 1 self.config.number_of_turns = self.gui.ask_number_turns() self.config.map_name = "m" + str(self.gui.ask_map()) self.config.server_url = self.gui.ask_server_url( self.config.game_mode) self.config.key = self.gui.ask_key(self.config.game_mode) if self.gui.ask_save_config(): self.save_config() if self.gui.ask_play_game(): self.gui.draw_game_windows() self.play() else: self.start_ui() elif choice == '3': # Load game from file game_file_name = self.gui.ask_game_file_path() self.load_game(game_file_name) self.gui.draw_game_windows() self.replay() elif choice == '4': # Load game from URL game_file_url = self.gui.ask_game_file_url() self.download_game_file(game_file_url) self.gui.draw_game_windows() self.replay() elif choice == '5': # quit self.gui.quit_ui() exit(0) if self.gui.running and self.gui.help_win: key = None while key != 'm': key = self.gui.ask_quit() if key == 's': self.save_game() elif key == 'r': self.replay() self.gui.clear() self.start_ui()