# The betting game.
# We are going to assume that we are given inputs that either propose
# a bet, take part of a bet, process a result.
# Determine winners and losers.

import sqlite3 as lite
import sys

games = (
	(123, 20150801, 2000, 'Madison Square Garden', 'Knicks', 'Cavalier', '', 'basketball'),
	(456, 20150802, 2000, 'Madison Square Garden', 'Knicks', 'Heat', '', 'basketball')
) 

results = (
	(123, 'LeBron', 'points', 43),
	(123, 'Stroudemayer', 'points', 20),
	(456, 'Stroudemayer', 'points', 30),
	(456, 'DWade', 'points', 14)
)

betdescriptions = (
	(13, 1, 123, 'LeBron points > 30', 1, 0, 1000, 0, 200, -1, -1),
	(14, 1, 123, 'LeBron points > 30; Stroudemayer points < 10', 2, 0, 1000, 0, 300, -1, -1)
)

bettakens =(
	(13,2,100),
	(13,3,200),
	(13,4,250),
	(14,4,100),
	(14,2,300)
)

accounts = (
	(-1,0, 0.0, 'The', 'House'),
	(1,1100.0, 0.0, 'T', 'Man'),
	(2,2200.0, 0.0, 'Dadbod', 'Cashman'),
	(3,3300.0, 0.0, 'Mom', 'Mothership'),
	(4,4400.0, 0.0, 'Cloe', 'Sister'),
)

try:
	con = lite.connect('bet.db')
	cur = con.cursor()
	cur.execute("DROP TABLE IF EXISTS team")
	cur.execute("CREATE TABLE team(team TEXT, player TEXT)")

	cur.execute("DROP TABLE IF EXISTS game")
	cur.execute("CREATE TABLE game(gameid INT, startdate INT, starttime INT, location TEXT, team1 TEXT, team2 TEXT, otherteams TEXT, gametype TEXT)")
	cur.executemany("INSERT INTO game VALUES(?, ?, ?, ?, ?, ?, ?, ?)",games)
	cur.execute("DROP TABLE IF EXISTS tmp")
	cur.execute("CREATE TABLE tmp(gameid INT, startdate INT, starttime INT)")
	cur.execute("INSERT INTO tmp SELECT gameid, startdate, starttime FROM game")

	con.commit()

except lite.Error, e:

    if con:
        con.rollback()

    print "Error %s:" % e.args[0]
    sys.exit(1)

finally:

    if con:
        con.close()
