From d1d3fe332f6f53e3f321da38a1618badfefae649 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 09:49:42 -0700 Subject: [PATCH 1/8] Dota 2 win rate module A Dota 2 win/loss module that helps track the win:loss ratio of recent games. --- docs/conf.py | 3 +- i3pystatus/dota2wins.py | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 i3pystatus/dota2wins.py diff --git a/docs/conf.py b/docs/conf.py index 1f7754a..70f9e82 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,7 +31,8 @@ MOCK_MODULES = [ "notmuch", "requests", "bs4", - "novaclient.v2" + "novaclient.v2", + "dota2py" ] for mod_name in MOCK_MODULES: diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py new file mode 100644 index 0000000..85c1042 --- /dev/null +++ b/i3pystatus/dota2wins.py @@ -0,0 +1,85 @@ +from dota2py import api +from i3pystatus import IntervalModule + + +class Dota2wins(IntervalModule): + """ + Displays the number of VMs in an openstack cluster in ACTIVE and + non-ACTIVE states. + Requires: python-novaclient + """ + + settings = ( + ("matches", "Number of recent matches to calculate"), + ("steamid", "Steam user to track"), + ("steam_api_key", "Steam API key " + "(http://steamcommunity.com/dev/apikey"), + ("good_threshold", "Win percentage (or higher) which you are happy " + "with"), + ("bad_threshold", "Win percentage you want to be alerted (difference " + "between good_threshold and bad_threshold is cautious_threshold)"), + ("interval", "Update interval (games usually last at least 20 min)."), + "format" + ) + required = ("steamid", "steam_api_key") + good_color = "#00FF00" # green + caution_color = "#FFFF00" # yellow + bad_color = "#FF0000" # red + good_threshold = 50 + bad_threshold = 45 + matches = 25 + interval = 1800 + format = "{wins}W:{losses}L {win_percent}%" + + def run(self): + api.set_api_key(self.steam_api_key) + hist = api.get_match_history(account_id=self.steamid)['result'] + recent_matches = [] + while len(recent_matches) < self.matches: + recent_matches.append(hist['matches'].pop(0)) + + player_team_per_match = [] + # create a list of tuples where each tuple is: + # [match_id, bool] + # The bool will be true if the player is on Radiant and alse if they + # are on Dire. + for match in recent_matches: + this_match = [match['match_id']] + for player in match['players']: + # 64bit player ID + long_id = player['account_id'] + 76561197960265728 + if long_id == self.steamid: + if player['player_slot'] < 128: + this_match.append(True) + else: + this_match.append(False) + player_team_per_match.append(this_match) + + outcomes = [] + for match in player_team_per_match: + if api.get_match_details(match[0])['result']['radiant_win'] == match[1]: + outcomes.append(1) + else: + outcomes.append(0) + + wins = outcomes.count(1) + losses = outcomes.count(0) + win_percent = float(sum(outcomes)/float(len(outcomes)))*100 + + if win_percent >= float(self.good_threshold): + color = self.good_color + elif win_percent <= float(self.bad_threshold): + color = self.bad_color + else: + color = self.caution_color + + cdict = { + "wins": wins, + "losses": losses, + "win_percent": win_percent, + } + + self.output = { + "full_text": self.format.format(**cdict), + "color": color + } From ebbe033a6679d1b6d3132c2e098f8149c3a5d119 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 10:21:40 -0700 Subject: [PATCH 2/8] dota2wins: pep8 compliance --- i3pystatus/dota2wins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index 85c1042..18ffa8f 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -64,7 +64,7 @@ class Dota2wins(IntervalModule): wins = outcomes.count(1) losses = outcomes.count(0) - win_percent = float(sum(outcomes)/float(len(outcomes)))*100 + win_percent = float(sum(outcomes) / float(len(outcomes)))*100 if win_percent >= float(self.good_threshold): color = self.good_color From 69b565ac7d7d879e75ff33c18637837f8fa5eac8 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 10:27:44 -0700 Subject: [PATCH 3/8] dota2wins: another pep8 compliance fix --- i3pystatus/dota2wins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index 18ffa8f..ffc82cd 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -64,7 +64,7 @@ class Dota2wins(IntervalModule): wins = outcomes.count(1) losses = outcomes.count(0) - win_percent = float(sum(outcomes) / float(len(outcomes)))*100 + win_percent = float(sum(outcomes) / float(len(outcomes))) * 100 if win_percent >= float(self.good_threshold): color = self.good_color From 4963ac9c9619a872956a7eb768439480fda0adaf Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 11:07:46 -0700 Subject: [PATCH 4/8] dota2wins: update discription --- i3pystatus/dota2wins.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index ffc82cd..3d74988 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -4,9 +4,8 @@ from i3pystatus import IntervalModule class Dota2wins(IntervalModule): """ - Displays the number of VMs in an openstack cluster in ACTIVE and - non-ACTIVE states. - Requires: python-novaclient + Displays the win/loss ratio of a given Dota account. + Requires: dota2py """ settings = ( From 3ce9c13a8a327c81997b637875c4fa6701f9ed07 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 12:05:20 -0700 Subject: [PATCH 5/8] dota2win: provide overrides for color While the default values for the colors are probably reasonable, this patch provides the user with a way to override them. This will be particularly useful for colorblind users, or people who just don't like the defaults. --- i3pystatus/dota2wins.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index 3d74988..f84fe3e 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -12,12 +12,18 @@ class Dota2wins(IntervalModule): ("matches", "Number of recent matches to calculate"), ("steamid", "Steam user to track"), ("steam_api_key", "Steam API key " - "(http://steamcommunity.com/dev/apikey"), + "(http://steamcommunity.com/dev/apikey)"), ("good_threshold", "Win percentage (or higher) which you are happy " "with"), ("bad_threshold", "Win percentage you want to be alerted (difference " "between good_threshold and bad_threshold is cautious_threshold)"), ("interval", "Update interval (games usually last at least 20 min)."), + ("good_color", "Color of text while win percentage is above " + "good_threshold"), + ("bad_color", "Color of text while win percentage is below " + "bad_threshold"), + ("cation_color", "Color of text while win precentage is between good " + "and bad thresholds"), "format" ) required = ("steamid", "steam_api_key") From a1a4127eed3aa93564f8edbe44e86fdab925b185 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 12:45:42 -0700 Subject: [PATCH 6/8] dota2wins: add screename identifier Add the ability for a user to define a screenname or for the name to be dynamically discovered from the API. --- docs/conf.py | 4 +++- i3pystatus/dota2wins.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 70f9e82..417e4d4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,7 +32,9 @@ MOCK_MODULES = [ "requests", "bs4", "novaclient.v2", - "dota2py" + "dota2py", + "urllib", + "json" ] for mod_name in MOCK_MODULES: diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index f84fe3e..32cc8bf 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -24,6 +24,9 @@ class Dota2wins(IntervalModule): "bad_threshold"), ("cation_color", "Color of text while win precentage is between good " "and bad thresholds"), + ("screenname", "If set to 'retrieve', requests for the users's " + "screenname via API calls. Else, use the supplied string as the " + "user's screename"), "format" ) required = ("steamid", "steam_api_key") @@ -34,7 +37,8 @@ class Dota2wins(IntervalModule): bad_threshold = 45 matches = 25 interval = 1800 - format = "{wins}W:{losses}L {win_percent}%" + screenname = 'retrieve' + format = "{screenname} {wins}W:{losses}L {win_percent}%" def run(self): api.set_api_key(self.steam_api_key) @@ -78,7 +82,18 @@ class Dota2wins(IntervalModule): else: color = self.caution_color + if self.screenname == 'retrieve': + from urllib.request import urlopen + import json + response = urlopen( + 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s' % + (self.steam_api_key, self.steamid)) + screenname = json.loads(bytes.decode(response.read()))['response']['players'][0]['personaname'] + else: + screenname = self.screenname + cdict = { + "screenname": screenname, "wins": wins, "losses": losses, "win_percent": win_percent, From b6c9aa5f3ff2e8d393d70143e37e83afa84a6ce8 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 13:57:46 -0700 Subject: [PATCH 7/8] dota2wins: try to fix urllib.request module --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 417e4d4..d005209 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,7 +33,7 @@ MOCK_MODULES = [ "bs4", "novaclient.v2", "dota2py", - "urllib", + "urllib.request", "json" ] From 22dc932e220f25d1fd0081a6506fe8b8b3897d35 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Fri, 5 Jun 2015 06:21:21 -0700 Subject: [PATCH 8/8] dota2wins: fix typo and remove uneeded modules urllib and json are part of the standard library. --- docs/conf.py | 2 -- i3pystatus/dota2wins.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index d005209..02f2f13 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,8 +33,6 @@ MOCK_MODULES = [ "bs4", "novaclient.v2", "dota2py", - "urllib.request", - "json" ] for mod_name in MOCK_MODULES: diff --git a/i3pystatus/dota2wins.py b/i3pystatus/dota2wins.py index 32cc8bf..9237c24 100644 --- a/i3pystatus/dota2wins.py +++ b/i3pystatus/dota2wins.py @@ -22,7 +22,7 @@ class Dota2wins(IntervalModule): "good_threshold"), ("bad_color", "Color of text while win percentage is below " "bad_threshold"), - ("cation_color", "Color of text while win precentage is between good " + ("caution_color", "Color of text while win precentage is between good " "and bad thresholds"), ("screenname", "If set to 'retrieve', requests for the users's " "screenname via API calls. Else, use the supplied string as the "