Skip to content

Commit

Permalink
Refactor EvolveAll and InitialTransfer workers (PokemonGoF#941)
Browse files Browse the repository at this point in the history
* Refactor EvolveAll and InitialTransfer workers

* Fixing Item import
  • Loading branch information
TheSavior authored and MFizz committed Jul 29, 2016
1 parent 3d21009 commit 8e37b98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
45 changes: 8 additions & 37 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,42 +131,17 @@ def work_on_cell(self, cell, position):
# Check if session token has expired
self.check_session(position)

if self.config.initial_transfer:
worker = InitialTransferWorker(self)
worker.work()
self.config.initial_transfer = False

if self.config.evolve_all:
# Will skip evolving if user wants to use an egg and there is none
skip_evolves = False

# Pop lucky egg before evolving to maximize xp gain
use_lucky_egg = self.config.use_lucky_egg
lucky_egg_count = self.item_inventory_count(Item.ITEM_LUCKY_EGG.value)

if use_lucky_egg and lucky_egg_count > 0:
logger.log('Using lucky egg ... you have {}'
.format(lucky_egg_count))
response_dict_lucky_egg = self.use_lucky_egg()
if response_dict_lucky_egg and 'responses' in response_dict_lucky_egg and \
'USE_ITEM_XP_BOOST' in response_dict_lucky_egg['responses'] and \
'result' in response_dict_lucky_egg['responses']['USE_ITEM_XP_BOOST']:
result = response_dict_lucky_egg['responses']['USE_ITEM_XP_BOOST']['result']
if result is 1: # Request success
logger.log('Successfully used lucky egg... ({} left!)'
.format(lucky_egg_count-1), 'green')
else:
logger.log('Failed to use lucky egg!', 'red')
skip_evolves = True
elif use_lucky_egg: #lucky_egg_count is 0
# Skipping evolve so they aren't wasted
logger.log('No lucky eggs... skipping evolve!', 'yellow')
skip_evolves = True

if not skip_evolves:
# Run evolve all once.
logger.log('Attempting to evolve all pokemons ...', 'cyan')
worker = EvolveAllWorker(self)
worker.work()

# Flip the bit.
worker = EvolveAllWorker(self)
worker.work()
self.config.evolve_all = []


if (self.config.mode == "all" or self.config.mode ==
"poke") and 'catchable_pokemons' in cell and len(cell[
'catchable_pokemons']) > 0:
Expand Down Expand Up @@ -282,10 +257,6 @@ def _setup_api(self):

self._print_character_info()

if self.config.initial_transfer:
worker = InitialTransferWorker(self)
worker.work()

logger.log('')
self.update_inventory()
# send empty map_cells and then our position
Expand Down
39 changes: 36 additions & 3 deletions pokemongo_bot/cell_workers/evolve_all_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pokemongo_bot import logger
from pokemongo_bot.human_behaviour import sleep
from item_list import Item


class EvolveAllWorker(object):
Expand All @@ -12,6 +13,9 @@ def __init__(self, bot):
# self.position = bot.position

def work(self):
if not self._should_run():
return

self.api.get_inventory()
response_dict = self.api.call()
cache = {}
Expand All @@ -26,7 +30,7 @@ def work(self):
if self.config.evolve_all[0] != 'all':
# filter out non-listed pokemons
evolve_list = [x for x in evolve_list if str(x[1]) in self.config.evolve_all]

# enable to limit number of pokemons to evolve. Useful for testing.
# nn = 3
# if len(evolve_list) > nn:
Expand All @@ -48,6 +52,35 @@ def work(self):
))
self._release_evolved(release_cand_list_ids)

def _should_run(self):
# Will skip evolving if user wants to use an egg and there is none
skip_evolves = False

# Pop lucky egg before evolving to maximize xp gain
use_lucky_egg = self.config.use_lucky_egg
lucky_egg_count = self.bot.item_inventory_count(Item.ITEM_LUCKY_EGG.value)

if use_lucky_egg and lucky_egg_count > 0:
logger.log('Using lucky egg ... you have {}'
.format(lucky_egg_count))
response_dict_lucky_egg = self.bot.use_lucky_egg()
if response_dict_lucky_egg and 'responses' in response_dict_lucky_egg and \
'USE_ITEM_XP_BOOST' in response_dict_lucky_egg['responses'] and \
'result' in response_dict_lucky_egg['responses']['USE_ITEM_XP_BOOST']:
result = response_dict_lucky_egg['responses']['USE_ITEM_XP_BOOST']['result']
if result is 1: # Request success
logger.log('Successfully used lucky egg... ({} left!)'
.format(lucky_egg_count-1), 'green')
else:
logger.log('Failed to use lucky egg!', 'red')
skip_evolves = True
elif use_lucky_egg: #lucky_egg_count is 0
# Skipping evolve so they aren't wasted
logger.log('No lucky eggs... skipping evolve!', 'yellow')
skip_evolves = True

return skip_evolves

def _release_evolved(self, release_cand_list_ids):
self.api.get_inventory()
response_dict = self.api.call()
Expand Down Expand Up @@ -131,7 +164,7 @@ def _execute_pokemon_evolve(self, pokemon, cache):
# cache pokemons we can't evolve. Less server calls
cache[pokemon_name] = 1
sleep(0.7)


# TODO: move to utils. These methods are shared with other workers.
def transfer_pokemon(self, pid):
Expand Down Expand Up @@ -243,7 +276,7 @@ def _check_always_capture_exception_for(self, pokemon_name):
def _compute_iv(self, pokemon):
total_IV = 0.0
iv_stats = ['individual_attack', 'individual_defense', 'individual_stamina']

for individual_stat in iv_stats:
try:
total_IV += pokemon[individual_stat]
Expand Down

0 comments on commit 8e37b98

Please sign in to comment.