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

Fixed seperate transfer bug in worker #1286

Merged
merged 11 commits into from
Jul 28, 2016
Merged
2 changes: 1 addition & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"walk": 4.16,
"debug": false,
"test": false,
"initial_transfer": false,
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
Expand All @@ -26,6 +25,7 @@
"cp_min": 300,
"use_lucky_egg": false,
"evolve_captured": false,
"release_pokemons": true,
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
2 changes: 1 addition & 1 deletion configs/config.json.pokemons.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"walk": 4.16,
"debug": false,
"test": false,
"initial_transfer": false,
"location_cache": true,
"distance_unit": "km",
"item_filter": {
Expand All @@ -24,6 +23,7 @@
"evolve_speed": 20,
"use_lucky_egg": false,
"evolve_captured": false,
"release_pokemons": true,
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or" },

Expand Down
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
91 changes: 4 additions & 87 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pokemongo_bot import logger
from pokemongo_bot.human_behaviour import sleep


class PokemonCatchWorker(object):
BAG_FULL = 'bag_full'
NO_POKEBALLS = 'no_pokeballs'
Expand Down Expand Up @@ -192,6 +191,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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's because I don't have a lot of state on this, but why are we raising a runtime error? Shouldn't the bot just "return" in this case?

I don't think workers should cause the bot to exit

'Trying to transfer or evolve 0 pokemons!')

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

Expand All @@ -202,7 +205,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 @@ -214,52 +216,8 @@ def work(self):
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')

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 +292,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,14 +3,15 @@
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:
if not self.config.release_pokemons:
return

pokemon_groups = self._initial_transfer_get_groups()
Expand All @@ -29,14 +30,9 @@ 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'])
logger.log('Exchanging {} [CP {}] [Potential {}] for candy!'.format(
pokemon_name, pokemon_cp, pokemon_potential))
self.api.release_pokemon(pokemon_id=pokemon_data['id'])
response_dict = self.api.call()
sleep(2)

Expand Down Expand Up @@ -75,7 +71,7 @@ def get_pokemon_potential(self, pokemon_data):
for individual_stat in iv_stats:
try:
total_iv += pokemon_data[individual_stat]
except Exception:
except:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@surfaace just one last detail to be good to go for me: put back except Exception here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot

continue
return round((total_iv / 45.0), 2)

Expand Down