Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 Refactoring code into a SpinNearestFortWorker #1351

Merged
merged 1 commit into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
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