From d1d3fe332f6f53e3f321da38a1618badfefae649 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Thu, 4 Jun 2015 09:49:42 -0700 Subject: [PATCH] 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 + }