

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


urls = ('/chat','chat_server',
		'/football','football_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
	chat = []
	users = {}
	colors = ['red','blue','green']
	football_messages = []


lastx = ""
class football_server:
	def GET(self):
		return render.football()

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

		# print "Action is: ", action
		if action == 'send':
			message = form['message']
			Constants.football_messages.append([message, web.ctx['ip']])
			print "message sent: ", Constants.football_messages
			if message =='clear':
				Constants.football_messages=[]
			return 'action/send' + str(len(Constants.football_messages))
		if action == 'check':
			if 2 == len(Constants.football_messages):
				x =  '<h1>' + generateoutcomedb(Constants.football_messages) + '</h1>'
				# Constants.football_messages = [] # erases things too quickly, which may be find for final deployment
				if (lastx != x):
					print "x is ", x
				return x
			else:
				if  3 == len(Constants.football_messages):
				# In this way, we don't just keep adding
				# to Constants.football_messages.
					ips = [ip for ip,message in Constants.football_messages]
					print "ips: ", ips
					print "messages: ", Constants.football_messages
					Constants.football_messages = [Constants.football_messages[-1]]
				return 'action/check' + str(len(Constants.football_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):
   out = mymessage[0][0] + ' at ' + mymessage[0][1] + '_____' + mymessage[1][0] + ' at ' + mymessage[1][1] + '<p>'
   con = lite.connect('football.db')

   with con:    
       
       cur = con.cursor()    
       # cur.execute("SELECT * FROM play ")
       cur.execute("SELECT * FROM play ")
   
       rows = cur.fetchall()
   
       for row in rows[:3]:
          out+= ' Offensive play: '+ row[0]+ ' Defensive play: '+ row[1]+ ' yards gained: '+ str(row[2]) + '<br>'

   return out

# Unused for now
class chat_server:
	def GET(self):
		return render.chatroom("Change this to change the tab title!")

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

		if action == 'submit':
			if ip not in Constants.users.keys():
				Constants.users[ip]= Constants.colors.pop()
			message = form['message']
			if message.strip():
				message= message.replace('<',"&lt;").replace('>',"&gt;")
				element = "<div style='color:{0}'>{1}: {2}</div>".format(Constants.users[ip], ip, message)
				Constants.chat.append(element)
		return '<br>'.join(Constants.chat)


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




