def _use_ai(self): if not Settings.CLIENT_USE_AI: return if self._player_number not in self._light_cycles: return command = self._client_ai.tick(self._tick_number, self._ai_number_of_commands_received) if command == 'left': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('left')])) return if command == 'double_left': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [ Messages.player_input('left'), Messages.player_input('left') ])) return if command == 'right': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('right')])) return if command == 'double_right': self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [ Messages.player_input('right'), Messages.player_input('right') ])) return
def _phase_play_game(self): if self._game_over: return logging.debug('_phase_play_game, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) while self._keep_running: self._user_input.get_input() messages = self._user_input.get_and_delete_messages() if messages != []: self._connection.send_message( Messages.tick(self._tick_number, self._game_number, messages)) self._connection.poll(0, 4) if self._tick_number + 2 in self._messages_on_tick: self._process_messages() sleep_time = Settings.TICK - (time.time() - self._last_tick_time) if sleep_time > 0.001: time.sleep(sleep_time) self._last_tick_time = time.time() self._update_display() self._no_message_ticks = 0 self._use_ai() else: self._no_message_ticks += 1 if self._no_message_ticks > 5: self._connection.send_message( Messages.resend_tick(self._tick_number, self._game_number)) logging.debug('_no_message_ticks: ' + str(self._no_message_ticks) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number) + ' _tick_number: ' + str(self._tick_number)) if self._no_message_ticks > Settings.CLIENT_SECONDS_TO_WAIT / Settings.TICK: self._game_over = True logging.debug('no message tick game over, _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number) + ' _tick_number: ' + str(self._tick_number)) time.sleep((Settings.TICK / 2.0)) if self._game_over: break self._connection.poll(Settings.TICK / 2.0, 2) logging.debug('_phase_play_game, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number) + ' _tick_number: ' + str(self._tick_number))
def _create_light_cycles(self): count = 0 for key in self._connections: self._connection_to_cycle[key] = count self._connections[key].send_message( Messages.tick(0, self._game_number, [Messages.player_number(count)])) self._message_resend_count[key] = 0 self._light_cycles[count] = Light_cycle(None, count, True) self._light_cycles[count].set_location( Settings.CYCLE_LOCATIONS[count]) self._light_cycles[count].set_direction( Settings.CYCLE_DIRECTIONS[count]) count += 1 for i in range(count, Settings.MATCHMAKER_MAXIMUM_PLAYERS): self._light_cycles[i] = Light_cycle(None, i, True) self._light_cycles[i].set_location(Settings.CYCLE_LOCATIONS[i]) self._light_cycles[i].set_direction(Settings.CYCLE_DIRECTIONS[i]) self._light_cycles[i].set_trail_on(None) self._light_cycles_ai[i] = Ai(True, self._light_cycles, i) for cycle in self._light_cycles.itervalues(): cycle.move_tick(0) self._send_messages_for_tick() self._tick_number += 1
def end_game(self): if self._game_over: return self._game_over = True for key in self._connections: won = False if len(self._light_cycles) == 1: if self._connection_to_cycle[key] in self._light_cycles: won = True # send extra messages because of client delay self._connections[key].send_message( Messages.tick(self._tick_number, self._game_number, [Messages.game_over(won)])) self._connections[key].send_message( Messages.tick(self._tick_number + 1, self._game_number, [Messages.game_over(won)])) self._connections[key].send_message( Messages.tick(self._tick_number + 2, self._game_number, [Messages.game_over(won)])) self._connections[key].set_game(None) logging.debug('end_game, _game_number: ' + str(self._game_number) + ' _tick_number: ' + str(self._tick_number))
def _send_messages_for_tick(self): messages = [] for cycle in self._light_cycles.itervalues(): while True: try: message = cycle.get_messages().pop() except IndexError: break messages.append(message) while True: try: message = cycle.trail_get_messages().pop() except IndexError: break messages.append(message) self._message_history[self._tick_number] = Messages.tick( self._tick_number, self._game_number, messages) for key in self._connections: self._connections[key].send_message( self._message_history[self._tick_number])
def _phase_load_cycles(self): if self._game_over: return logging.debug('_phase_load_cycles, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) waiting_for_server_ticks = 0 while self._keep_running: self._user_input.get_input() self._connection.poll(Settings.TICK / 2.0, 2) if 1 in self._messages_on_tick: self._process_messages() break if waiting_for_server_ticks > Settings.CLIENT_SECONDS_TO_WAIT / Settings.TICK: self._game_over = True logging.debug( '_phase_load_cycles failed, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) break waiting_for_server_ticks += 1 if not self._keep_running: return self._connection.send_message( Messages.start_game(self._tick_number, self._game_number)) self._connection.poll(0, 2) for key in self._light_cycles: if key == self._player_number: continue self._light_cycles[key].set_color(Colors.CYCLE_COLORS[key]) self._light_cycles[key].set_trail_color(Colors.CYCLE_COLORS[key]) self._text = None self._show_instructions = False self._update_display() self._user_input.get_input() self._user_input.get_and_delete_messages() if Settings.CLIENT_USE_AI: self._client_ai = Ai(False, self._light_cycles, self._player_number) self._connection.send_message( Messages.tick(self._tick_number, self._game_number, [Messages.player_input('space')])) self._connection.send_message( Messages.start_game(self._tick_number, self._game_number)) self._connection.poll(0, 2) waiting_for_server_ticks = 0 while self._keep_running: self._user_input.get_input() self._connection.poll(Settings.TICK / 2.0, 2) if 4 in self._messages_on_tick: break if waiting_for_server_ticks > Settings.CLIENT_SECONDS_TO_WAIT / Settings.TICK: self._game_over = True logging.debug( '_phase_load_cycles failed, _client_start_time: ' + str(self._client_start_time) + ' _game_number: ' + str(self._game_number) + ' _player_number: ' + str(self._player_number)) break waiting_for_server_ticks += 1 waiting_for_server_ticks = 0