#open 'data/game_list.csv' for reading
#iterate through all games. For each game:
	#Record result and number of moves
	#use dict where key is length in moves and value is list of dim 3
		#First element is #of white wins for games of that length, 2nd is # of black wins, third, is # of draws
	
#For each key in dict:
	#Find proportion of each result and store in list where i-th element is distribution for i-length games
#Save all of these distributions in a csv so we don't have to repeat computation to see/plot the results again

#Plot that shit and check for a 'drop-off' point where games are more likely to end in draw

import math
import time


dists = {}

t1 = time.time()
with open('data/game_list.csv', 'r') as fp:
	line = fp.readline()
	line = fp.readline()
	
	while line:
		data = line.split(',')
		
		#for datum in data:
		#	if datum[0:2] == '1.':
		#		moves = datum.split()
		
		moves = data[-2].split()
		
		length = math.ceil(len(moves)/3)
		
		result = data[-1].strip()
		
		if result == '1-0':
			print('white')
			if length not in dists:
				dists[length] = [1, 0, 0, 0]
			else:
				dists[length][0] = dists[length][0] + 1
				
		elif result == '0-1':
			print('black')
			if length not in dists:
				dists[length] = [0, 1, 0, 0]
			else:
				dists[length][1] = dists[length][1] + 1
				
		elif result == '1/2-1/2':
			print('draw')
			if length not in dists:
				dists[length] = [0, 0, 1, 0]
			else:
				dists[length][2] = dists[length][2] + 1
				
		elif result == '*':
			print('unknown')
			if length not in dists:
				dists[length] = [0, 0, 0, 1]
			else:
				dists[length][3] = dists[length][3] + 1
				
		else:
			print('ERROR! UNKNOWN RESULT: {}'.format(result))
			exit(0)
	
		line = fp.readline()
t2 = time.time()

print('Time to count game results: {}. Writing...'.format(t2-t1))
			
t3 = time.time()
with open('data/simple_dists.csv', 'w') as fp:
	fp.write('length,white,black,draw,unknown\n')
	for key in dists:
		fp.write('{},{},{},{},{}\n'.format(key, dists[key][0], dists[key][1], dists[key][2], dists[key][3]))
t4 = time.time()
		
print('Time to write: {}. Thank you for using this shitty code.'.format(t4-t3))