diff --git a/pokemongo_bot/cell_workers/catch_lured_pokemon.py b/pokemongo_bot/cell_workers/catch_lured_pokemon.py index 10a046dce9..187c1eb40d 100644 --- a/pokemongo_bot/cell_workers/catch_lured_pokemon.py +++ b/pokemongo_bot/cell_workers/catch_lured_pokemon.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from pokemongo_bot.cell_workers.utils import fort_details -from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker from pokemongo_bot.base_task import BaseTask +from pokemongo_bot.worker_result import WorkerResult +from pokemongo_bot.constants import Constants +from pokemongo_bot.cell_workers.utils import fort_details, distance +from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker class CatchLuredPokemon(BaseTask): @@ -11,24 +13,42 @@ class CatchLuredPokemon(BaseTask): def work(self): lured_pokemon = self.get_lured_pokemon() - if lured_pokemon: - self.catch_pokemon(lured_pokemon) + if len(lured_pokemon) > 0: + self.catch_pokemon(lured_pokemon[0]) + + if len(lured_pokemon) > 1: + return WorkerResult.RUNNING + + return WorkerResult.SUCCESS def get_lured_pokemon(self): + forts_in_range = [] + pokemon_to_catch = [] forts = self.bot.get_forts(order_by_distance=True) if len(forts) == 0: return False - fort = forts[0] - details = fort_details(self.bot, fort_id=fort['id'], - latitude=fort['latitude'], - longitude=fort['longitude']) - fort_name = details.get('name', 'Unknown') + for fort in forts: + distance_to_fort = distance( + self.bot.position[0], + self.bot.position[1], + fort['latitude'], + fort['longitude'] + ) + + encounter_id = fort.get('lure_info', {}).get('encounter_id', None) + if distance_to_fort < Constants.MAX_DISTANCE_FORT_IS_REACHABLE and encounter_id: + forts_in_range.append(fort) - encounter_id = fort.get('lure_info', {}).get('encounter_id', None) - if encounter_id: + for fort in forts_in_range: + details = fort_details(self.bot, fort_id=fort['id'], + latitude=fort['latitude'], + longitude=fort['longitude']) + fort_name = details.get('name', 'Unknown') + encounter_id = fort['lure_info']['encounter_id'] + result = { 'encounter_id': encounter_id, 'fort_id': fort['id'], @@ -36,15 +56,14 @@ def get_lured_pokemon(self): 'latitude': fort['latitude'], 'longitude': fort['longitude'] } + pokemon_to_catch.append(result) self.emit_event( 'lured_pokemon_found', formatted='Lured pokemon at fort {fort_name} ({fort_id})', data=result ) - return result - - return False + return pokemon_to_catch def catch_pokemon(self, pokemon): worker = PokemonCatchWorker(pokemon, self.bot)