Repository: PokemonGoF/PokemonGo-Bot

Issue:
Lucky egg broken on dev
###### ###### Problem

On dev branch pulled 5 mins ago
using lucky eggs are broken
###### ###### config

enabling evolve caused  the error
{
        "type": "EvolvePokemon",
        "config": {
            "evolve_all": "all",
            "first_evolve_by": "iv",
            "evolve_above_cp": 0,
            "evolve_above_iv": 0.8,
            "logic": "or",
            "evolve_speed": 20,
            "use_lucky_egg": true
        }
},
###### ### Exception:

Traceback (most recent call last):
  File "pokecli.py", line 595, in <module>
    main()
  File "pokecli.py", line 105, in main
    bot.tick()
  File "/home/nburn42/Projects/PokemonGo-Bot/pokemongo_bot/**init**.py", line 532, in tick
    if worker.work() == WorkerResult.RUNNING:
  File "/home/nburn42/Projects/PokemonGo-Bot/pokemongo_bot/cell_workers/evolve_pokemon.py", line 27, in work
    if not self._should_run():
  File "/home/nburn42/Projects/PokemonGo-Bot/pokemongo_bot/cell_workers/evolve_pokemon.py", line 49, in _should_run
    lucky_egg_count = self.bot.item_inventory_count(Item.ITEM_LUCKY_EGG.value)
AttributeError: 'PokemonGoBot' object has no attribute 'item_inventory_count'
2016-08-18 21:54:04,108 [sentry.errors] [ERROR] Sentry responded with an error: 'module' object has no attribute 'HTTPSHandler' (url: https://app.getsentry.com/api/90254/store/)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/raven/transport/threaded.py", line 174, in send_sync
    super(ThreadedHTTPTransport, self).send(data, headers)
  File "/usr/local/lib/python2.7/dist-packages/raven/transport/http.py", line 47, in send
    ca_certs=self.ca_certs,
  File "/usr/local/lib/python2.7/dist-packages/raven/utils/http.py", line 44, in urlopen
    class ValidHTTPSHandler(urllib2.HTTPSHandler):
AttributeError: 'module' object has no attribute 'HTTPSHandler'
2016-08-18 21:54:04,108 [sentry.errors.uncaught] [ERROR] [u"AttributeError: 'PokemonGoBot' object has no attribute 'item_inventory_count'", u'  File "pokecli.py", line 595, in <module>', u'  File "pokecli.py", line 105, in main', u'  File "pokemongo_bot/__init__.py", line 532, in tick', u'  File "pokemongo_bot/cell_workers/evolve_pokemon.py", line 27, in work', u'  File "pokemongo_bot/cell_workers/evolve_pokemon.py", line 49, in _should_run']
Thu Aug 18 21:54:04 EDT 2016 Pokebot  Stopped.
run.sh: 28: read: Illegal option -s
### Other Information

OS: 
ubuntu
Branch: 
dev 
Git Commit: 
3dfe476
Python Version: 
2.7

Thanks for taking the time I can't spare myself at the moment.



Discussion:

Resolving patch:
diff --git a/pokemongo_bot/cell_workers/evolve_pokemon.py b/pokemongo_bot/cell_workers/evolve_pokemon.py
--- 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
--- 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
--- 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
 


Test patch: