import psql,init_webgo,helper,gamecreation
from sets import Set as set
from mod_python import *
def process_form(req,form):
"""
reads username and password from form
"""
#req.write("
"+"name="+form['name']+", password="+form['password']+"
")
if form.keys() != []:
if ("username" in form.keys()) and ("password" in form.keys()):
#extract name and password
username = form["username"]
password = form["password"]
sessionid = form["sessionid"]
origpassword = psql.get_user_info(username,"password")
#debug:
helper.debug(req,form,'
--password:'+str(password)+' ---origpassword:'+str(origpassword)+'
')
#check if user exists (else we would get an error string)
if origpassword != "no such user": #no error message, now check password
if password == origpassword:
#login accepted
psql.set_user_sessionid(username,sessionid)
psql.set_user_timeout(username)
#now display list of games.
game_overview_form(req,form)
else:
req.write("Login incorrect. Please try again.
")
req.write(login_form())
else: #no such user
req.write("Login incorrect. Please try again.-
")
req.write(login_form())
else: #one of (name,password) is missing:
req.write("Please enter your name and password.")
req.write(login_form())
def game_overview_form(req,form):
"""
gets: request object, util.FieldStorage form, name of user, sessionid.
prints a form with the option to select,create and delete games.
"""
username = form["username"]
sessionid = form["sessionid"]
data = helper.header()+ """
Current Games:
"""
gamelist = psql.get_user_game_list(username)
#debug
helper.debug(req,form,str(gamelist)+"
\n")
#display list of current games
counter = 10 - len(helper.clean_list(gamelist))
tmp = ""
if helper.clean_list(gamelist) != []:
tmp += " | Name of game | White | "
tmp += "Black | Time of Creation | Time of last move | "
tmp += " |
"
for item in set(helper.clean_list(gamelist)):
if (item != None) and (item != "None"):
tmp += '
\n'
if helper.clean_list(gamelist) != []:
tmp += '
'
if gamelist == []: #no current games
data += "You don't have any running games.\n"
else:
data += tmp
#now comes the option for creating new games.
data += "New Game:
\n"
if counter > 0:
data+= "You have %s free game slots.
" % counter
data += """
""" % (sessionid, username)
else:
data+= "Sorry, all your game slots are in use."
data+=helper.footer()
req.write(data)
def login_form():
"""
print welcome message and html form.
"""
data = helper.header() + """
""" % helper.generate_session_id()
data += helper.footer()
return data
def navigation_bar(req,form):
"""
gets request object and util.FieldStorage form.
writes the following to req:
- a button to return to the game overview
- a logout button
returns string
"""
username = form["username"]
sessionid = form["sessionid"]
game = form["game"]
#TODO: buttons
data="""
""" % (username,sessionid,game)
return(data)
def logout(req,form):
"""
gets request object and util.FieldStorage form.
reads username from form and clears timeout and sessionid from users table.
"""
username = form["username"]
psql.set_user_sessionid(username,"")
psql.set_user_timeout(username,"")
def main(req,form):
#debug
helper.debug(req,form)
#req.write(helper.footer())
if "sessionid" not in form.keys():
req.write(login_form())
elif ("game overview" in form.keys()) and ("username" in form.keys()) and ("sessionid" in form.keys()):
game_overview_form(req,form)
elif ("logout" in form.keys()) and ("username" in form.keys()):
logout(req,form)
process_form(req,form)
else:
process_form(req,form)