forked from PokemonGoF/PokemonGo-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring code into a SpinNearestFortWorker (PokemonGoF#1351)
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |