Repository: PokemonGoF/PokemonGo-Bot

Issue:
Berries and balls count not always updated
3 berry throws and always "have 2". Last one with error.
![screenshot_2016-08-10-14-45-58](https://cloud.githubusercontent.com/assets/20733365/17546029/4b1c0876-5f0a-11e6-9d80-6d007930e96e.png)

One more haopening
![screenshot_2016-08-10-14-58-19](https://cloud.githubusercontent.com/assets/20733365/17546154/eb8f347c-5f0a-11e6-9efa-7951c6667646.png)

Same thing happens with Ultraballs. Probably, not only with Ultra- ones.
![screenshot_2016-08-10-16-42-16](https://cloud.githubusercontent.com/assets/20733365/17549281/add2d31e-5f19-11e6-8fe1-2014eacbc4a0.png)

Berries and balls count not always updated
3 berry throws and always "have 2". Last one with error.
![screenshot_2016-08-10-14-45-58](https://cloud.githubusercontent.com/assets/20733365/17546029/4b1c0876-5f0a-11e6-9d80-6d007930e96e.png)

One more haopening
![screenshot_2016-08-10-14-58-19](https://cloud.githubusercontent.com/assets/20733365/17546154/eb8f347c-5f0a-11e6-9efa-7951c6667646.png)

Same thing happens with Ultraballs. Probably, not only with Ultra- ones.
![screenshot_2016-08-10-16-42-16](https://cloud.githubusercontent.com/assets/20733365/17549281/add2d31e-5f19-11e6-8fe1-2014eacbc4a0.png)



Discussion:
This looks like you may be on an older commit, which are you using?

Almost latest
01bc14de0543196de6688dc6bd560739db59fd14
Nothing related to this issue was changed since this commit was merged to
dev.

10 авг. 2016 г. 19:02 пользователь "Ph4i1ur3" notifications@github.com
написал:

> This looks like you may be on an older commit, which are you using?
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> https://github.com/PokemonGoF/PokemonGo-Bot/issues/3411#issuecomment-238846016,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/ATxdtfTZAe_wkjXX3X5iNO7830srf3X1ks5qeb2-gaJpZM4Jg363
> .

Update: it happens always when catching several pokemons in one turn of task, even if catch completes successfully.
![screenshot_2016-08-10-23-17-33](https://cloud.githubusercontent.com/assets/20733365/17561621/20a55754-5f51-11e6-89f1-947593591522.png)

This looks like you may be on an older commit, which are you using?

Almost latest
01bc14de0543196de6688dc6bd560739db59fd14
Nothing related to this issue was changed since this commit was merged to
dev.

10 авг. 2016 г. 19:02 пользователь "Ph4i1ur3" notifications@github.com
написал:

> This looks like you may be on an older commit, which are you using?
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> https://github.com/PokemonGoF/PokemonGo-Bot/issues/3411#issuecomment-238846016,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/ATxdtfTZAe_wkjXX3X5iNO7830srf3X1ks5qeb2-gaJpZM4Jg363
> .

Update: it happens always when catching several pokemons in one turn of task, even if catch completes successfully.
![screenshot_2016-08-10-23-17-33](https://cloud.githubusercontent.com/assets/20733365/17561621/20a55754-5f51-11e6-89f1-947593591522.png)


Resolving patch:
diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py
--- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py
+++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py
@@ -54,8 +54,7 @@ def __init__(self, pokemon, bot):
         self.position = bot.position
         self.config = bot.config
         self.pokemon_list = bot.pokemon_list
-        self.item_list = bot.item_list
-        self.inventory = bot.inventory
+        self.inventory = inventory.items()
         self.spawn_point_guid = ''
         self.response_key = ''
         self.response_status_key = ''
@@ -204,8 +203,8 @@ def _use_berry(self, berry_id, berry_count, encounter_id, catch_rate_by_ball, cu
             formatted='Catch rate of {catch_rate} with {ball_name} is low. Throwing {berry_name} (have {berry_count})',
             data={
                 'catch_rate': self._pct(catch_rate_by_ball[current_ball]),
-                'ball_name': self.item_list[str(current_ball)],
-                'berry_name': self.item_list[str(berry_id)],
+                'ball_name': self.inventory.get(current_ball).name,
+                'berry_name': self.inventory.get(berry_id).name,
                 'berry_count': berry_count
             }
         )
@@ -227,8 +226,8 @@ def _use_berry(self, berry_id, berry_count, encounter_id, catch_rate_by_ball, cu
                     'threw_berry',
                     formatted="Threw a {berry_name}! Catch rate with {ball_name} is now: {new_catch_rate}",
                     data={
-                        'berry_name': self.item_list[str(berry_id)],
-                        'ball_name': self.item_list[str(current_ball)],
+                        'berry_name': self.inventory.get(berry_id).name,
+                        'ball_name': self.inventory.get(current_ball).name,
                         'new_catch_rate': self._pct(new_catch_rate_by_ball[current_ball])
                     }
                 )
@@ -261,16 +260,18 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
         maximum_ball = ITEM_ULTRABALL if is_vip else ITEM_GREATBALL
         ideal_catch_rate_before_throw = 0.9 if is_vip else 0.35
 
-        berry_count = self.bot.item_inventory_count(berry_id)
-        items_stock = self.bot.current_inventory()
+        berry_count = self.inventory.get(ITEM_RAZZBERRY).count
+        ball_count = {}
+        for ball_id in [ITEM_POKEBALL, ITEM_GREATBALL, ITEM_ULTRABALL]:
+            ball_count[ball_id] = self.inventory.get(ball_id).count
 
         while True:
 
             # find lowest available ball
             current_ball = ITEM_POKEBALL
-            while items_stock[current_ball] == 0 and current_ball < maximum_ball:
+            while ball_count[current_ball] == 0 and current_ball < maximum_ball:
                 current_ball += 1
-            if items_stock[current_ball] == 0:
+            if ball_count[current_ball] == 0:
                 self.emit_event('no_pokeballs', formatted='No usable pokeballs found!')
                 break
 
@@ -279,7 +280,7 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
             next_ball = current_ball
             while next_ball < maximum_ball:
                 next_ball += 1
-                num_next_balls += items_stock[next_ball]
+                num_next_balls += ball_count[next_ball]
 
             # check if we've got berries to spare
             berries_to_spare = berry_count > 0 if is_vip else berry_count > num_next_balls + 30
@@ -287,23 +288,29 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
             # use a berry if we are under our ideal rate and have berries to spare
             used_berry = False
             if catch_rate_by_ball[current_ball] < ideal_catch_rate_before_throw and berries_to_spare:
-                catch_rate_by_ball = self._use_berry(berry_id, berry_count, encounter_id, catch_rate_by_ball, current_ball)
-                berry_count -= 1
-                used_berry = True
+                new_catch_rate_by_ball = self._use_berry(berry_id, berry_count, encounter_id, catch_rate_by_ball, current_ball)
+                if new_catch_rate_by_ball != catch_rate_by_ball:
+                    catch_rate_by_ball = new_catch_rate_by_ball
+                    self.inventory.get(ITEM_RAZZBERRY).remove(1)
+                    berry_count -= 1
+                    used_berry = True
 
             # pick the best ball to catch with
             best_ball = current_ball
             while best_ball < maximum_ball:
                 best_ball += 1
-                if catch_rate_by_ball[current_ball] < ideal_catch_rate_before_throw and items_stock[best_ball] > 0:
+                if catch_rate_by_ball[current_ball] < ideal_catch_rate_before_throw and ball_count[best_ball] > 0:
                     # if current ball chance to catch is under our ideal rate, and player has better ball - then use it
                     current_ball = best_ball
 
             # if the rate is still low and we didn't throw a berry before, throw one
             if catch_rate_by_ball[current_ball] < ideal_catch_rate_before_throw and berry_count > 0 and not used_berry:
-                catch_rate_by_ball = self._use_berry(berry_id, berry_count, encounter_id, catch_rate_by_ball, current_ball)
-                berry_count -= 1
-            
+                new_catch_rate_by_ball = self._use_berry(berry_id, berry_count, encounter_id, catch_rate_by_ball, current_ball)
+                if new_catch_rate_by_ball != catch_rate_by_ball:
+                    catch_rate_by_ball = new_catch_rate_by_ball
+                    self.inventory.get(ITEM_RAZZBERRY).remove(1)
+                    berry_count -= 1
+
             # Randomize the quality of the throw
             # Default structure
             throw_parameters = {'normalized_reticle_size': 1.950,
@@ -315,14 +322,15 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
 
             # try to catch pokemon!
             # TODO : Log which type of throw we selected
-            items_stock[current_ball] -= 1
+            ball_count[current_ball] -= 1
+            self.inventory.get(current_ball).remove(1)
             self.emit_event(
                 'threw_pokeball',
                 formatted='Used {ball_name}, with chance {success_percentage} ({count_left} left)',
                 data={
-                    'ball_name': self.item_list[str(current_ball)],
+                    'ball_name': self.inventory.get(current_ball).name,
                     'success_percentage': self._pct(catch_rate_by_ball[current_ball]),
-                    'count_left': items_stock[current_ball]
+                    'count_left': ball_count[current_ball]
                 }
             )
 


Test patch: