diff --git a/pokemongo_bot/base_task.py b/pokemongo_bot/base_task.py index e643ee7c07..7dd0bf35f3 100644 --- a/pokemongo_bot/base_task.py +++ b/pokemongo_bot/base_task.py @@ -1,5 +1,21 @@ import logging +from pokemongo_bot import inventory +from pokemongo_bot.inventory import Pokemon +ITEM_POKEBALL = 1 +ITEM_GREATBALL = 2 +ITEM_ULTRABALL = 3 + +LOGIC_TO_FUNCTION = { + 'or': lambda x, y, z: x or y or z, + 'and': lambda x, y, z: x and y and z, + 'orand': lambda x, y, z: x or y and z, + 'andor': lambda x, y, z: x and y or z +} + +ENOUGH_POKEBALL_FOR_ALL = 1 +ENOUGH_POKEBALL_FOR_VIP = 2 +NOT_ENOUGH_POKEBALL = 3 class BaseTask(object): TASK_API_VERSION = 1 @@ -37,3 +53,52 @@ def emit_event(self, event, sender=None, level='info', formatted='', data={}): def initialize(self): pass + + + def _is_vip_pokemon(self, pokemon): + # having just a name present in the list makes them vip + if self.bot.config.vips.get(pokemon.name) == {}: + return True + return self._pokemon_matches_config(self.bot.config.vips, pokemon, default_logic='or') + + def _pokemon_matches_config(self, config, pokemon, default_logic='and'): + pokemon_config = config.get(pokemon.name, config.get('any')) + + if not pokemon_config: + return False + + catch_results = { + 'ncp': False, + 'cp': False, + 'iv': False, + } + + if pokemon_config.get('never_catch', False): + return False + + if pokemon_config.get('always_catch', False): + return True + + catch_ncp = pokemon_config.get('catch_above_ncp', 0.8) + if pokemon.cp_percent > catch_ncp: + catch_results['ncp'] = True + + catch_cp = pokemon_config.get('catch_above_cp', 1200) + if pokemon.cp > catch_cp: + catch_results['cp'] = True + + catch_iv = pokemon_config.get('catch_above_iv', 0.8) + if pokemon.iv > catch_iv: + catch_results['iv'] = True + + return LOGIC_TO_FUNCTION[pokemon_config.get('logic', default_logic)](*catch_results.values()) + + def _check_enough_pokeball(self): + self.min_ultraball_to_keep = self.config.get('min_ultraball_to_keep', 10) + if inventory.items().get(ITEM_POKEBALL).count == 0: + if inventory.items().get(ITEM_GREATBALL).count == 0: + if inventory.items().get(ITEM_ULTRABALL).count == 0: + return NOT_ENOUGH_POKEBALL + elif inventory.items().get(ITEM_ULTRABALL).count <= self.min_ultraball_to_keep: + return ENOUGH_POKEBALL_FOR_VIP + return ENOUGH_POKEBALL_FOR_ALL diff --git a/pokemongo_bot/cell_workers/catch_lured_pokemon.py b/pokemongo_bot/cell_workers/catch_lured_pokemon.py index 5c1456b88f..559a1c8712 100644 --- a/pokemongo_bot/cell_workers/catch_lured_pokemon.py +++ b/pokemongo_bot/cell_workers/catch_lured_pokemon.py @@ -7,12 +7,26 @@ from pokemongo_bot.cell_workers.utils import fort_details, distance from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker +ENOUGH_POKEBALL_FOR_ALL = 1 +ENOUGH_POKEBALL_FOR_VIP = 2 +NOT_ENOUGH_POKEBALL = 3 class CatchLuredPokemon(BaseTask): SUPPORTED_TASK_API_VERSION = 1 def work(self): lured_pokemon = self.get_lured_pokemon() + + enough_pokeball_for = self._check_enough_pokeball() + if enough_pokeball_for == NOT_ENOUGH_POKEBALL: + return WorkerResult.SUCCESS + elif enough_pokeball_for == ENOUGH_POKEBALL_FOR_VIP: + lured_vip_pokemon = [] + for pokemon in lured_pokemon: + if(self._is_vip_pokemon(pokemon)): + lured_vip_pokemon.append(pokemon) + lured_pokemon = lured_vip_pokemon + if len(lured_pokemon) > 0: self.catch_pokemon(lured_pokemon[0]) diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py index eb839e977e..7413d1bc81 100644 --- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py +++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py @@ -29,6 +29,10 @@ ITEM_ULTRABALL = 3 ITEM_RAZZBERRY = 701 +ENOUGH_POKEBALL_FOR_ALL = 1 +ENOUGH_POKEBALL_FOR_VIP = 2 +NOT_ENOUGH_POKEBALL = 3 + LOGIC_TO_FUNCTION = { 'or': lambda x, y, z: x or y or z, 'and': lambda x, y, z: x and y and z, @@ -108,12 +112,10 @@ def work(self, response_dict=None): return WorkerResult.SUCCESS is_vip = self._is_vip_pokemon(pokemon) - if inventory.items().get(ITEM_POKEBALL).count < 1: - if inventory.items().get(ITEM_GREATBALL).count < 1: - if inventory.items().get(ITEM_ULTRABALL).count < 1: - return WorkerResult.SUCCESS - elif (not is_vip) and inventory.items().get(ITEM_ULTRABALL).count <= self.min_ultraball_to_keep: - return WorkerResult.SUCCESS + enough_pokeball_for = self._check_enough_pokeball() + + if enough_pokeball_for == NOT_ENOUGH_POKEBALL or (not is_vip and enough_pokeball_for == ENOUGH_POKEBALL_FOR_VIP): + return WorkerResult.SUCCESS # log encounter self.emit_event( @@ -195,47 +197,9 @@ def create_encounter_api_call(self): # helpers ############################################################################ - def _pokemon_matches_config(self, config, pokemon, default_logic='and'): - pokemon_config = config.get(pokemon.name, config.get('any')) - - if not pokemon_config: - return False - - catch_results = { - 'ncp': False, - 'cp': False, - 'iv': False, - } - - if pokemon_config.get('never_catch', False): - return False - - if pokemon_config.get('always_catch', False): - return True - - catch_ncp = pokemon_config.get('catch_above_ncp', 0.8) - if pokemon.cp_percent > catch_ncp: - catch_results['ncp'] = True - - catch_cp = pokemon_config.get('catch_above_cp', 1200) - if pokemon.cp > catch_cp: - catch_results['cp'] = True - - catch_iv = pokemon_config.get('catch_above_iv', 0.8) - if pokemon.iv > catch_iv: - catch_results['iv'] = True - - return LOGIC_TO_FUNCTION[pokemon_config.get('logic', default_logic)](*catch_results.values()) - def _should_catch_pokemon(self, pokemon): return self._pokemon_matches_config(self.bot.config.catch, pokemon) - def _is_vip_pokemon(self, pokemon): - # having just a name present in the list makes them vip - if self.bot.config.vips.get(pokemon.name) == {}: - return True - return self._pokemon_matches_config(self.bot.config.vips, pokemon, default_logic='or') - def _pct(self, rate_by_ball): return '{0:.2f}'.format(rate_by_ball * 100)