Skip to content

Commit

Permalink
✨ Added login and username to available stats (#2494)
Browse files Browse the repository at this point in the history
Added a player_data property in PokemonGoBot to access player data from outside
Added unit tests for login and username stats
Added tests for call args when updating the window title
Added a platform-specific test for window title updating on win32 platform
  • Loading branch information
Genesis authored and douglascamata committed Aug 7, 2016
1 parent 823ba83 commit fc4e802
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@
* matheussampaio
* Abraxas000
* lucasfevi
* Moonlight-Angel
9 changes: 9 additions & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def position(self):
def position(self, position_tuple):
self.api._position_lat, self.api._position_lng, self.api._position_alt = position_tuple

@property
def player_data(self):
"""
Returns the player data as received from the API.
:return: The player data.
:rtype: dict
"""
return self._player

def __init__(self, config):
self.config = config
self.fort_timeouts = dict()
Expand Down
12 changes: 9 additions & 3 deletions pokemongo_bot/cell_workers/update_title_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class UpdateTitleStats(BaseTask):
"type": "UpdateTitleStats",
"config": {
"min_interval": 10,
"stats": ["uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"]
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"]
}
}
Available stats :
- login : The account login (from the credentials).
- username : The trainer name (asked at first in-game connection).
- uptime : The bot uptime.
- km_walked : The kilometers walked since the bot started.
- level : The current character's level.
Expand Down Expand Up @@ -107,8 +109,7 @@ def _update_title(self, title, platform):
:rtype: None
:raise: RuntimeError: When the given platform isn't supported.
"""
if platform == "linux" or platform == "linux2"\
or platform == "cygwin":
if platform == "linux" or platform == "linux2" or platform == "cygwin":
stdout.write("\x1b]2;{}\x07".format(title))
elif platform == "darwin":
stdout.write("\033]0;{}\007".format(title))
Expand Down Expand Up @@ -145,6 +146,9 @@ def _get_stats_title(self, player_stats):
metrics = self.bot.metrics
metrics.capture_stats()
runtime = metrics.runtime()
login = self.bot.config.username
player_data = self.bot.player_data
username = player_data.get('username', '?')
distance_travelled = metrics.distance_travelled()
current_level = int(player_stats.get('level', 0))
prev_level_xp = int(player_stats.get('prev_level_xp', 0))
Expand Down Expand Up @@ -172,6 +176,8 @@ def _get_stats_title(self, player_stats):

# Create stats strings.
available_stats = {
'login': login,
'username': username,
'uptime': 'Uptime : {}'.format(runtime),
'km_walked': '{:,.2f}km walked'.format(distance_travelled),
'level': 'Level {}'.format(current_level),
Expand Down
57 changes: 38 additions & 19 deletions tests/update_title_stats_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import unittest
from sys import platform as _platform
from datetime import datetime, timedelta
from mock import patch, MagicMock
from mock import call, patch, MagicMock
from pokemongo_bot.cell_workers.update_title_stats import UpdateTitleStats
from tests import FakeBot


class UpdateTitleStatsTestCase(unittest.TestCase):
config = {
'min_interval': 20,
'stats': ['pokemon_evolved', 'pokemon_encountered', 'uptime', 'pokemon_caught',
'stops_visited', 'km_walked', 'level', 'stardust_earned', 'level_completion',
'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon', 'level_stats',
'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon', 'pokemon_stats',
'pokemon_released']
'stats': ['login', 'username', 'pokemon_evolved', 'pokemon_encountered', 'uptime',
'pokemon_caught', 'stops_visited', 'km_walked', 'level', 'stardust_earned',
'level_completion', 'xp_per_hour', 'pokeballs_thrown', 'highest_cp_pokemon',
'level_stats', 'xp_earned', 'pokemon_unseen', 'most_perfect_pokemon',
'pokemon_stats', 'pokemon_released']
}
player_stats = {
'level': 25,
Expand All @@ -23,6 +24,8 @@ class UpdateTitleStatsTestCase(unittest.TestCase):

def setUp(self):
self.bot = FakeBot()
self.bot._player = {'username': 'Username'}
self.bot.config.username = 'Login'
self.worker = UpdateTitleStats(self.bot, self.config)

def mock_metrics(self):
Expand Down Expand Up @@ -87,22 +90,37 @@ def test_next_update_after_update_title(self, mock_datetime):
now + timedelta(seconds=self.config['min_interval']))

@patch('pokemongo_bot.cell_workers.update_title_stats.stdout')
def test_update_title_linux_osx(self, mock_stdout):
self.worker._update_title('', 'linux')
def test_update_title_linux_cygwin(self, mock_stdout):
self.worker._update_title('new title linux', 'linux')

self.assertEqual(mock_stdout.write.call_count, 1)
self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux\x07'))

self.worker._update_title('', 'linux2')
self.worker._update_title('new title linux2', 'linux2')

self.assertEqual(mock_stdout.write.call_count, 2)
self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title linux2\x07'))

self.worker._update_title('', 'darwin')
self.worker._update_title('new title cygwin', 'cygwin')

self.assertEqual(mock_stdout.write.call_count, 3)
self.assertEqual(mock_stdout.write.call_args, call('\x1b]2;new title cygwin\x07'))

@patch('pokemongo_bot.cell_workers.update_title_stats.stdout')
def test_update_title_darwin(self, mock_stdout):
self.worker._update_title('new title darwin', 'darwin')

self.assertEqual(mock_stdout.write.call_count, 1)
self.assertEqual(mock_stdout.write.call_args, call('\033]0;new title darwin\007'))

@unittest.skipUnless(_platform.startswith("win"), "requires Windows")
@patch('pokemongo_bot.cell_workers.update_title_stats.ctypes')
def test_update_title_win32(self, mock_ctypes):
self.worker._update_title('new title win32', 'win32')

@unittest.skip("Didn't find a way to mock ctypes.windll.kernel32.SetConsoleTitleA")
def test_update_title_win32(self):
self.worker._update_title('', 'win32')
self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_count, 1)
self.assertEqual(mock_ctypes.windll.kernel32.SetConsoleTitleA.call_args,
call('new title win32'))

def test_get_stats_title_player_stats_none(self):
title = self.worker._get_stats_title(None)
Expand All @@ -119,12 +137,13 @@ def test_get_stats(self):
self.mock_metrics()

title = self.worker._get_stats_title(self.player_stats)
expected = 'Evolved 12 pokemon | Encountered 130 pokemon | Uptime : 15:42:13 | ' \
'Caught 120 pokemon | Visited 220 stops | 42.05km walked | Level 25 | ' \
'Earned 24,069 Stardust | 87,500 / 150,000 XP (58%) | 1,337 XP/h | ' \
'Threw 145 pokeballs | Highest CP pokemon : highest_cp | ' \
'Level 25 (87,500 / 150,000, 58%) | +424,242 XP | ' \
'Encountered 3 new pokemon | Most perfect pokemon : most_perfect | ' \
expected = 'Login | Username | Evolved 12 pokemon | Encountered 130 pokemon | ' \
'Uptime : 15:42:13 | Caught 120 pokemon | Visited 220 stops | ' \
'42.05km walked | Level 25 | Earned 24,069 Stardust | ' \
'87,500 / 150,000 XP (58%) | 1,337 XP/h | Threw 145 pokeballs | ' \
'Highest CP pokemon : highest_cp | Level 25 (87,500 / 150,000, 58%) | ' \
'+424,242 XP | Encountered 3 new pokemon | ' \
'Most perfect pokemon : most_perfect | ' \
'Encountered 130 pokemon, 120 caught, 30 released, 12 evolved, ' \
'3 never seen before | Released 30 pokemon'

Expand Down

0 comments on commit fc4e802

Please sign in to comment.