Created
April 4, 2018 22:08
-
-
Save arfie/59fefc54c796b1ed493702ffa03cd812 to your computer and use it in GitHub Desktop.
stat aggregator that can deal with tpl files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Takes one or more TagPro Analytics matches through standard input, then prints | |
a CSV file containing aggregated statistics for all players in those games. | |
""" | |
import requests | |
import tagpro_eu | |
import csv | |
import sys | |
from collections import defaultdict | |
def parse_tpl_shit(data): | |
match = defaultdict(tagpro_eu.PlayerStats) | |
cd = 0 | |
for player, stats in data['players'].items(): | |
s = match[player] | |
team = 1 if stats['team'] == 1 else -1 | |
cd += team * stats['captures'] | |
s.tags = stats['tags'] | |
s.pops = stats['pops'] | |
s.grabs = stats['grabs'] | |
s.drops = stats['drops'] | |
s.hold = tagpro_eu.Time.from_seconds(int(stats['hold'])) | |
s.captures = stats['captures'] | |
s.prevent = tagpro_eu.Time.from_seconds(int(stats['prevent'])) | |
s.returns = stats['returns'] | |
s.pups[tagpro_eu.Powerup.none] = stats['pups'] | |
s.time = tagpro_eu.Time.from_minutes(int(stats['minutes'])) | |
for player, stats in data['players'].items(): | |
if stats['team'] == 1: | |
match[player].caps_for = cd | |
else: | |
match[player].caps_against = cd | |
return match | |
links = sys.stdin.readlines() | |
stats = defaultdict(tagpro_eu.PlayerStats) | |
for l in links: | |
link = l.strip() | |
if not link: | |
continue | |
mid = tagpro_eu.match_url_to_id(link) | |
if mid is not None: | |
link = f'https://tagpro.eu/?download={mid}' | |
sys.stderr.write(f'{link}\n') | |
data = requests.get(link).json() | |
if data.get('type', None) == 'stream': | |
match = parse_tpl_shit(data) | |
for player, s in match.items(): | |
stats[player] += s | |
else: | |
match = tagpro_eu.Match(data) | |
for player in match.players: | |
stats[player.name] += player.stats | |
columns = [ | |
('+/-', lambda s: s.cap_diff), | |
('time', lambda s: s.time // 60), | |
('tags', lambda s: s.tags), | |
('pops', lambda s: s.pops), | |
('grabs', lambda s: s.grabs), | |
('drops', lambda s: s.drops), | |
('hold', lambda s: s.hold // 60), | |
('captures', lambda s: s.captures), | |
('returns', lambda s: s.returns), | |
('prevent', lambda s: s.prevent // 60), | |
('pups', lambda s: sum(s.pups.values())), | |
('button', lambda s: s.button // 60), | |
('block', lambda s: s.block // 60), | |
] | |
wr = csv.writer(sys.stdout) | |
wr.writerow(['player'] + [t[0] for t in columns]) | |
for p, s in stats.items(): | |
wr.writerow([p] + [t[1](s) for t in columns]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment