Skip to content

Commit

Permalink
Extract CatchLuredPokemonWorker from PokemonCatchWorker and improved …
Browse files Browse the repository at this point in the history
…worker order (#1627)

* extracted lure catch worker from pokemon catch

* removing information less logs
  • Loading branch information
douglascamata authored Jul 29, 2016
1 parent 9bc0abe commit 4a8bde0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 39 deletions.
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('')

This comment has been minimized.

Copy link
@1cu

1cu Jul 29, 2016

Creates an unnecessary blank log line every time the bot walks.

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

0 comments on commit 4a8bde0

Please sign in to comment.