Skip to content

Commit

Permalink
Add and Remove pokemon from the inventory cache when catch, release a…
Browse files Browse the repository at this point in the history
…nd evolve (#3738)

* Add and Remove pokemon from the inventory cache when catch and release

* Add dealing with evolved pokemon also

* Add the evolved pokemon
  • Loading branch information
achretien authored and douglascamata committed Aug 12, 2016
1 parent 59d0865 commit 76587de
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
7 changes: 6 additions & 1 deletion pokemongo_bot/cell_workers/evolve_pokemon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pokemongo_bot import inventory
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot.inventory import Pokemon
from pokemongo_bot.item_list import Item
from pokemongo_bot.base_task import BaseTask

Expand Down Expand Up @@ -110,7 +111,11 @@ def _execute_pokemon_evolve(self, pokemon, cache):
'xp': 0
}
)
inventory.candies().get(pokemon.pokemon_id).consume(pokemon.evolution_cost)
awarded_candies = response_dict.get('responses', {}).get('EVOLVE_POKEMON', {}).get('candy_awarded', 0)
inventory.candies().get(pokemon.pokemon_id).consume(pokemon.evolution_cost - awarded_candies)
inventory.pokemons().remove(pokemon.id)
pokemon = Pokemon(response_dict.get('responses', {}).get('EVOLVE_POKEMON', {}).get('evolved_pokemon_data', {}))
inventory.pokemons().add(pokemon)
sleep(self.evolve_speed)
return True
else:
Expand Down
34 changes: 11 additions & 23 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pokemongo_bot import inventory
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot.inventory import Pokemon
from pokemongo_bot.worker_result import WorkerResult

CATCH_STATUS_SUCCESS = 1
Expand All @@ -26,25 +27,6 @@
}


class Pokemon(object):

def __init__(self, pokemon_list, pokemon_data):
self.num = int(pokemon_data['pokemon_id'])
self.name = pokemon_list[int(self.num) - 1]['Name']
self.cp = pokemon_data['cp']
self.attack = pokemon_data.get('individual_attack', 0)
self.defense = pokemon_data.get('individual_defense', 0)
self.stamina = pokemon_data.get('individual_stamina', 0)

@property
def iv(self):
return round((self.attack + self.defense + self.stamina) / 45.0, 2)

@property
def iv_display(self):
return '{}/{}/{}'.format(self.attack, self.defense, self.stamina)


class PokemonCatchWorker(BaseTask):

def __init__(self, pokemon, bot):
Expand Down Expand Up @@ -84,7 +66,7 @@ def work(self, response_dict=None):

# get pokemon data
pokemon_data = response['wild_pokemon']['pokemon_data'] if 'wild_pokemon' in response else response['pokemon_data']
pokemon = Pokemon(self.pokemon_list, pokemon_data)
pokemon = Pokemon(pokemon_data)

# skip ignored pokemon
if not self._should_catch_pokemon(pokemon):
Expand All @@ -102,7 +84,7 @@ def work(self, response_dict=None):
'encounter_id': self.pokemon['encounter_id'],
'latitude': self.pokemon['latitude'],
'longitude': self.pokemon['longitude'],
'pokemon_id': pokemon.num
'pokemon_id': pokemon.pokemon_id
}
)

Expand Down Expand Up @@ -256,6 +238,10 @@ def _use_berry(self, berry_id, berry_count, encounter_id, catch_rate_by_ball, cu

def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
# settings that may be exposed at some point
"""
:type pokemon: Pokemon
"""
berry_id = ITEM_RAZZBERRY
maximum_ball = ITEM_ULTRABALL if is_vip else ITEM_GREATBALL
ideal_catch_rate_before_throw = 0.9 if is_vip else 0.35
Expand Down Expand Up @@ -389,7 +375,9 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):

