Skip to content

Commit

Permalink
Refactoring code into a SpinNearestFortWorker (PokemonGoF#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
elicwhite authored and MFizz committed Jul 29, 2016
1 parent 94f9e8f commit 1f9d3d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 57 additions & 0 deletions pokemongo_bot/cell_workers/spin_nearest_fort_worker.py
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

0 comments on commit 1f9d3d1

Please sign in to comment.