Skip to content

Commit

Permalink
seperate worker for transfering pokemons to professor (#1281)
Browse files Browse the repository at this point in the history
pulled out initial transfer and transfer on catched pokemon to seperate
worker
  • Loading branch information
fredrik-hellmangroup authored and eggins committed Jul 27, 2016
1 parent f0a77f9 commit e44a13a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 101 deletions.
4 changes: 2 additions & 2 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, InitialTransferWorker, EvolveAllWorker, RecycleItemsWorker
from cell_workers import 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 Down Expand Up @@ -160,7 +160,7 @@ def work_on_cell(self, cell, position):
# Check if session token has expired
self.check_session(position)

worker = InitialTransferWorker(self)
worker = PokemonTransferWorker(self)
if worker.work() == WorkerResult.RUNNING:
return

Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pokemon_catch_worker import PokemonCatchWorker
from seen_fort_worker import SeenFortWorker
from move_to_fort_worker import MoveToFortWorker
from initial_transfer_worker import InitialTransferWorker
from pokemon_transfer_worker import PokemonTransferWorker
from evolve_all_worker import EvolveAllWorker
from catch_visible_pokmeon_worker import CatchVisiblePokemonWorker
from recycle_items_worker import RecycleItemsWorker
94 changes: 7 additions & 87 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pokemongo_bot import logger
from pokemongo_bot.human_behaviour import sleep

from pokemon_transfer_worker import PokemonTransferWorker

class PokemonCatchWorker(object):
BAG_FULL = 'bag_full'
Expand Down Expand Up @@ -192,6 +192,10 @@ def work(self):
if status is 1:

id_list2 = self.count_pokemon_inventory()
pokemon_to_transfer = list(Set(id_list2) - Set(id_list1))
if len(pokemon_to_transfer) == 0:
raise RuntimeError(
'Trying to transfer or evolve 0 pokemons!')

self.bot.metrics.captured_pokemon(pokemon_name, cp, iv_display, pokemon_potential)

Expand All @@ -202,7 +206,6 @@ def work(self):
), 'blue')

if self.config.evolve_captured:
pokemon_to_transfer = list(Set(id_list2) - Set(id_list1))
# No need to capture this even for metrics, player stats includes it.
self.api.evolve_pokemon(pokemon_id=pokemon_to_transfer[0])
response_dict = self.api.call()
Expand All @@ -213,53 +216,11 @@ def work(self):
else:
logger.log(
'Failed to evolve {}!'.format(pokemon_name))

if self.should_release_pokemon(pokemon_name, cp, pokemon_potential, response_dict):
# Transfering Pokemon
pokemon_to_transfer = list(
Set(id_list2) - Set(id_list1))
if len(pokemon_to_transfer) == 0:
raise RuntimeError(
'Trying to transfer 0 pokemons!')
self.transfer_pokemon(pokemon_to_transfer[0])
self.bot.metrics.released_pokemon()
logger.log(
'{} has been exchanged for candy!'.format(pokemon_name), 'green')
worker = PokemonTransferWorker(self.bot)
worker.release_catched_pokemon(pokemon_name, pokemon_to_transfer[0], cp, pokemon_potential)

break
time.sleep(5)

def _transfer_low_cp_pokemon(self, value):
response_dict = self.bot.get_inventory()
self._transfer_all_low_cp_pokemon(value, response_dict)

def _transfer_all_low_cp_pokemon(self, value, response_dict):
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"], item)
except KeyError:
pass
else:
pokemon = item['inventory_item_data']['pokemon']
self._execute_pokemon_transfer(value, pokemon)
time.sleep(1.2)

def _execute_pokemon_transfer(self, value, pokemon):
if 'cp' in pokemon and pokemon['cp'] < value:
self.api.release_pokemon(pokemon_id=pokemon['id'])
response_dict = self.api.call()

def transfer_pokemon(self, pid):
self.api.release_pokemon(pokemon_id=pid)
response_dict = self.api.call()

def count_pokemon_inventory(self):
self.bot.latest_inventory = None # Need accurate count of balls/berries/pokemons
response_dict = self.bot.get_inventory()
Expand Down Expand Up @@ -334,47 +295,6 @@ def _get_catch_config_for(self, pokemon):
catch_config = self.config.catch.get('any')
return catch_config

def should_release_pokemon(self, pokemon_name, cp, iv, response_dict):
release_config = self._get_release_config_for(pokemon_name)
cp_iv_logic = release_config.get('logic')
if not cp_iv_logic:
cp_iv_logic = self._get_release_config_for('any').get('logic', 'and')

release_results = {
'cp': False,
'iv': False,
}

if release_config.get('never_release', False):
return False

if release_config.get('always_release', False):
return True

release_cp = release_config.get('release_below_cp', 0)
if cp < release_cp:
release_results['cp'] = True

release_iv = release_config.get('release_below_iv', 0)
if iv < release_iv:
release_results['iv'] = True

logic_to_function = {
'or': lambda x, y: x or y,
'and': lambda x, y: x and y
}

#logger.log(
# "Release config for {}: CP {} {} IV {}".format(
# pokemon_name,
# min_cp,
# cp_iv_logic,
# min_iv
# ), 'yellow'
#)

return logic_to_function[cp_iv_logic](*release_results.values())

def _get_release_config_for(self, pokemon):
release_config = self.config.release.get(pokemon)
if not release_config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot import logger

class InitialTransferWorker(object):
class PokemonTransferWorker(object):
def __init__(self, bot):
self.config = bot.config
self.pokemon_list = bot.pokemon_list
self.api = bot.api
self.metrics = bot.metrics

def work(self):
if not self.config.initial_transfer:
Expand All @@ -29,17 +30,22 @@ def work(self):
pokemon_data = pokemon_groups[id][pokemon_cp]
pokemon_potential = self.get_pokemon_potential(pokemon_data)
if self.should_release_pokemon(pokemon_name, pokemon_cp, pokemon_potential):
message = 'Exchanging {} [CP {}] [Potential {}]'.format(
pokemon_name,
pokemon_cp,
pokemon_potential
)
logger.log(message, 'red')
self.api.release_pokemon(
pokemon_id=pokemon_data['id'])
response_dict = self.api.call()
logger.log('Exchanging {} [CP {}] [Potential {}] for candy!'.format(
pokemon_name, pokemon_cp, pokemon_potential))
self.transfer_pokemon(pokemon_data['id'])
sleep(2)

def release_catched_pokemon(self, pokemon_name, pokemon_to_transfer, cp, iv):
# Transfering Pokemon
self.transfer_pokemon(pokemon_to_transfer)
self.metrics.released_pokemon()
logger.log(
'{} has been exchanged for candy!'.format(pokemon_name), 'green')

def transfer_pokemon(self, pid):
self.api.release_pokemon(pokemon_id=pid)
response_dict = self.api.call()

def _initial_transfer_get_groups(self):
pokemon_groups = {}
self.api.get_player().get_inventory()
Expand Down Expand Up @@ -75,7 +81,7 @@ def get_pokemon_potential(self, pokemon_data):
for individual_stat in iv_stats:
try:
total_iv += pokemon_data[individual_stat]
except Exception:
except:
continue
return round((total_iv / 45.0), 2)

Expand Down

0 comments on commit e44a13a

Please sign in to comment.