def is_valid_move(self, current_room, destination_room): room = self._rooms[current_room] current_room_str = RoomEnum.to_string(current_room) destination_room_str = RoomEnum.to_string(destination_room) self._logger.debug('Determining if move from "%s" to "%s" is valid.', current_room_str, destination_room_str) valid_move = room.is_valid_move(destination_room) return valid_move
def perform_move(self, player_enum, destination_room): current_room = self._player_positions[player_enum] player_enum_str = PlayerEnum.to_string(player_enum) current_room_str = RoomEnum.to_string(current_room) destination_room_str = RoomEnum.to_string(destination_room) self._logger.debug('Attempting to move %s from "%s" to "%s".', player_enum_str, current_room_str, destination_room_str) # Check to see if the move is valid on the gameboard valid_move = self._gameboard.is_valid_move(current_room, destination_room) # Check to see if the destination is an occupied hallway if self._gameboard.is_hallway(destination_room) == True: for player_position, player_location in self._player_positions.items(): if destination_room == player_location: valid_move = False break # Update the player position if the move is valid if valid_move == True: self._player_positions[player_enum] = destination_room return valid_move
def handle_message(self): while self._output_queue.qsize() > 0: message = self._output_queue.get() message_enum = message.get_message_enum() num_args = message.get_num_args() message_args = message.get_args() # Handle move messages if message_enum == MessageEnum.MOVE: valid_move = message_args[0] if valid_move == True: player_enum = message_args[1] old_room = message_args[2] new_room = message_args[3] old_room_str = RoomEnum.to_string(old_room) player_enum_str = PlayerEnum.to_string(player_enum) new_room_str = RoomEnum.to_string(new_room) self._logger.debug('%s moved from "%s" to "%s".', player_enum_str, old_room_str, new_room_str) else: self._logger.debug('Invalid move!') # Handle suggest messages elif message_enum == MessageEnum.SUGGEST: self._logger.debug('Received a suggest message.') # Handle accuse message elif message_enum == MessageEnum.ACCUSE: self._logger.debug('Received an accusation message.') # Handle lobby ready and unready messages elif message_enum == MessageEnum.LOBBY_ADD or message_enum == MessageEnum.LOBBY_READY or message_enum == MessageEnum.LOBBY_UNREADY: # Refresh the lobby with the updated list of player names and ready states # This keeps the lobby in sync in case someone leaves and provides the entire lobby list to new players lobby_list = message_args self._logger.debug('Printing lobby list:') for lobby_entry in lobby_list: player_name = lobby_entry[0] ready_state = lobby_entry[1] self._logger.debug('\t(%s, %s).', player_name, ready_state) # Handle lobby change player message elif message_enum == MessageEnum.LOBBY_CHANGE_PLAYER: player_enum = message_args[0] self._logger.debug('You have been assigned the character "%s".', PlayerEnum.to_string(player_enum)) self._client_model.set_player_enum(player_enum) # Handle game state change message elif message_enum == MessageEnum.GAME_STATE_CHANGE: self._logger.debug('Received a game state change message.') # Handle turn over message elif message_enum == MessageEnum.TURN_OVER: self._logger.debug('Received a turn over message.') # Handle turn begin message elif message_enum == MessageEnum.TURN_BEGIN: player_enum = message_args[0] self._logger.debug('It is now "%s\'s" turn!.', PlayerEnum.to_string(player_enum)) if player_enum == self._client_model.get_player_enum(): self._logger.debug('It is now your turn!') # Handle error message elif message_enum == MessageEnum.ERROR: self._logger.debug('Received an error message.')