add team members not in list. sort columns by number of games.

This commit is contained in:
Maximilian Keßler 2023-11-07 09:24:39 +01:00
parent 3832aecf95
commit dab247886e
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -1,7 +1,10 @@
import requests import requests
import requests_cache
import json import json
import csv import csv
session = requests_cache.CachedSession('.hanab-live.cache')
player_mapping = { player_mapping = {
'RamaNoVarjan': 'Ramanujan', 'RamaNoVarjan': 'Ramanujan',
'purplejoe2': 'PurpleJoe', 'purplejoe2': 'PurpleJoe',
@ -11,18 +14,24 @@ player_mapping = {
'MarkusKahlsen': 'Markus', 'MarkusKahlsen': 'Markus',
'NoVarkusKahlsen': 'Markus', 'NoVarkusKahlsen': 'Markus',
'spring': 'spring', 'spring': 'spring',
'str8tsknacker': 'str8t', 'str8tsknacker': 'str8tsknacker',
'novarknacker': 'str8t', 'novarknacker': 'str8tsknacker',
'StKildaFan': 'Kilda', 'StKildaFan': 'Kilda',
'noarv': 'arv', 'noarv': 'Arv',
'arv': 'arv', 'arv': 'Arv',
'ElenaDhynho': 'Elena', 'ElenaDhynho': 'Elena',
'ElenaDhynh0': 'Elena', 'ElenaDhynh0': 'Elena',
'Elenanovar': 'Elena', 'Elenanovar': 'Elena',
'TimeHoodie': 'Hoodie' 'TimeHoodie': 'Hoodie',
'Hoodie': 'Hoodie',
'newduke': 'Duke',
} }
players_lower = [s.lower() for s in player_mapping.keys()] allowed_team_members = ['Carunty', 'HelanaAshryvr', 'rz']
allowed_players = [s.lower() for s in player_mapping.keys()] + [s.lower() for s in allowed_team_members]
print(allowed_players)
player_cols = set() player_cols = set()
for _, p in player_mapping.items(): for _, p in player_mapping.items():
player_cols.add(p) player_cols.add(p)
@ -38,7 +47,7 @@ class Entry():
def get_player_games(player: str): def get_player_games(player: str):
r = requests.get('https://hanab.live/api/v1/history-full/{}'.format(player)) r = session.get('https://hanab.live/api/v1/history-full/{}?start={}'.format(player, 1044446))
if r.status_code == 200: if r.status_code == 200:
return json.loads(r.text) return json.loads(r.text)
@ -61,7 +70,9 @@ def collect_player_games():
ok = True ok = True
for player in players: for player in players:
if player.lower() not in players_lower: if player.lower() not in allowed_players:
print('rejecting game because of player {}'.format(player))
print(allowed_players)
ok = False ok = False
if ok: if ok:
global_games[game_id] = Entry(game_id, seed, score, players) global_games[game_id] = Entry(game_id, seed, score, players)
@ -69,16 +80,27 @@ def collect_player_games():
return global_games return global_games
def sort_players_by_num_games(games_dict):
nums = {}
for _, entry in games_dict.items():
for player in entry.players:
col = player_mapping.get(player, None)
if col is not None:
num = nums.get(col, 0)
nums[col] = num + 1
return sorted(player_cols, key = lambda col: -nums[col])
if __name__ == "__main__": if __name__ == "__main__":
games = collect_player_games() games = collect_player_games()
streaks = {} streaks = {}
fieldnames = ['Replay Number', 'Seed Name', 'Player #', 'W/L(1/0)'] fieldnames = ['Replay Number', 'Seed Name', 'Player #', 'W/L(1/0)']
fieldnames += player_cols fieldnames += sort_players_by_num_games(games)
fieldnames += ['Other']
with open('games.csv', 'w') as f: with open('games.csv', 'w', newline='') as f:
f.writelines([','.join(fieldnames)]) f.writelines([','.join(fieldnames), "\n"])
with open('games.csv', 'w+', newline='') as f: with open('games.csv', 'a', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames) writer = csv.DictWriter(f, fieldnames=fieldnames)
for _, entry in sorted(games.items()): for _, entry in sorted(games.items()):
row = { row = {
@ -88,15 +110,19 @@ if __name__ == "__main__":
'W/L(1/0)': entry.won 'W/L(1/0)': entry.won
} }
for player in entry.players: for player in entry.players:
col = player_mapping[player] col = player_mapping.get(player, None)
if entry.won: if col is not None:
streak = streaks.get(col, 0) if entry.won:
streak += 1 streak = streaks.get(col, 0)
streaks[col] = streak streak += 1
row[col] = streak streaks[col] = streak
row[col] = streak
else:
streaks[col] = 0
row[col] = 0
else: else:
streaks[col] = 0 num_others = row.get('Other', 0)
row[col] = 0 row['Other'] = num_others + 1
writer.writerow(row) writer.writerow(row)