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

Extract CatchLuredPokemonWorker from PokemonCatchWorker and improved worker order #1627

Merged
merged 2 commits into from
Jul 29, 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
9 changes: 5 additions & 4 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class PokemonGoBot(object):
cell_workers.EvolveAllWorker,
cell_workers.RecycleItemsWorker,
cell_workers.CatchVisiblePokemonWorker,
cell_workers.SeenFortWorker,
cell_workers.MoveToFortWorker
cell_workers.MoveToFortWorker,
cell_workers.CatchLuredPokemonWorker,
cell_workers.SeenFortWorker
]

@property
Expand Down Expand Up @@ -83,7 +84,9 @@ def _setup_event_system(self):
# self.event_manager.emit('location', 'level'='info', data={'lat': 1, 'lng':1}),

def tick(self):
logger.log('')
self.cell = self.get_meta_cell()
self.tick_count +=1

# Check if session token has expired
self.check_session(self.position[0:2])
Expand All @@ -94,8 +97,6 @@ def tick(self):

self.navigator.take_step()

self.tick_count +=1

def get_meta_cell(self):
location = self.position[0:2]
cells = self.find_close_cells(*location)
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 @@ -8,3 +8,4 @@
from catch_visible_pokemon_worker import CatchVisiblePokemonWorker
from recycle_items_worker import RecycleItemsWorker
from incubate_eggs_worker import IncubateEggsWorker
from catch_lured_pokemon_worker import CatchLuredPokemonWorker
57 changes: 57 additions & 0 deletions pokemongo_bot/cell_workers/catch_lured_pokemon_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import json
from utils import distance, format_dist, i2f
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot import logger
from pokemongo_bot.step_walker import StepWalker
from pokemongo_bot.cell_workers import PokemonCatchWorker


class CatchLuredPokemonWorker(object):
def __init__(self, bot):
self.bot = bot
self.cell = bot.cell;
self.api = bot.api
self.config = bot.config
self.position = bot.position

def work(self):
if not self.config.catch_pokemon:
return

lured_pokemon = self.get_lured_pokemon()
if lured_pokemon:
self.catch_pokemon(lured_pokemon)

def get_lured_pokemon(self):
forts = self.bot.get_forts(order_by_distance=True)

if len(forts) == 0:
return False

fort = forts[0]

self.api.fort_details(fort_id=fort['id'],
latitude=fort['latitude'],
longitude=fort['longitude'])

response_dict = self.api.call()
fort_details = response_dict.get('responses', {}).get('FORT_DETAILS', {})
fort_name = fort_details.get('name', 'Unknown').encode('utf8', 'replace')

encounter_id = fort.get('lure_info', {}).get('encounter_id', None)

pokemon = {
'encounter_id': encounter_id,
'fort_id': fort['id'],
'latitude': fort['latitude'],
'longitude': fort['longitude']
}

return pokemon

def catch_pokemon(self, pokemon):
worker = PokemonCatchWorker(pokemon, self.bot)
return_value = worker.work()

return return_value
28 changes: 1 addition & 27 deletions pokemongo_bot/cell_workers/catch_visible_pokemon_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pokemongo_bot.step_walker import StepWalker
from pokemongo_bot.cell_workers import PokemonCatchWorker


class CatchVisiblePokemonWorker(object):
def __init__(self, bot):
self.bot = bot
Expand Down Expand Up @@ -44,33 +45,6 @@ def work(self):
lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude']))
return self.catch_pokemon(self.cell['wild_pokemons'][0])

lured_pokemon = self.get_lured_pokemon()
if lured_pokemon:
self.catch_pokemon(lured_pokemon)

def get_lured_pokemon(self):
forts = self.bot.get_forts(order_by_distance=True)
fort = forts[0]

self.api.fort_details(fort_id=fort['id'],
latitude=fort['latitude'],
longitude=fort['longitude'])

response_dict = self.api.call()
fort_details = response_dict.get('responses', {}).get('FORT_DETAILS', {})
fort_name = fort_details.get('name', 'Unknown').encode('utf8', 'replace')

encounter_id = fort.get('lure_info', {}).get('encounter_id', None)

pokemon = {
'encounter_id': encounter_id,
'fort_id': fort['id'],
'latitude': fort['latitude'],
'longitude': fort['longitude']
}

return pokemon

def catch_pokemon(self, pokemon):
worker = PokemonCatchWorker(pokemon, self.bot)
return_value = worker.work()
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/move_to_fort_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def work(self):
if not step_walker.step():
return WorkerResult.RUNNING

logger.log('Arrived at Pokestop')
logger.log('Arrived at pokestop.')
return WorkerResult.SUCCESS

def get_nearest_fort(self):
Expand Down
5 changes: 0 additions & 5 deletions pokemongo_bot/spiral_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ def take_step(self):
point = self.points[self.ptr]
self.cnt += 1

if self.cnt == 1:
logger.log('Scanning area for objects....')

# Scan location math

if self.config.walk > 0:
step_walker = StepWalker(
self.bot,
Expand Down
2 changes: 0 additions & 2 deletions pokemongo_bot/step_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def __init__(self, bot, speed, destLat, destLng):

def step(self):
if (self.dLat == 0 and self.dLng == 0) or self.dist < self.speed:
if sys.stdout.isatty():
sys.stdout.write('\n')
self.api.set_position(self.destLat, self.destLng, 0)
return True

Expand Down