import json
import glob

played = {}

for fn in glob.glob("*.json", recursive=False):
    with open(fn, encoding='utf-8') as f:
        data = json.load(f)
    for r in data:
        if r['cat'] == 'player' and r['act'] == 'join':
            if r['uid'] not in played:
                played[r['uid']] = 0
            played[r['uid']] += 1

kv = {}

with open('s.csv', encoding='utf-8') as f:
    for l in f:
        r = l.strip().split(',')
        kv[f'{r[1].upper()}-{r[2].upper()}'] = dict(uid=int(r[0]),rest=r[3:])

with open('u.csv', encoding='utf-8') as fin:
    with open('o.csv', 'w', encoding='utf-8') as fout:
        for l in fin:
            r = l.strip().split(',')
            key=f'{r[-1].upper()}-{r[-2].upper()}'
            if key not in kv:
                print(f'!!!!!!MISSING #{key}')
                continue
            uid = kv[key]['uid']
            out = ','.join([l.strip(),f'{uid}',f'{played[uid]}',*kv[key]['rest']])
            fout.write(out + '\n')
            print(out)