import json
import csv
from island.matches import Matches

user_data = {}  # user_id => {m => match_count, s => total_score, d => {match_id => score}}

ms = Matches('wos-data-2022-12-16')
for m in ms.data:
    game_end_at = int(m.query('game', 'created').first()['info']['game_end_at'])
    for r in m.query('user', 'fitness').raw_data:
        if r['user'] not in user_data:
            user_data[r['user']] = dict(m=0, s=0, d={})
        user_data[r['user']]['m'] += 1
        user_data[r['user']]['s'] += float(r['fitness']) / game_end_at

match_bonus = {'炎儒': {'vote': 24, 'bonus': '106.67'}, 'linhaha': {'vote': 15, 'bonus': '66.67'}, '樊欣': {'vote': 10, 'bonus': '44.44'}, 'z': {'vote': 9, 'bonus': '40.00'}, '乖乖': {'vote': 36, 'bonus': '160.00'}, 'CHYK': {'vote': 27, 'bonus': '120.00'}, '赵子截江夺阿斗': {'vote': 24, 'bonus': '106.67'}, '海丽可可': {'vote': 12, 'bonus': '53.33'}, 'gwb': {'vote': 28, 'bonus': '124.44'}, 'Lee.h': {'vote': 13, 'bonus': '57.78'}, 'Yao': {'vote': 12, 'bonus': '53.33'}, 'zmw': {'vote': 15, 'bonus': '66.67'}}

print('username\tvote\tbonus')
for k, v in match_bonus.items():
    print(f"{k}\t{v['vote']}\t{v['bonus']}")

total = 0
print('\t'.join(('UID', 'Bonus', 'username', 'email')))
with open('username.csv', 'r', encoding='utf-8') as f:
    for line in f:
        parts = line.strip().split(',')
        uid = int(parts[0])
        if uid in user_data:
            bonus = user_data[uid]['s']
            if parts[1] in match_bonus:
                bonus += float(match_bonus[parts[1]]['bonus'])
            parts.insert(1, '%.2f' % bonus)
            total += bonus
            print('\t'.join(parts))

print(f'total={total:.2f}')
