Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor EvolveAll and InitialTransfer workers #941

Merged
merged 3 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 8 additions & 37 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,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 @@ -272,10 +247,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
38 changes: 35 additions & 3 deletions pokemongo_bot/cell_workers/evolve_all_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 +29,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 +51,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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing the Item import in this module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!


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 +163,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 +275,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