

import datetime
import sqlite3 as lite
import sys
import web, random

# APPLICATION-SPECIFIC



urls = ('/bet','bet_server')

# relates after the url /xxx to the server that should be called.

# What does this template.render thing. 
render = web.template.render('templates')


class Constants:
	x = 10
	users = {}
	colors = ['red','blue','green']
	bet_messages = []


count = 0
lastx = ""
class bet_server:
	def GET(self):
		return render.bet()

	def POST(self):
		form = web.input()
		action = form['action']

		# print "Action is: ", action
		if action == 'send':
			message = form['message']
			Constants.bet_messages.append([message, web.ctx['ip']])
			# print "message sent: ", Constants.bet_messages
			if message =='clear':
				Constants.bet_messages=[]
			return 'action/send' + str(len(Constants.bet_messages))
		if action == 'check':
				x =  '<h1>' + generateoutcomedb(Constants.bet_messages) + '</h1>'
				# Constants.bet_messages = [] # erases things too quickly, which may be find for final deployment
				return x
				if  True:
				# In this way, we don't just keep adding
				# to Constants.bet_messages.
					ips = [ip for ip,message in Constants.bet_messages]
					# print "ips: ", ips
					# print "messages: ", Constants.bet_messages
					Constants.bet_messages = [Constants.bet_messages[-1]]
				return 'action/check' + str(len(Constants.bet_messages))

# mymessage is a standin for the incoming plays
# mymessage[0][0] is a play and mymessage[0][1] is the ip address
# mymessage[1][0] is a play and mymessage[1][1] is the ip address
def generateoutcome(mymessage):
   out = '<font size="10">' + mymessage[0][0] + ' at ' + mymessage[0][1] + '_____' + mymessage[1][0] + ' at ' + mymessage[1][1]
   return out


# mymessage is a standin for the incoming plays
# mymessage[0][0] is a play and mymessage[0][1] is the ip address
# mymessage[1][0] is a play and mymessage[1][1] is the ip address
def generateoutcomedb(mymessage):
   global count
   out = 'generating an output' + str(count)
   count+= 1
   con = lite.connect('bet.db')

   with con:    
       
       cur = con.cursor()    
       # cur.execute("SELECT * FROM play ")
       cur.execute("SELECT * FROM game ")
   
       rows = cur.fetchall()
   
       for row in rows[:3]:
          out+= ' Game: '+ str(row[0])+ ' start date: '+ str(row[1]) + ' start time: '+ str(row[2]) + ' location: ' + row[3]  + ' team: ' + row[4] + ' other team: ' + row[5] + ' game type: ' + row[7] + '<br>'

   return out


if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()




