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

Log stats on terminal #3312

Merged
merged 5 commits into from
Aug 9, 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
53 changes: 36 additions & 17 deletions pokemongo_bot/cell_workers/update_title_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ class UpdateTitleStats(BaseTask):
"type": "UpdateTitleStats",
"config": {
"min_interval": 10,
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"]
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"],
}
}

You can set a logging on terminal mode like this:

Example logging on console (and disabling title change):
{
"type": "UpdateTitleStats",
"config": {
"min_interval": 10,
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"],
"terminal_log": true,
"terminal_title": false
}
}
Available stats :
- login : The account login (from the credentials).
- username : The trainer name (asked at first in-game connection).
Expand Down Expand Up @@ -53,8 +65,6 @@ class UpdateTitleStats(BaseTask):
"""
SUPPORTED_TASK_API_VERSION = 1

DEFAULT_MIN_INTERVAL = 10
DEFAULT_DISPLAYED_STATS = []

def __init__(self, bot, config):
"""
Expand All @@ -67,12 +77,14 @@ def __init__(self, bot, config):
super(UpdateTitleStats, self).__init__(bot, config)

self.next_update = None
self.min_interval = self.DEFAULT_MIN_INTERVAL
self.displayed_stats = self.DEFAULT_DISPLAYED_STATS

self.bot.event_manager.register_event('update_title', parameters=('title'))
self.min_interval = int(self.config.get('min_interval', 120))
self.displayed_stats = self.config.get('stats', [])
self.terminal_log = self.config.get('terminal_log', False)
self.terminal_title = self.config.get('terminal_title', True)

self._process_config()
self.bot.event_manager.register_event('update_title', parameters=('title',))
self.bot.event_manager.register_event('log_stats',parameters=('title',))

def initialize(self):
pass
Expand All @@ -89,7 +101,12 @@ def work(self):
# If title is empty, it couldn't be generated.
if not title:
return WorkerResult.SUCCESS
self._update_title(title, _platform)

if self.terminal_title:
self._update_title(title, _platform)

if self.terminal_log:
self._log_on_terminal(title)
return WorkerResult.SUCCESS

def _should_display(self):
Expand All @@ -100,6 +117,16 @@ def _should_display(self):
"""
return self.next_update is None or datetime.now() >= self.next_update

def _log_on_terminal(self, title):
self.emit_event(
'log_stats',
formatted="{title}",
data={
'title': title
}
)
self.next_update = datetime.now() + timedelta(seconds=self.min_interval)

def _update_title(self, title, platform):
"""
Updates the window title using different methods, according to the given platform
Expand All @@ -119,7 +146,7 @@ def _update_title(self, title, platform):
'title': title
}
)

if platform == "linux" or platform == "linux2" or platform == "cygwin":
stdout.write("\x1b]2;{}\x07".format(title))
stdout.flush()
Expand All @@ -133,14 +160,6 @@ def _update_title(self, title, platform):

self.next_update = datetime.now() + timedelta(seconds=self.min_interval)

def _process_config(self):
"""
Fetches the configuration for this worker and stores the values internally.
:return: Nothing.
:rtype: None
"""
self.min_interval = int(self.config.get('min_interval', self.DEFAULT_MIN_INTERVAL))
self.displayed_stats = self.config.get('stats', self.DEFAULT_DISPLAYED_STATS)

Copy link
Member

@douglascamata douglascamata Aug 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you rewrite all the configs again. IMHO, this code should go to def __init__ and instead of the constants as default we can have actual values of these constants.

def _get_stats_title(self, player_stats):
"""
Expand Down