Skip to content

Commit

Permalink
Modify pokemon_catch_worker.py to use Inventory class
Browse files Browse the repository at this point in the history
  • Loading branch information
cmezh committed Aug 11, 2016
1 parent d9bd39d commit 5b6e4d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, pokemon, bot):
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 = ''
Expand Down Expand Up @@ -259,16 +259,15 @@ 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.count_for(ITEM_RAZZBERRY)

while True:

# find lowest available ball
current_ball = ITEM_POKEBALL
while items_stock[current_ball] == 0 and current_ball < maximum_ball:
while self.inventory.count_for(current_ball) == 0 and current_ball < maximum_ball:
current_ball += 1
if items_stock[current_ball] == 0:
if self.inventory.count_for(current_ball) == 0:
self.emit_event('no_pokeballs', formatted='No usable pokeballs found!')
break

Expand All @@ -277,7 +276,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 += self.inventory.count_for(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
Expand All @@ -287,6 +286,7 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=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
self.inventory.decrement_count(ITEM_RAZZBERRY)
used_berry = True

# pick the best ball to catch with
Expand All @@ -301,16 +301,18 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
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
self.inventory.decrement_count(ITEM_RAZZBERRY)

# try to catch pokemon!
items_stock[current_ball] -= 1
ball_count = self.inventory.count_for(current_ball) - 1
self.inventory.update_local_count(current_ball, ball_count)
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)],
'success_percentage': self._pct(catch_rate_by_ball[current_ball]),
'count_left': items_stock[current_ball]
'count_left': ball_count
}
)

Expand Down
3 changes: 3 additions & 0 deletions pokemongo_bot/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class Items(_BaseInventoryComponent):
def count_for(self, item_id):
return self._data[item_id]['count']

def decrement_count(self, item_id):
self._data[item_id]["count"] -= 1


class Pokemons(_BaseInventoryComponent):
TYPE = 'pokemon_data'
Expand Down

0 comments on commit 5b6e4d3

Please sign in to comment.