Skip to content

Commit

Permalink
Merge pull request #4735 from DeXtroTip/show_best_pokemon
Browse files Browse the repository at this point in the history
Show best pokemon task
  • Loading branch information
solderzzc authored Aug 26, 2016
2 parents 6984b85 + cd2526d commit 83ead0b
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 0 deletions.
10 changes: 10 additions & 0 deletions configs/config.json.cluster.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "TransferPokemon",
"config": {
Expand Down
10 changes: 10 additions & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "TransferPokemon",
"config": {
Expand Down
10 changes: 10 additions & 0 deletions configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "TransferPokemon",
"config": {
Expand Down
10 changes: 10 additions & 0 deletions configs/config.json.optimizer.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "PokemonOptimizer",
"config": {
Expand Down
10 changes: 10 additions & 0 deletions configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "TransferPokemon",
"config": {
Expand Down
10 changes: 10 additions & 0 deletions configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
"items": ["pokemon_bag", "space_info", "pokeballs", "greatballs", "ultraballs", "razzberries", "luckyegg"]
}
},
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps", "hp"]
}
},
{
"type": "TransferPokemon",
"config": {
Expand Down
51 changes: 51 additions & 0 deletions docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- [Sleep Schedule Task](#sleep-schedule-task)
- [Random Pause](#random-pause)
- [Egg Incubator](#egg-incubator)
- [ShowBestPokemon](#showbestpokemon)

#Configuration files

Expand Down Expand Up @@ -704,6 +705,7 @@ Periodically displays the user inventory in the terminal.

### Options
[[back to top](#table-of-contents)]

* `min_interval` : The minimum interval at which the stats are displayed, in seconds (defaults to 120 seconds). The update interval cannot be accurate as workers run synchronously.
* `show_all_multiple_lines` : Logs all items on inventory using multiple lines. Ignores configuration of 'items'
* `items` : An array of items to display and their display order (implicitly), see available items below (defaults to []).
Expand Down Expand Up @@ -824,4 +826,53 @@ Configure how the bot should use the incubators.
}
```

## ShowBestPokemon
[[back to top](#table-of-contents)]

### Description
[[back to top](#table-of-contents)]

Periodically displays the user best pokemon in the terminal.

### Options
[[back to top](#table-of-contents)]

* `min_interval` : The minimum interval at which the pokemon are displayed, in seconds (defaults to 120 seconds). The update interval cannot be accurate as workers run synchronously.
* `amount` : Amount of pokemon to show.
* `order_by` : Stat that will be used to get best pokemons.
Available Stats: 'cp', 'iv', 'ivcp', 'ncp', 'dps', 'hp', 'level'
* `info_to_show` : Info to show for each pokemon

Available `info_to_show` :
```
'cp',
'iv_ads',
'iv_pct',
'ivcp',
'ncp',
'level',
'hp',
'moveset',
'dps'
```

### Sample configuration
[[back to top](#table-of-contents)]
```json
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps"]
}
}
```

### Example console output
[[back to top](#table-of-contents)]
```
2016-08-25 21:20:59,642 [ShowBestPokemon] [INFO] [show_best_pokemon] [Tauros, CP 575, IVCP 0.95, DPS 12.04] | [Grimer, CP 613, IVCP 0.93, DPS 13.93] | [Tangela, CP 736, IVCP 0.93, DPS 14.5] | [Staryu, CP 316, IVCP 0.92, DPS 10.75] | [Gastly, CP 224, IVCP 0.9, DPS 11.7]
```
1 change: 1 addition & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def _register_events(self):
self.event_manager.register_event('vip_pokemon')
self.event_manager.register_event('gained_candy', parameters=('quantity', 'type'))
self.event_manager.register_event('catch_limit')
self.event_manager.register_event('show_best_pokemon', parameters=('pokemons'))

# level up stuff
self.event_manager.register_event(
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
from random_pause import RandomPause
from update_web_inventory import UpdateWebInventory
from random_alive_pause import RandomAlivePause
from show_best_pokemon import ShowBestPokemon
152 changes: 152 additions & 0 deletions pokemongo_bot/cell_workers/show_best_pokemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import ctypes
from datetime import datetime, timedelta

from pokemongo_bot import inventory
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.tree_config_builder import ConfigException


class ShowBestPokemon(BaseTask):
"""
Periodically displays the user best pokemon in the terminal.
Example config :
{
"type": "ShowBestPokemon",
"config": {
"enabled": true,
"min_interval": 60,
"amount": 5,
"order_by": "cp",
"info_to_show": ["cp", "ivcp", "dps"]
}
}
min_interval : The minimum interval at which the pokemon are displayed,
in seconds (defaults to 120 seconds).
The update interval cannot be accurate as workers run synchronously.
amount : Amount of pokemon to show
order_by : Stat that will be used to get best pokemons
Available Stats: 'cp', 'iv', 'ivcp', 'ncp', 'dps', 'hp', 'level'
info_to_show : Info to show for each pokemon
Available info_to_show :
'cp',
'iv_ads',
'iv_pct',
'ivcp',
'ncp',
'level',
'hp',
'moveset',
'dps'
"""

SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.next_update = None
self.min_interval = self.config.get('min_interval', 120)
self.amount = self.config.get('amount', 3)
self.order_by = self.config.get('order_by', 'cp')
self.info_to_show = self.config.get('info_to_show', [])

def work(self):
"""
Displays the pokemon if necessary.
:return: Always returns WorkerResult.SUCCESS.
:rtype: WorkerResult
"""
if not self.info_to_show or not self.amount or not self._should_print():
return WorkerResult.SUCCESS

self.pokemons = inventory.pokemons().all()

line = self._get_pokemons_line()
if not line:
return WorkerResult.SUCCESS

self.print_pokemons(line)
return WorkerResult.SUCCESS

def _should_print(self):
"""
Returns a value indicating whether the pokemon should be displayed.
:return: True if the stats should be displayed; otherwise, False.
:rtype: bool
"""
return self.next_update is None or datetime.now() >= self.next_update

def _compute_next_update(self):
"""
Computes the next update datetime based on the minimum update interval.
:return: Nothing.
:rtype: None
"""
self.next_update = datetime.now() + timedelta(seconds=self.min_interval)

def print_pokemons(self, pokemons):
"""
Logs the pokemon into the terminal using an event.
:param pokemons: The pokemon to display.
:type pokemons: string
:return: Nothing.
:rtype: None
"""
self.emit_event(
'show_best_pokemon',
formatted="{pokemons}",
data={
'pokemons': pokemons
}
)

self._compute_next_update()

def _get_pokemons_line(self):
"""
Generates a string according to the configuration.
:return: A string containing pokemons and their info, ready to be displayed.
:rtype: string
"""
def get_poke_info(info, pokemon):
poke_info = {
'cp': pokemon.cp,
'iv': pokemon.iv,
'ivcp': pokemon.ivcp,
'ncp': pokemon.cp_percent,
'level': pokemon.level,
'hp': pokemon.hp,
'dps': pokemon.moveset.dps
}
if info not in poke_info:
raise ConfigException("order by {}' isn't available".format(self.order_by))
return poke_info[info]

def get_poke_info_formatted(info, pokemon):
poke_info = {
'name': pokemon.name,
'cp': 'CP {}'.format(pokemon.cp),
'iv_ads': 'A/D/S {}/{}/{}'.format(pokemon.iv_attack, pokemon.iv_defense, pokemon.iv_stamina),
'iv_pct': 'IV {}'.format(pokemon.iv),
'ivcp': 'IVCP {}'.format(round(pokemon.ivcp,2)),
'ncp': 'NCP {}'.format(round(pokemon.cp_percent,2)),
'level': "Level {}".format(pokemon.level),
'hp': 'HP {}/{}'.format(pokemon.hp, pokemon.hp_max),
'moveset': 'Moves: {}'.format(pokemon.moveset),
'dps': 'DPS {}'.format(round(pokemon.moveset.dps, 2))
}
if info not in poke_info:
raise ConfigException("info '{}' isn't available for displaying".format(info))
return poke_info[info]

info_to_show = ['name'] + self.info_to_show

pokemons_ordered = sorted(self.pokemons, key=lambda x: get_poke_info(self.order_by, x), reverse=True)
pokemons_ordered = pokemons_ordered[:self.amount]

poke_info = ['[{}]'.format(', '.join([get_poke_info_formatted(x, p) for x in info_to_show])) for p in pokemons_ordered]

line = ' | '.join(poke_info)
return line
1 change: 1 addition & 0 deletions pokemongo_bot/event_handlers/colored_logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ColoredLoggingHandler(EventHandler):
'path_lap_end': 'green',
'log_stats': 'magenta',
'show_inventory': 'magenta',
'show_best_pokemon': 'magenta',

# event names for 'white' still here to remember that these events are already determined its color.
'arrived_at_cluster': 'white',
Expand Down

0 comments on commit 83ead0b

Please sign in to comment.