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

Refactor: Moving evolve logic out of the stepper #639

Merged
merged 4 commits into from
Jul 24, 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
10 changes: 9 additions & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
import time
from pgoapi import PGoApi
from cell_workers import PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker
from cell_workers import PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker
from cell_workers.utils import distance
from human_behaviour import sleep
from stepper import Stepper
Expand All @@ -37,6 +37,14 @@ def take_step(self):
self.stepper.take_step()

def work_on_cell(self, cell, position, include_fort_on_path):
if self.config.evolve_all:
# Run evolve all once. Flip the bit.
print('[#] Attempting to evolve all pokemons ...')
self.config.lcd.message('Attempting to evolve all pokemons')
self.config.evolve_all = False
worker = EvolveAllWorker(self)
worker.work()

self._filter_ignored_pokemons(cell)

if (self.config.mode == "all" or self.config.mode ==
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 @@ -4,3 +4,4 @@
from seen_fort_worker import SeenFortWorker
from move_to_fort_worker import MoveToFortWorker
from initial_transfer_worker import InitialTransferWorker
from evolve_all_worker import EvolveAllWorker
53 changes: 53 additions & 0 deletions pokemongo_bot/cell_workers/evolve_all_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from utils import distance, format_dist
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot import logger

class EvolveAllWorker(object):
def __init__(self, bot):
self.api = bot.api
self.config = bot.config
# self.stepper = bot.stepper
# self.position = bot.position

def work(self):
self.api.get_inventory()
response_dict = self.api.call()
cache = {}

try:
reduce(dict.__getitem__, [
"responses", "GET_INVENTORY", "inventory_delta", "inventory_items"], response_dict)
except KeyError:
pass
else:
for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
try:
reduce(dict.__getitem__, [
"inventory_item_data", "pokemon_data"], item)
except KeyError:
pass
else:
try:
pokemon = item['inventory_item_data']['pokemon_data']
self._execute_pokemon_evolve(pokemon, cache)
sleep(1.2)
except:
pass

def _execute_pokemon_evolve(self, pokemon, cache):
pokemon_num = int(pokemon['pokemon_id']) - 1
pokemon_name = self.bot.pokemon_list[
int(pokemon_num)]['Name']
if pokemon_name in cache:
return

self.api.evolve_pokemon(pokemon_id=pokemon['id'])
response_dict = self.api.call()
status = response_dict['responses']['EVOLVE_POKEMON']['result']
if status == 1:
print('[#] Successfully evolved {}!'.format(
pokemon_name
))
else:
# cache pokemons we can't evolve. Less server calls
cache[pokemon_name] = 1
54 changes: 0 additions & 54 deletions pokemongo_bot/stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def __init__(self, bot):
self.origin_lon = self.bot.position[1]

def take_step(self):
if self.config.evolve_all:
# Run evolve all once. Flip the bit.
print('[#] Attempting to evolve all pokemons ...')
self.config.lcd.message('Attempting to evolve all pokemons')
self.config.evolve_all = False
self._evolve_all()

position = (self.origin_lat, self.origin_lon, 0.0)

self.api.set_position(*position)
Expand Down Expand Up @@ -164,50 +157,3 @@ def _encode(self, cellid):
output = []
encoder._VarintEncoder()(output.append, cellid)
return ''.join(output)

def _evolve_all(self):
self.api.get_inventory()
response_dict = self.api.call()
cache = {}

try:
reduce(dict.__getitem__, [
"responses", "GET_INVENTORY", "inventory_delta", "inventory_items"], response_dict)
except KeyError:
pass
else:
for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
try:
reduce(dict.__getitem__, [
"inventory_item_data", "pokemon_data"], item)
except KeyError:
pass
else:
try:
pokemon = item['inventory_item_data']['pokemon_data']
self._execute_pokemon_evolve(pokemon, cache)
time.sleep(1.2)
except:
pass

def _execute_pokemon_evolve(self, pokemon, cache):
pokemon_num = int(pokemon['pokemon_id']) - 1
pokemon_name = self.bot.pokemon_list[
int(pokemon_num)]['Name']
if pokemon_name in cache:
return

self.api.evolve_pokemon(pokemon_id=pokemon['id'])
response_dict = self.api.call()
status = response_dict['responses']['EVOLVE_POKEMON']['result']
if status == 1:
print('[#] Successfully evolved {}!'.format(
pokemon_name
))
if hasattr(self.config, 'lcd'):
self.config.lcd.message('Successfully evolved {}!'.format(
pokemon_name
))
else:
# cache pokemons we can't evolve. Less server calls
cache[pokemon_name] = 1