import json
from pathlib import Path
from island.match import Match



result = {}

for file in Path('wos-data-new').iterdir():
    p = Path(file)
    if p.suffix == '.json':
        name = p.stem
        m = Match.read_from_json(str(file))
        info = m.query('game', 'created').select('info').first()['info']
        conf = json.loads(info['config'])
        game_end_at = int(info['game_end_at'])
        players = []
        foods = {}
        for p in m.query('player', 'join').select('pid').raw_data:
            foods[p['pid']] = conf['start_resource']
        
        for i in range(1, game_end_at+1):
            for a in m.query('action', 'done').where(lambda x: x['rno'] == i).raw_data:
                foods[a['a']] += conf['payoffs']["%s%s"%(a['act_a'], a['act_b'])][0] * a['tr'] / 1440.0
                foods[a['b']] += conf['payoffs']["%s%s"%(a['act_a'], a['act_b'])][1] * a['tr'] / 1440.0
            for j in foods.keys():
                foods[j] -= conf['rounds']['consumption']
        
        for j in foods.keys():
            if foods[j] > 0:
                players.append(j)
        
        result[name] = players

print(json.dumps(result))