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

Restart the loop when catching pokemon and there are more to catch #3242

Merged
merged 1 commit into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 24 additions & 4 deletions pokemongo_bot/cell_workers/catch_visible_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker
from utils import distance
from pokemongo_bot.worker_result import WorkerResult


class CatchVisiblePokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def work(self):
if 'catchable_pokemons' in self.bot.cell and len(self.bot.cell['catchable_pokemons']) > 0:
num_catchable_pokemon = 0
if 'catchable_pokemons' in self.bot.cell:
num_catchable_pokemon = len(self.bot.cell['catchable_pokemons'])

num_wild_pokemon = 0
if 'wild_pokemons' in self.bot.cell:
num_wild_pokemon = len(self.bot.cell['wild_pokemons'])

num_available_pokemon = num_catchable_pokemon + num_wild_pokemon

if num_catchable_pokemon > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['catchable_pokemons'].sort(
Expand All @@ -33,15 +44,24 @@ def work(self):
}
)

return self.catch_pokemon(self.bot.cell['catchable_pokemons'].pop(0))
self.catch_pokemon(self.bot.cell['catchable_pokemons'].pop(0))
if num_catchable_pokemon > 1:
return WorkerResult.RUNNING
else:
return WorkerResult.SUCCESS

if 'wild_pokemons' in self.bot.cell and len(self.bot.cell['wild_pokemons']) > 0:
if num_available_pokemon > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['wild_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']))
return self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0))
self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0))

if num_catchable_pokemon > 1:
return WorkerResult.RUNNING
else:
return WorkerResult.SUCCESS

def catch_pokemon(self, pokemon):
worker = PokemonCatchWorker(pokemon, self.bot)
Expand Down
10 changes: 5 additions & 5 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pokemongo_bot import inventory
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.human_behaviour import normalized_reticle_size, sleep, spin_modifier

from pokemongo_bot.worker_result import WorkerResult

CATCH_STATUS_SUCCESS = 1
CATCH_STATUS_FAILED = 2
Expand Down Expand Up @@ -68,7 +68,7 @@ def work(self, response_dict=None):

# validate response
if not response_dict:
return False
return WorkerResult.ERROR
try:
responses = response_dict['responses']
response = responses[self.response_key]
Expand All @@ -77,17 +77,17 @@ def work(self, response_dict=None):
self.emit_event('pokemon_not_in_range', formatted='Pokemon went out of range!')
elif response[self.response_status_key] == ENCOUNTER_STATUS_POKEMON_INVENTORY_FULL:
self.emit_event('pokemon_inventory_full', formatted='Your Pokemon inventory is full! Could not catch!')
return False
return WorkerResult.ERROR
except KeyError:
return False
return WorkerResult.ERROR

# get pokemon data
pokemon_data = response['wild_pokemon']['pokemon_data'] if 'wild_pokemon' in response else response['pokemon_data']
pokemon = Pokemon(self.pokemon_list, pokemon_data)

# skip ignored pokemon
if not self._should_catch_pokemon(pokemon):
return False
return WorkerResult.SUCCESS

# log encounter
self.emit_event(
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/worker_result.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class WorkerResult(object):
RUNNING = 'RUNNING'
SUCCESS = 'SUCCESS'
ERROR = 'ERROR'