Dota 2 win rate module

A Dota 2 win/loss module that helps track the win:loss ratio of
recent games.
This commit is contained in:
David Wahlstrom 2015-06-04 09:49:42 -07:00
parent 588e8dfc88
commit d1d3fe332f
2 changed files with 87 additions and 1 deletions

View File

@ -31,7 +31,8 @@ MOCK_MODULES = [
"notmuch",
"requests",
"bs4",
"novaclient.v2"
"novaclient.v2",
"dota2py"
]
for mod_name in MOCK_MODULES:

85
i3pystatus/dota2wins.py Normal file
View File

@ -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
}