# pokemon caught!
elif catch_pokemon_status == CATCH_STATUS_SUCCESS:
pokemon.id = response_dict['responses']['CATCH_POKEMON']['captured_pokemon_id']
self.bot.metrics.captured_pokemon(pokemon.name, pokemon.cp, pokemon.iv_display, pokemon.iv)
inventory.pokemons().add(pokemon)
self.emit_event(
'pokemon_caught',
formatted='Captured {pokemon}! [CP {cp}] [Potential {iv}] [{iv_display}] [+{exp} exp]',
Expand All @@ -402,12 +390,12 @@ def _do_catch(self, pokemon, encounter_id, catch_rate_by_ball, is_vip=False):
'encounter_id': self.pokemon['encounter_id'],
'latitude': self.pokemon['latitude'],
'longitude': self.pokemon['longitude'],
'pokemon_id': pokemon.num
'pokemon_id': pokemon.pokemon_id
}
)

# We could refresh here too, but adding 3 saves a inventory request
candy = inventory.candies(True).get(pokemon.num)
candy = inventory.candies(True).get(pokemon.pokemon_id)
self.emit_event(
'gained_candy',
formatted='You now have {quantity} {type} candy!',
Expand Down
8 changes: 7 additions & 1 deletion pokemongo_bot/cell_workers/transfer_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pokemongo_bot import inventory
from pokemongo_bot.human_behaviour import action_delay
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.inventory import Pokemons
from pokemongo_bot.inventory import Pokemons, Pokemon


class TransferPokemon(BaseTask):
Expand Down Expand Up @@ -66,6 +66,7 @@ def work(self):

def _release_pokemon_get_groups(self):
pokemon_groups = {}
# TODO: Use new inventory everywhere and then remove the inventory update
for pokemon in inventory.pokemons(True).all():
if pokemon.in_fort or pokemon.is_favorite:
continue
Expand Down Expand Up @@ -134,6 +135,10 @@ def should_release_pokemon(self, pokemon, keep_best_mode = False):
return logic_to_function[cp_iv_logic](*release_results.values())

def release_pokemon(self, pokemon):
"""
:type pokemon: Pokemon
"""
try:
if self.bot.config.test:
candy_awarded = 1
Expand All @@ -146,6 +151,7 @@ def release_pokemon(self, pokemon):
# We could refresh here too, but adding 1 saves a inventory request
candy = inventory.candies().get(pokemon.pokemon_id)
candy.add(candy_awarded)
inventory.pokemons().remove(pokemon.id)
self.bot.metrics.released_pokemon()
self.emit_event(
'pokemon_release',
Expand Down
18 changes: 17 additions & 1 deletion pokemongo_bot/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ def all(self):
# makes caller's lives more difficult)
return [p for p in super(Pokemons, self).all() if not isinstance(p, Egg)]

def add(self, pokemon):
if pokemon.id <= 0:
raise ValueError("Can't add a pokemin whitout id")
if pokemon.id in self._data:
raise ValueError("Pokemon already present in the inventory")
self._data[pokemon.id] = pokemon

def remove(self, pokemon_id):
if pokemon_id not in self._data:
raise ValueError("Pokemon not present in the inventory")
self._data.pop(pokemon_id)


#
# Static Components
Expand Down Expand Up @@ -484,7 +496,7 @@ class Pokemon(object):
def __init__(self, data):
self._data = data
# Unique ID for this particular Pokemon
self.id = data['id']
self.id = data.get('id', 0)
# Id of the such pokemons in pokedex
self.pokemon_id = data['pokemon_id']

Expand Down Expand Up @@ -597,6 +609,10 @@ def candy_quantity(self):
def evolution_cost(self):
return Pokemons.evolution_cost_for(self.pokemon_id)

@property
def iv_display(self):
return '{}/{}/{}'.format(self.iv_attack, self.iv_defense, self.iv_stamina)

def _compute_iv_perfection(self):
total_iv = self.iv_attack + self.iv_defense + self.iv_stamina
iv_perfection = round((total_iv / 45.0), 2)
Expand Down

0 comments on commit 76587de

Please sign in to comment.