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

Revert "Removing duplicated release logic from catching pokemon" #1188

Merged
merged 1 commit into from
Jul 27, 2016
Merged
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
92 changes: 92 additions & 0 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,52 @@ 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):
self.api.get_inventory()
response_dict = self.api.call()
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.api.get_inventory()
Expand Down Expand Up @@ -286,3 +329,52 @@ def _get_catch_config_for(self, pokemon):
if not catch_config:
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:
release_config = self.config.release.get('any')
if not release_config:
release_config = {}
return release_config