import json
import glob

winner = {}
pid2uid = {}
score = {}

act_set = {'accept_failed', 'request', 'deny'}

to_print = [359, 502, 413, 512, 460, 451, 473, 511, 489, 255, 342, 481]
username = {359: '雷神的小锤子', 502: '囚徒困境', 413: 'luwen', 512: 'Richard', 460: 'CHYK', 451: '海丽可可',
            473: 'fyl', 511: '赵子截江夺阿斗', 489: 'J', 255: 'gly', 342: '陈晓宇', 481: 'Arthur'}

G_NAME = dict(G975=1, G1142=2, G1156=3, G1162=4)
G = ['G975.json', 'G1142.json', 'G1156.json', 'G1162.json']

for fn in glob.glob("*.json", recursive=False):
    with open(fn, encoding='utf-8') as f:
        data = json.load(f)
    total_round = int(data[0]['info']['game_end_at'])
    uids = {}
    for r in data[::-1]:
        if r['cat'] == 'game' and r['act'] == 'winner':
            for uid in r['data']:
                uids[uid] = dict(h=[[] for _ in range(total_round)])
            break

    for uid, d in uids.items():
        if uid not in winner:
            winner[uid] = {}

    for r in data:
        if r['cat'] == 'player' and r['act'] == 'join':
            if r['uid'] in uids:
                pid2uid[r['pid']] = r['uid']
                winner[r['uid']][fn] = uids[r['uid']] | {'p': r['pid']}
        if r['cat'] == 'action':
            if r['act'] == 'done':
                if r['a'] in pid2uid:
                    winner[pid2uid[r['a']]][fn]['h'][r['rno'] - 1].append(r)
                elif r['b'] in pid2uid:
                    winner[pid2uid[r['b']]][fn]['h'][r['rno'] - 1].append(r)
            elif r['act'] in act_set:
                if r['from'] in pid2uid:
                    winner[pid2uid[r['from']]][fn]['h'][r['rno'] - 1].append(r)

# print(winner.keys())
symbol = dict(D='🔪', C='🤝')
ACT_DONE = "%d. TA出了%s，对方%d出了%s，消耗时间资源%d。"

with open("result.txt", "w", encoding="utf-8") as f:
    for uid in to_print:
        if uid not in winner:
            continue
        print("\n\n玩家：", username[uid], file=f)
        for g in G:
            if g not in winner[uid]:
                continue
            data = winner[uid][g]
            print("在第%d场比赛中获胜" % G_NAME[g.split('.')[0]], file=f)
            for i, h in enumerate(data['h']):
                print("第%d轮：" % (i + 1), file=f)
                for j, r in enumerate(h):
                    if r['act'] == 'done':
                        if r['a'] == data['p']:
                            print(ACT_DONE % (j + 1, symbol[r['act_a']], r['b'], symbol[r['act_b']], r['tr']), file=f)
                        else:
                            print(ACT_DONE % (j + 1, symbol[r['act_b']], r['a'], symbol[r['act_a']], r['tr']), file=f)
                    elif r['act'] == 'accept_failed':
                        print("%d. TA因为时间资源不足不得不拒绝了%d。" % (j + 1, r['to']), file=f)
                    elif r['act'] == 'request':
                        print("%d. TA向%d发出了%s请求，时间资源%d。" % (j + 1, r['to'], symbol[r['action']], r['tr']),
                              file=f)
                    elif r['act'] == 'deny':
                        print("%d. TA拒绝了%d的博弈请求。" % (j + 1, r['to']), file=f)
