diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index a7628d01ff..b9a2172bca 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -14,7 +14,7 @@ from pgoapi.utilities import f2i import logger -from cell_workers import CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker, RecycleItemsWorker +from cell_workers import CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker from cell_workers.utils import distance, get_cellid, encode, i2f from human_behaviour import sleep from item_list import Item @@ -160,7 +160,7 @@ def work_on_cell(self, cell, position): # Check if session token has expired self.check_session(position) - worker = InitialTransferWorker(self) + worker = PokemonTransferWorker(self) if worker.work() == WorkerResult.RUNNING: return diff --git a/pokemongo_bot/cell_workers/__init__.py b/pokemongo_bot/cell_workers/__init__.py index 804c6a1633..ab69be4b2a 100644 --- a/pokemongo_bot/cell_workers/__init__.py +++ b/pokemongo_bot/cell_workers/__init__.py @@ -3,7 +3,7 @@ from pokemon_catch_worker import PokemonCatchWorker from seen_fort_worker import SeenFortWorker from move_to_fort_worker import MoveToFortWorker -from initial_transfer_worker import InitialTransferWorker +from pokemon_transfer_worker import PokemonTransferWorker from evolve_all_worker import EvolveAllWorker from catch_visible_pokmeon_worker import CatchVisiblePokemonWorker from recycle_items_worker import RecycleItemsWorker diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py index 1a1c7db818..620a947cf3 100644 --- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py +++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py @@ -5,7 +5,7 @@ from pokemongo_bot import logger from pokemongo_bot.human_behaviour import sleep - +from pokemon_transfer_worker import PokemonTransferWorker class PokemonCatchWorker(object): BAG_FULL = 'bag_full' @@ -192,6 +192,10 @@ def work(self): if status is 1: id_list2 = self.count_pokemon_inventory() + pokemon_to_transfer = list(Set(id_list2) - Set(id_list1)) + if len(pokemon_to_transfer) == 0: + raise RuntimeError( + 'Trying to transfer or evolve 0 pokemons!') self.bot.metrics.captured_pokemon(pokemon_name, cp, iv_display, pokemon_potential) @@ -202,7 +206,6 @@ def work(self): ), 'blue') if self.config.evolve_captured: - pokemon_to_transfer = list(Set(id_list2) - Set(id_list1)) # No need to capture this even for metrics, player stats includes it. self.api.evolve_pokemon(pokemon_id=pokemon_to_transfer[0]) response_dict = self.api.call() @@ -213,53 +216,11 @@ def work(self): else: logger.log( 'Failed to evolve {}!'.format(pokemon_name)) - - if self.should_release_pokemon(pokemon_name, cp, pokemon_potential, response_dict): - # Transfering Pokemon - pokemon_to_transfer = list( - Set(id_list2) - Set(id_list1)) - if len(pokemon_to_transfer) == 0: - raise RuntimeError( - 'Trying to transfer 0 pokemons!') - self.transfer_pokemon(pokemon_to_transfer[0]) - self.bot.metrics.released_pokemon() - logger.log( - '{} has been exchanged for candy!'.format(pokemon_name), 'green') + worker = PokemonTransferWorker(self.bot) + worker.release_catched_pokemon(pokemon_name, pokemon_to_transfer[0], cp, pokemon_potential) break time.sleep(5) - - def _transfer_low_cp_pokemon(self, value): - response_dict = self.bot.get_inventory() - self._transfer_all_low_cp_pokemon(value, response_dict) - - def _transfer_all_low_cp_pokemon(self, value, response_dict): - try: - reduce(dict.__getitem__, [ - "responses", "GET_INVENTORY", "inventory_delta", "inventory_items"], response_dict) - except KeyError: - pass - else: - for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']: - try: - reduce(dict.__getitem__, [ - "inventory_item_data", "pokemon"], item) - except KeyError: - pass - else: - pokemon = item['inventory_item_data']['pokemon'] - self._execute_pokemon_transfer(value, pokemon) - time.sleep(1.2) - - def _execute_pokemon_transfer(self, value, pokemon): - if 'cp' in pokemon and pokemon['cp'] < value: - self.api.release_pokemon(pokemon_id=pokemon['id']) - response_dict = self.api.call() - - def transfer_pokemon(self, pid): - self.api.release_pokemon(pokemon_id=pid) - response_dict = self.api.call() - def count_pokemon_inventory(self): self.bot.latest_inventory = None # Need accurate count of balls/berries/pokemons response_dict = self.bot.get_inventory() @@ -334,47 +295,6 @@ def _get_catch_config_for(self, pokemon): catch_config = self.config.catch.get('any') return catch_config - def should_release_pokemon(self, pokemon_name, cp, iv, response_dict): - release_config = self._get_release_config_for(pokemon_name) - cp_iv_logic = release_config.get('logic') - if not cp_iv_logic: - cp_iv_logic = self._get_release_config_for('any').get('logic', 'and') - - release_results = { - 'cp': False, - 'iv': False, - } - - if release_config.get('never_release', False): - return False - - if release_config.get('always_release', False): - return True - - release_cp = release_config.get('release_below_cp', 0) - if cp < release_cp: - release_results['cp'] = True - - release_iv = release_config.get('release_below_iv', 0) - if iv < release_iv: - release_results['iv'] = True - - logic_to_function = { - 'or': lambda x, y: x or y, - 'and': lambda x, y: x and y - } - - #logger.log( - # "Release config for {}: CP {} {} IV {}".format( - # pokemon_name, - # min_cp, - # cp_iv_logic, - # min_iv - # ), 'yellow' - #) - - return logic_to_function[cp_iv_logic](*release_results.values()) - def _get_release_config_for(self, pokemon): release_config = self.config.release.get(pokemon) if not release_config: diff --git a/pokemongo_bot/cell_workers/initial_transfer_worker.py b/pokemongo_bot/cell_workers/pokemon_transfer_worker.py similarity index 83% rename from pokemongo_bot/cell_workers/initial_transfer_worker.py rename to pokemongo_bot/cell_workers/pokemon_transfer_worker.py index 8755326d52..63ccceec1f 100644 --- a/pokemongo_bot/cell_workers/initial_transfer_worker.py +++ b/pokemongo_bot/cell_workers/pokemon_transfer_worker.py @@ -3,11 +3,12 @@ from pokemongo_bot.human_behaviour import sleep from pokemongo_bot import logger -class InitialTransferWorker(object): +class PokemonTransferWorker(object): def __init__(self, bot): self.config = bot.config self.pokemon_list = bot.pokemon_list self.api = bot.api + self.metrics = bot.metrics def work(self): if not self.config.initial_transfer: @@ -29,17 +30,22 @@ def work(self): pokemon_data = pokemon_groups[id][pokemon_cp] pokemon_potential = self.get_pokemon_potential(pokemon_data) if self.should_release_pokemon(pokemon_name, pokemon_cp, pokemon_potential): - message = 'Exchanging {} [CP {}] [Potential {}]'.format( - pokemon_name, - pokemon_cp, - pokemon_potential - ) - logger.log(message, 'red') - self.api.release_pokemon( - pokemon_id=pokemon_data['id']) - response_dict = self.api.call() + logger.log('Exchanging {} [CP {}] [Potential {}] for candy!'.format( + pokemon_name, pokemon_cp, pokemon_potential)) + self.transfer_pokemon(pokemon_data['id']) sleep(2) + def release_catched_pokemon(self, pokemon_name, pokemon_to_transfer, cp, iv): + # Transfering Pokemon + self.transfer_pokemon(pokemon_to_transfer) + self.metrics.released_pokemon() + logger.log( + '{} has been exchanged for candy!'.format(pokemon_name), 'green') + + def transfer_pokemon(self, pid): + self.api.release_pokemon(pokemon_id=pid) + response_dict = self.api.call() + def _initial_transfer_get_groups(self): pokemon_groups = {} self.api.get_player().get_inventory() @@ -75,7 +81,7 @@ def get_pokemon_potential(self, pokemon_data): for individual_stat in iv_stats: try: total_iv += pokemon_data[individual_stat] - except Exception: + except: continue return round((total_iv / 45.0), 2)