import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
import numpy as np

matches = Matches.from_profile_expr(lambda r: True)
max_round = 15

total_players = 0
dnc_players = 0

for j in range(len(matches.data)):
    players = set()
    for r in matches.data[j].query('player', 'join').raw_data:
        players.add(r['pid'])
    total_players += len(players)
    for i in range(int(matches.data[j].query('game', 'created').first()['info']['game_end_at'])):
        actions = matches.data[j].query('action', 'done').where(lambda x: x['rno']==i+1)
        for r in actions.raw_data:
            if r['a'] in players:
                if actions.where(lambda y: (y['a'] == r['a'] and y['b'] != r['b']) or (y['b'] == r['a'] and y['a'] != r['b'])).count() > 0:
                    dnc_players += 1
                    players.remove(r['a'])
            if r['b'] in players:
                if actions.where(lambda y: (y['a'] == r['b'] and y['b'] != r['a']) or (y['b'] == r['b'] and y['a'] != r['a'])).count() > 0:
                    dnc_players += 1
                    players.remove(r['b'])
            
    

print("dnc=%d, total=%d, frac=%f" % (dnc_players, total_players, dnc_players / total_players))