#!/usr/bin/python

"""
	Record and/or return statistics about HtR deaths.
	Uses a Cookie to identify players.
"""

import sys
import os
import cgi
import Cookie
import sqlite3
#import cgitb
#cgitb.enable()

import helpers



if __name__ == "__main__":
	
	#sys.stderr = open(sys.argv[0]+".err", "w");

	site = helpers.GetSite()

	# Setup database if it doesn't exist
	try:
		f = open("stats.db", "r")
	except IOError:
		conn = sqlite3.connect("stats.db")
		c = conn.cursor()
		c.execute("CREATE TABLE stats (identity, start, runtime, steps, death, x, y, foxesSquished, foxesDazed, boxesSquished)")
		conn.commit()
		conn.close()
	else:
		f.close()

	# Get the cookie
	identity = helpers.GetIdentity()


	# Get CGI fields
	form = cgi.FieldStorage()
	fields = ["start", "runtime", "steps", "death", "x", "y", "foxesSquished", "foxesDazed", "boxesSquished"]


	values = []
	for f in fields:
		if form.has_key(f):
			try:
				v = float(form[f].value)
			except:
				v = form[f].value
			values += [v]

	# Send response
	if len(values) == 0:
		print("Content-type: text/plain\n")
		print("# You are: %s\n" % identity);
		print("# Get the full SQlite DB from: %s\n" % (site+"/stats.db"))
		print("# Expecting plots? This is the wrong place. Visit: %s\n" %(site+"/view.py?query=highscores"))
		print("# TOP 100 Runs\n")
		print("# identity\t" + "\t".join(fields)+"\n")
		conn = sqlite3.connect("stats.db")
		cursor = conn.cursor()
		for row in cursor.execute("SELECT * FROM stats ORDER BY runtime DESC LIMIT 100"):
			s = ""
			for field in row:
				s += str(field) + "\t"
			print(s)

		conn.close()
		sys.exit(0)
	elif len(values) != len(fields):
		print("Content-type: text/plain\n")
		#sys.stderr.write("Wrong number of fields\n");
		sys.exit(0)

	# Someone could just leave their browser on the page not actually playing.
	# So only record games >10s
	elif int(form["runtime"].value) < 10000:

		print("Content-type: text/plain\n")
		sys.exit(0)

	conn = sqlite3.connect("stats.db")
	cursor = conn.cursor()
	# Check size of DB
	cursor.execute("SELECT Count(*) FROM stats")
	count = cursor.fetchall()[0][0]

	# Delete earliest record if necessary
	if count > 5000:
		cursor.execute("DELETE FROM stats WHERE start = (SELECT MIN(start) FROM stats)")


	cursor.execute("INSERT INTO stats VALUES (?"+"".join([",?" for _ in xrange(len(fields))])+")", [identity] + values)
	
	conn.commit()
	conn.close()

	print("Content-type: text/plain\n")
	sys.exit(0)

#!/usr/bin/python

"""
	Record and/or return statistics about HtR deaths.
	Uses a Cookie to identify players.
"""

import sys
import os
import cgi
import Cookie
import sqlite3
#import cgitb
#cgitb.enable()

import helpers



if __name__ == "__main__":
	
	#sys.stderr = open(sys.argv[0]+".err", "w");

	site = helpers.GetSite()

	# Setup database if it doesn't exist
	try:
		f = open("stats.db", "r")
	except IOError:
		conn = sqlite3.connect("stats.db")
		c = conn.cursor()
		c.execute("CREATE TABLE stats (identity, start, runtime, steps, death, x, y, foxesSquished, foxesDazed, boxesSquished)")
		conn.commit()
		conn.close()
	else:
		f.close()

	# Get the cookie
	identity = helpers.GetIdentity()


	# Get CGI fields
	form = cgi.FieldStorage()
	fields = ["start", "runtime", "steps", "death", "x", "y", "foxesSquished", "foxesDazed", "boxesSquished"]


	values = []
	for f in fields:
		if form.has_key(f):
			try:
				v = float(form[f].value)
			except:
				v = form[f].value
			values += [v]

	# Send response
	if len(values) == 0:
		print("Content-type: text/plain\n")
		print("# You are: %s\n" % identity);
		print("# Get the full SQlite DB from: %s\n" % (site+"/stats.db"))
		print("# Expecting plots? This is the wrong place. Visit: %s\n" %(site+"/view.py?query=highscores"))
		print("# TOP 100 Runs\n")
		print("# identity\t" + "\t".join(fields)+"\n")
		conn = sqlite3.connect("stats.db")
		cursor = conn.cursor()
		for row in cursor.execute("SELECT * FROM stats ORDER BY runtime DESC LIMIT 100"):
			s = ""
			for field in row:
				s += str(field) + "\t"
			print(s)

		conn.close()
		sys.exit(0)
	elif len(values) != len(fields):
		print("Content-type: text/plain\n")
		#sys.stderr.write("Wrong number of fields\n");
		sys.exit(0)

	# Someone could just leave their browser on the page not actually playing.
	# So only record games >10s
	elif int(float(form["runtime"].value)) < 10000:

		print("Content-type: text/plain\n")
		sys.exit(0)

	conn = sqlite3.connect("stats.db")
	cursor = conn.cursor()
	# Check size of DB
	cursor.execute("SELECT Count(*) FROM stats")
	count = cursor.fetchall()[0][0]

	# Delete earliest record if necessary
	if count > 5000:
		cursor.execute("DELETE FROM stats WHERE start = (SELECT MIN(start) FROM stats)")


	cursor.execute("INSERT INTO stats VALUES (?"+"".join([",?" for _ in xrange(len(fields))])+")", [identity] + values)
	
	conn.commit()
	conn.close()

	print("Content-type: text/plain\n")
	sys.exit(0)

Fix string -> int conversion in stats.py