-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Added nickname templating and associated worker #1850
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from pokemongo_bot import logger | ||
from pokemongo_bot.human_behaviour import sleep | ||
from pokemongo_bot.cell_workers.base_task import BaseTask | ||
|
||
class NicknamePokemon(BaseTask): | ||
def initialize(self): | ||
self.template = self.config.get('nickname_template','').lower().strip() | ||
if self.template == "{name}": | ||
self.template = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might seem unintuitive, but setting the nickname to blank (like in the app) will actually revert the pokemon back to its original name. So, we set the template to blank to achieve this behavior when we anticipate that is what the user wants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add this behaviour explanation as a comment in the code. |
||
|
||
def work(self): | ||
try: | ||
inventory = reduce(dict.__getitem__, ["responses", "GET_INVENTORY", "inventory_delta", "inventory_items"], self.bot.get_inventory()) | ||
except KeyError: | ||
pass | ||
else: | ||
pokemon_data = self._get_inventory_pokemon(inventory) | ||
for pokemon in pokemon_data: | ||
self._nickname_pokemon(pokemon) | ||
|
||
def _get_inventory_pokemon(self,inventory_dict): | ||
pokemon_data = [] | ||
for inv_data in inventory_dict: | ||
try: | ||
pokemon = reduce(dict.__getitem__,['inventory_item_data','pokemon_data'],inv_data) | ||
except KeyError: | ||
pass | ||
else: | ||
if not pokemon.get('is_egg',False): | ||
pokemon_data.append(pokemon) | ||
return pokemon_data | ||
|
||
def _nickname_pokemon(self,pokemon): | ||
"""This requies a pokemon object containing all the standard fields: id, ivs, cp, etc""" | ||
new_name = "" | ||
instance_id = pokemon.get('id',0) | ||
if not instance_id: | ||
logger.log("Pokemon instance id returned 0. Can't rename.",'red') | ||
return | ||
id = pokemon.get('pokemon_id',0)-1 | ||
name = self.bot.pokemon_list[id]['Name'] | ||
cp = pokemon.get('cp',0) | ||
iv_attack = pokemon.get('individual_attack',0) | ||
iv_defense = pokemon.get('individual_defense',0) | ||
iv_stamina = pokemon.get('individual_stamina',0) | ||
iv_list = [iv_attack,iv_defense,iv_stamina] | ||
iv_ads = "/".join(map(str,iv_list)) | ||
iv_sum = sum(iv_list) | ||
iv_pct = "{:0.0f}".format(100*iv_sum/45.0) | ||
log_color = 'red' | ||
try: | ||
new_name = self.template.format(name=name, | ||
id=id, | ||
cp=cp, | ||
iv_attack=iv_attack, | ||
iv_defense=iv_defense, | ||
iv_stamina=iv_stamina, | ||
iv_ads=iv_ads, | ||
iv_sum=iv_sum, | ||
iv_pct=iv_pct)[:12] | ||
except KeyError as bad_key: | ||
logger.log("Unable to nickname {} due to bad template ({})".format(name,bad_key),log_color) | ||
if pokemon.get('nickname', "") == new_name: | ||
return | ||
self.bot.api.nickname_pokemon(pokemon_id=instance_id,nickname=new_name) | ||
response = self.bot.api.call() | ||
sleep(1.2) | ||
try: | ||
result = reduce(dict.__getitem__, ["responses", "NICKNAME_POKEMON"], response) | ||
except KeyError: | ||
logger.log("Attempt to nickname received bad response from server.",log_color) | ||
if self.bot.config.debug: | ||
logger.log(response,log_color) | ||
return | ||
result = result['result'] | ||
if new_name == "": | ||
new_name = name | ||
output = { | ||
0: 'Nickname unset', | ||
1: 'Nickname set successfully! {} is now {}'.format(name,new_name), | ||
2: 'Invalid nickname! ({})'.format(new_name), | ||
3: 'Pokemon not found.', | ||
4: 'Pokemon is egg' | ||
}[result] | ||
if result==1: | ||
log_color='green' | ||
pokemon['nickname'] = new_name | ||
logger.log(output,log_color) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import time | ||
|
||
from pokemongo_bot import logger | ||
from pokemongo_bot.human_behaviour import (normalized_reticle_size, sleep, | ||
spin_modifier) | ||
|
||
|
||
class PokemonCatchWorker(object): | ||
BAG_FULL = 'bag_full' | ||
NO_POKEBALLS = 'no_pokeballs' | ||
|
@@ -52,9 +50,9 @@ def work(self): | |
individual_defense = pokemon_data.get("individual_defense", 0) | ||
|
||
iv_display = '{}/{}/{}'.format( | ||
individual_stamina, | ||
individual_attack, | ||
individual_defense | ||
individual_defense, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like the changes to this file aren't relevant either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its a nice feature to resort the Bot ordering of IV from SAD to ADS so its in the same order like the renaming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason that the renaming worker uses ADS format is because that is also used in the web UI. We're using this as an opportunity to make the pokemon catch worker output consistent with this format as well. |
||
individual_stamina | ||
) | ||
|
||
pokemon_potential = self.pokemon_potential(pokemon_data) | ||
|
@@ -63,7 +61,7 @@ def work(self): | |
logger.log('A Wild {} appeared! [CP {}] [Potential {}]'.format( | ||
pokemon_name, cp, pokemon_potential), 'yellow') | ||
|
||
logger.log('IV [Stamina/Attack/Defense] = [{}]'.format(iv_display)) | ||
logger.log('IV [Attack/Defense/Stamina] = [{}]'.format(iv_display)) | ||
pokemon_data['name'] = pokemon_name | ||
# Simulate app | ||
sleep(3) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does a PR that is adding a task for renaming pokemon need to modify the incubate eggs task?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was initially a change required for an earlier iteration of this to work (as it required that pokemon objects passed in maintained their standard keys). As you can see, instead of passing out an object with only a subset of keys, it maintains the original object and just appends new calculated data. I opted to leave it in because I feel this is still a better approach in case the egg incubation worker wants to utilize other keys in the pokemon object. At the very least, it doesn't break anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it isn't needed by this PR, can you undo these changes? It makes it much scarier to review and accept these PRs since it is hard to tell what the impact across the whole project is going to be. Keeping changes small and focused on a single worker makes our lives way easier. If we need these changes down the road, I'd feel more comfortable making them when it actually impacts something.