From 029df03b3d2b89f468d465e6e80d354c11cbf6ae Mon Sep 17 00:00:00 2001 From: Eli White Date: Fri, 29 Jul 2016 02:38:10 -0700 Subject: [PATCH] Refactoring function to get forts (#1578) * Refactoring function to get forts * Optionally sort by distance --- pokemongo_bot/__init__.py | 17 ++++++++- .../cell_workers/spin_nearest_fort_worker.py | 36 +++++++------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 19133cb9a6..7ca837e22c 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -90,7 +90,7 @@ def tick(self): return self.navigator.take_step() - + self.tick_count +=1 def get_meta_cell(self): @@ -561,3 +561,18 @@ def get_player_info(self): logger.log( 'Pokemon Captured: {pokemons_captured}'.format(**playerdata) + ' | Pokestops Visited: {poke_stop_visits}'.format(**playerdata), 'cyan') + + def get_forts(self, order_by_distance=False): + forts = [fort + for fort in self.cell['forts'] + if 'latitude' in fort and 'type' in fort] + + if order_by_distance: + forts.sort(key=lambda x: distance( + self.position[0], + self.position[1], + x['latitude'], + x['longitude'] + )) + + return forts diff --git a/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py b/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py index 42cb15d861..d03fcd0a46 100644 --- a/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py +++ b/pokemongo_bot/cell_workers/spin_nearest_fort_worker.py @@ -37,26 +37,16 @@ def should_run(self): 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) - - # Remove all forts which were spun in the last ticks to avoid circles if set - if self.config.avoid_circles: - forts = filter(lambda x: x["id"] not in self.recent_forts, 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 + forts = self.bot.get_forts() + + # Remove stops that are still on timeout + forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts) + + # Remove all forts which were spun in the last ticks to avoid circles if set + if self.config.avoid_circles: + forts = filter(lambda x: x["id"] not in self.recent_forts, forts) + + if len(forts) > 0: + return forts[0] + else: + return None