#!/usr/bin/env python

import fileinput,re,sys,csv

REGEX_JOINED = '\[.*\] (.*) \(.*\) Joined as Player \d'
REGEX_DIED = '\[.*\] (.*) Died'

_swap2 = lambda (x,y): (y,x)

def sortbyvalue(dict):
	mdict = map(_swap2, dict.items())
	mdict.sort(reverse=True)
	mdict = map(_swap2, mdict)
	return mdict

def parse():
	playercount = 0
	diedcount = 0
	d = {}
	died_d = {}
	
	regexp = re.compile(REGEX_JOINED)
	regexp2 = re.compile(REGEX_DIED)
	
	for line in fileinput.input(['log.txt']):
		if line.find("Joined as Player") > 0:
			res = regexp.search(line).groups()
			if res is not None:
				name = res[0]
				playercount += 1
				
				try:
					tmp = d[name]
					d[name] += 1
				except:
					d[name] = 1
					
		if line.find("Died") > 0:
			res = regexp2.search(line).groups()
			if res is not None:
				name = res[0]
				diedcount += 1

				try:
					tmp = died_d[name]
					died_d[name] += 1
				except:
					died_d[name] = 1
	
	print "Joins: %d" % playercount
	print "Killed creatures (including players): %d" % diedcount
	print "Unique players seen: %d" % len(d)
	
	writer = csv.writer(sys.stdout)
	print "-----------------------------------------------------------------"
	print "List of players and the number of times they've Joined the server"
	print "-----------------------------------------------------------------"
	for item in sortbyvalue(d):
		writer.writerow(item)

	print "--------------------------------------------"
	print "List of killed creatures (Including players)"
	print "--------------------------------------------"
	for item in sortbyvalue(died_d):
		writer.writerow(item)

if __name__ == "__main__":
	parse()
	
