swdata/calc_coopr_rate.py
2018-12-28 23:36:24 +08:00

43 lines
1.2 KiB
Python

from island.match import Match
from island.matches import Matches
import numpy as np
import csv
import json
def calcAndSave(mode):
matches = Matches.from_profile(mode)
times = np.zeros(15)
cs = np.zeros(15)
crs = []
for m in matches.data:
maxr = int(m.query('game', 'created').first()['info']['game_end_at'])
c = [0]*maxr
for r in range(1, maxr+1):
rows = m.query('action', 'done').where(lambda x: x['rno'] == r).raw_data
coop = 0
for row in rows:
if row['act_a'] == 'C':
coop += 1
if row['act_b'] == 'C':
coop += 1
if rows:
times[r-1] += len(rows) * 2
cs[r-1] += coop
c[r - 1] = coop / (len(rows) * 2)
crs.append(c)
for i in range(15):
if(times[i] == 0):
times[i] = 1
cs /= times
with open("outputs/CR_%s.csv"%mode, 'w') as f:
csv.writer(f).writerow(cs)
with open("outputs/CRAW_%s.json"%mode, 'w') as f:
json.dump(crs, f)
if __name__ == '__main__':
calcAndSave('CLASSIC')
calcAndSave('SURVIVE')