From a58d43720610557e84cbf51cdca9fa2c69b46912 Mon Sep 17 00:00:00 2001 From: Saunier Debes Brice Date: Fri, 19 Aug 2016 03:52:56 +0200 Subject: [PATCH] Hotfix/remove old inventory (#4238) * Updated configuration documentation after #4005 * Updated configuration documentation of walk * Renamed id in unique_id Added functions to get pokemon inventory size * Removed variable used for test purpose * Computing candy awarded instead of taking only the first value * Reverted to use json again * Calling the api instead of the removed inventory * Fixed #4237 Not calling old inventory to get available lucky egg count --- pokemongo_bot/cell_workers/evolve_pokemon.py | 7 +++-- .../cell_workers/pokemon_optimizer.py | 30 +++++++++++-------- pokemongo_bot/inventory.py | 5 +--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pokemongo_bot/cell_workers/evolve_pokemon.py b/pokemongo_bot/cell_workers/evolve_pokemon.py index bb1f1d2b6f..995cce92de 100644 --- a/pokemongo_bot/cell_workers/evolve_pokemon.py +++ b/pokemongo_bot/cell_workers/evolve_pokemon.py @@ -46,19 +46,20 @@ def _should_run(self): if self.bot.tick_count is not 1 or not self.use_lucky_egg: return True - lucky_egg_count = self.bot.item_inventory_count(Item.ITEM_LUCKY_EGG.value) + lucky_egg = inventory.items().get(Item.ITEM_LUCKY_EGG.value) # Make sure the user has a lucky egg and skip if not - if lucky_egg_count > 0: + if lucky_egg.count > 0: response_dict_lucky_egg = self.bot.use_lucky_egg() if response_dict_lucky_egg: result = response_dict_lucky_egg.get('responses', {}).get('USE_ITEM_XP_BOOST', {}).get('result', 0) if result is 1: # Request success + lucky_egg.remove(1) self.emit_event( 'used_lucky_egg', formatted='Used lucky egg ({amount_left} left).', data={ - 'amount_left': lucky_egg_count - 1 + 'amount_left': lucky_egg.count } ) return True diff --git a/pokemongo_bot/cell_workers/pokemon_optimizer.py b/pokemongo_bot/cell_workers/pokemon_optimizer.py index 682c5ad007..fdb0ab0f95 100644 --- a/pokemongo_bot/cell_workers/pokemon_optimizer.py +++ b/pokemongo_bot/cell_workers/pokemon_optimizer.py @@ -22,7 +22,7 @@ class PokemonOptimizer(BaseTask): def initialize(self): self.family_by_family_id = {} - self.max_pokemon_storage = 0 + self.max_pokemon_storage = inventory.get_pokemon_inventory_size() self.last_pokemon_count = 0 self.config_transfer = self.config.get("transfer", False) @@ -40,14 +40,13 @@ def initialize(self): self.transfer_wait_max = self.config.get('transfer_wait_max', 4) def get_pokemon_slot_left(self): - pokemon_count = len(inventory.pokemons()._data) - self.max_pokemon_storage = self.bot.player_data["max_pokemon_storage"] + pokemon_count = inventory.Pokemons.get_space_used() if pokemon_count != self.last_pokemon_count: self.last_pokemon_count = pokemon_count self.logger.info("Pokemon Bag: %s/%s", pokemon_count, self.max_pokemon_storage) - return self.max_pokemon_storage - pokemon_count + return inventory.Pokemons.get_space_left() def work(self): if (not self.enabled) or (self.get_pokemon_slot_left() > 5): @@ -79,7 +78,7 @@ def work(self): def open_inventory(self): self.family_by_family_id.clear() - for pokemon in inventory.pokemons(True).all(): + for pokemon in inventory.pokemons().all(): family_id = pokemon.first_evolution_id setattr(pokemon, "ncp", pokemon.cp_percent) setattr(pokemon, "dps", pokemon.moveset.dps) @@ -89,7 +88,7 @@ def open_inventory(self): self.family_by_family_id.setdefault(family_id, []).append(pokemon) def save_web_inventory(self): - inventory_items = self.bot.get_inventory()["responses"]["GET_INVENTORY"]["inventory_delta"]["inventory_items"] + inventory_items = self.bot.api.get_inventory()["responses"]["GET_INVENTORY"]["inventory_delta"]["inventory_items"] web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username) with open(web_inventory, "w") as outfile: @@ -173,7 +172,7 @@ def get_pokemon_max_cp(self, pokemon_name): def unique_pokemons(self, l): seen = set() - return [p for p in l if not (p.id in seen or seen.add(p.id))] + return [p for p in l if not (p.unique_id in seen or seen.add(p.unique_id))] def get_evolution_plan(self, family_id, family, evolve_best, keep_best): candies = inventory.candies().get(family_id).quantity @@ -266,7 +265,7 @@ def apply_optimization(self, transfer, evo): def transfer_pokemon(self, pokemon): if self.config_transfer and (not self.bot.config.test): - response_dict = self.bot.api.release_pokemon(pokemon_id=pokemon.id) + response_dict = self.bot.api.release_pokemon(pokemon_id=pokemon.unique_id) else: response_dict = {"responses": {"RELEASE_POKEMON": {"candy_awarded": 0}}} @@ -282,15 +281,20 @@ def transfer_pokemon(self, pokemon): "dps": round(pokemon.dps, 2)}) if self.config_transfer and (not self.bot.config.test): - candy = response_dict.get("responses", {}).get("RELEASE_POKEMON", {}).get("candy_awarded", 0) - inventory.candies().get(pokemon.pokemon_id).add(candy) - inventory.pokemons().remove(pokemon.id) + inventory.candies().get(pokemon.pokemon_id).add(self.get_candy_gained_count(response_dict)) + inventory.pokemons().remove(pokemon.unique_id) action_delay(self.transfer_wait_min, self.transfer_wait_max) return True + def get_candy_gained_count(self, response_dict): + total_candy_gained = 0 + for candy_gained in response_dict['responses']['CATCH_POKEMON']['capture_award']['candy']: + total_candy_gained += candy_gained + return total_candy_gained + def use_lucky_egg(self): lucky_egg = inventory.items().get(Item.ITEM_LUCKY_EGG.value) # @UndefinedVariable @@ -327,7 +331,7 @@ def use_lucky_egg(self): def evolve_pokemon(self, pokemon): if self.config_evolve and (not self.bot.config.test): - response_dict = self.bot.api.evolve_pokemon(pokemon_id=pokemon.id) + response_dict = self.bot.api.evolve_pokemon(pokemon_id=pokemon.unique_id) else: response_dict = {"responses": {"EVOLVE_POKEMON": {"result": 1}}} @@ -354,7 +358,7 @@ def evolve_pokemon(self, pokemon): if self.config_evolve and (not self.bot.config.test): inventory.candies().get(pokemon.pokemon_id).consume(pokemon.evolution_cost - candy) - inventory.pokemons().remove(pokemon.id) + inventory.pokemons().remove(pokemon.unique_id) new_pokemon = inventory.Pokemon(evolution) inventory.pokemons().add(new_pokemon) diff --git a/pokemongo_bot/inventory.py b/pokemongo_bot/inventory.py index 2b92df647f..525c761317 100644 --- a/pokemongo_bot/inventory.py +++ b/pokemongo_bot/inventory.py @@ -1079,13 +1079,11 @@ def __init__(self, bot): self.candy = Candies() self.items = Items() self.pokemons = Pokemons() - self.refresh_count = 0 self.refresh() self.item_inventory_size = None self.pokemon_inventory_size = None def refresh(self): - self.refresh_count += 1 inventory = self.bot.api.get_inventory() inventory = inventory['responses']['GET_INVENTORY']['inventory_delta']['inventory_items'] for i in (self.pokedex, self.candy, self.items, self.pokemons): @@ -1104,13 +1102,12 @@ def retrieve_inventories_size(self): :return: Nothing. :rtype: None """ - # TODO: Force update of it if the player upgrades its size + # TODO: Force to update it if the player upgrades its size if self.item_inventory_size is None or self.pokemon_inventory_size is None: player_data = self.bot.api.get_player()['responses']['GET_PLAYER']['player_data'] self.item_inventory_size = player_data['max_item_storage'] self.pokemon_inventory_size = player_data['max_pokemon_storage'] - # # Other