diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 01a14fef13..e1b217d4b2 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -14,7 +14,7 @@ from pgoapi.utilities import f2i import logger -from cell_workers import CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker +from cell_workers import SpinNearestFortWorker, CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker from cell_workers.utils import distance, get_cellid, encode, i2f from human_behaviour import sleep from item_list import Item @@ -36,6 +36,7 @@ def __init__(self, config): self.item_list = json.load(open(os.path.join('data', 'items.json'))) self.metrics = Metrics(self) self.latest_inventory = None + self.cell = None def start(self): self._setup_logging() @@ -157,6 +158,8 @@ def find_close_cells(self, lat, lng): return map_cells def work_on_cell(self, cell, position): + self.cell = cell + # Check if session token has expired self.check_session(position) @@ -174,19 +177,8 @@ def work_on_cell(self, cell, position): if worker.work() == WorkerResult.RUNNING: return - - number_of_things_gained_by_stop = 5 - - if (self.config.spin_forts and - (self.get_inventory_count('item') < self._player['max_item_storage'] - number_of_things_gained_by_stop)): - nearest_fort = self.get_nearest_fort(cell) - - if nearest_fort: - # Move to and spin the nearest stop. - if MoveToFortWorker(nearest_fort, self).work() == WorkerResult.RUNNING: - return - if SeenFortWorker(nearest_fort, self).work() == WorkerResult.RUNNING: - return + if SpinNearestFortWorker(self).work() == WorkerResult.RUNNING: + return self.navigator.take_step() @@ -309,27 +301,6 @@ def _print_character_info(self): logger.log('') - def get_nearest_fort(self, cell): - if 'forts' in cell: - # Only include those with a lat/long - forts = [fort - for fort in cell['forts'] - if 'latitude' in fort and 'type' in fort] - gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] - - # Remove stops that are still on timeout - forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts) - - # Sort all by distance from current pos- eventually this should - # build graph & A* it - forts.sort(key=lambda x: distance(self.position[ - 0], self.position[1], x['latitude'], x['longitude'])) - - if len(forts) > 0: - return forts[0] - else: - return None - def use_lucky_egg(self): self.api.use_item_xp_boost(item_id=301) inventory_req = self.api.call() diff --git a/pokemongo_bot/cell_workers/__init__.py b/pokemongo_bot/cell_workers/__init__.py index ab69be4b2a..858f54d7c2 100644 --- a/pokemongo_bot/cell_workers/__init__.py +++ b/pokemongo_bot/cell_workers/__init__.py @@ -7,3 +7,4 @@ from evolve_all_worker import EvolveAllWorker from catch_visible_pokmeon_worker import CatchVisiblePokemonWorker from recycle_items_worker import RecycleItemsWorker +from spin_nearest_fort_worker import SpinNearestFortWorker diff --git a/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py b/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py new file mode 100644 index 0000000000..b241acf830 --- /dev/null +++ b/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py @@ -0,0 +1,57 @@ +from pokemongo_bot import logger +from pokemongo_bot.worker_result import WorkerResult +from pokemongo_bot.cell_workers import MoveToFortWorker, SeenFortWorker +from utils import distance + +class SpinNearestFortWorker(object): + + def __init__(self, bot): + self.bot = bot + self.config = bot.config + + self.cell = bot.cell + self.fort_timeouts = bot.fort_timeouts + self.position = bot.position + + def work(self): + if not self.should_run(): + return WorkerResult.SUCCESS + + nearest_fort = self.get_nearest_fort() + + if nearest_fort: + # Move to and spin the nearest stop. + if MoveToFortWorker(nearest_fort, self.bot).work() == WorkerResult.RUNNING: + return WorkerResult.RUNNING + if SeenFortWorker(nearest_fort, self.bot).work() == WorkerResult.RUNNING: + return WorkerResult.RUNNING + + return WorkerResult.SUCCESS + + def should_run(self): + number_of_things_gained_by_stop = 5 + + enough_space = self.bot.get_inventory_count('item') < self.bot._player['max_item_storage'] - number_of_things_gained_by_stop + + return self.config.spin_forts and enough_space + + def get_nearest_fort(self): + if 'forts' in self.cell: + # Only include those with a lat/long + forts = [fort + for fort in self.cell['forts'] + if 'latitude' in fort and 'type' in fort] + gyms = [gym for gym in self.cell['forts'] if 'gym_points' in gym] + + # Remove stops that are still on timeout + forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts) + + # Sort all by distance from current pos- eventually this should + # build graph & A* it + forts.sort(key=lambda x: distance(self.position[ + 0], self.position[1], x['latitude'], x['longitude'])) + + if len(forts) > 0: + return forts[0] + else: + return None \ No newline at end of file