def _navigate_to_shiplist_page(self, target_page): """Wrapper method that navigates the shiplist to the specified target page from the known current page. Uses NavList's navigate_to_page for navigation. Args: target_page (int): page to navigate to Raises: ValueError: invalid target_page specified """ if target_page > self.ship_page_count: raise ValueError( "Invalid shiplist target page ({}) for number of known pages " "({}).".format(target_page, self.ship_page_count)) self.current_shiplist_page = NavList.navigate_to_page( self.regions, self.ship_page_count, self.current_shiplist_page, target_page, 'repair')
def _resolve_replacement_ship_by_asset(self, slot_config): """Method that finds a resolves a replacement ship by class or ship asset. Args: slot_config (dict): dictionary containing the slot's config Returns: bool: True if a successful switch was made; False otherwise """ cache_override = True self.temp_ship_config_dict = {} self.temp_asset_position_dict = {} self._switch_shiplist_sorting('class') # switch tabs to all relevant classes specified for the slot specified_tabs = [] for ship in slot_config['ships']: specified_tabs.append(self.CLASS_TAB_MAPPING[ship['class']]) self._switch_shiplist_tab(list(set(specified_tabs))) if slot_config['slot'] in self.position_cache: # start search from cached position, if available Util.log_msg("Jumping to cached page {}.".format( self.position_cache[slot_config['slot']])) self._navigate_to_shiplist_page( self.position_cache[slot_config['slot']]) else: # otherwise, start from first page to establish a good position self.current_shiplist_page = NavList.navigate_to_page( self.regions, self.ship_page_count, self.current_shiplist_page, 1) while (not self.temp_asset_position_dict and self.current_shiplist_page <= self.ship_page_count): ship_search_threads = [] for ship in slot_config['ships']: ship_search_threads.append( Thread(target=self._match_shiplist_ships_func, args=(ship['asset'], ship))) Util.multithreader(ship_search_threads) if not self.temp_asset_position_dict: # no matches on this page; continue loop self._navigate_to_shiplist_page(self.current_shiplist_page + 1) continue if cache_override: # update cache on first encounter self._set_position_cache(slot_config['slot']) cache_override = False ship_position_list = [ i for j in [ self.temp_asset_position_dict[x] for x in self.temp_asset_position_dict ] for i in j ] ship_position_list.sort() ship_position_list = self._filter_ships_on_level( ship_position_list) Util.log_msg( "Candidate ships found in page {} positions {}".format( self.current_shiplist_page, ", ".join([str(i) for i in ship_position_list]))) for asset in self.temp_asset_position_dict: if '_' in asset: # ship-specific asset mode for position in self.temp_asset_position_dict[asset]: if position not in ship_position_list: # ship in position did not pass filtering on level continue availability = ( self._choose_and_check_availability_of_ship( position, slot_config['criteria'])) if availability is True: return True elif availability == 'conflict': # ship already exists elsewhere in fleet; skip break else: # class-wide asset mode for position in ship_position_list: if self._choose_and_check_availability_of_ship( position, slot_config['criteria']) is True: return True # no available ships on this page; reset matches and continue loop self.temp_ship_config_dict = {} self.temp_asset_position_dict = {} if 'sparkle' in slot_config['criteria']: # if in sparkle mode and we didn't see any valid ships here, # don't jump to this page on the next pass cache_override = True if self.current_shiplist_page < self.ship_page_count: self._navigate_to_shiplist_page(self.current_shiplist_page + 1) else: # at last page but no switches made, so the switch was a # failure return False if 'sparkle' in slot_config['criteria']: # if in sparkle mode and we reach this point, we've exhausted the # list of candidate ships; disable the combat module self.combat.disable_module() return False