#import database,init_webgo,helper,gamecreation
import helper,database
from sets import Set as set
from cherrypy import cpg
from cherrypy.lib import httptools
"""
"""
"""
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"]
myuser = database.Users.byUsername(username)
myuser.sessionid = ""
myuser.timeout = 0
def main(req,form):
#debug
helper.debug(req,form)
#req.write(helper.footer())
if "sessionid" not in form.keys():
req.write(login_form(req,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)
"""
class Login:
def index(self,username=None,password=None):
"""
Without arguments, this function writes a login form. Else the login data is evaluated
and the user is eventually logged in.
"""
if username == None and password == None:
return self.login_form()
else:
return self.process_form(username,password)
def login_form(self):
"""
print welcome message and html form.
"""
data = helper.header()
data += """
create account
"""
data += helper.footer()
return data
def process_form(self,username,password):
"""
gets username and password, checks for validity, eventually the user is logged in.
TODO: check for session timeout
"""
data = helper.header()
sessionid = cpg.request.sessionMap["_sessionId"]
myuser = database.Users.byUsername(username)
origpassword = myuser.password
if password == origpassword:
#login accepted
myuser.sessionid = sessionid
myuser.set_timeout()
cpg.request.sessionMap["username"] = username
#now display list of games.
httptools.redirect("/overview")
#data += "Login accepted."
else:
data += "Login incorrect. Please try again.
"
data += self.login_form()
data += helper.footer()
return data
index.exposed=True
class Overview:
"""
give out a list of current games and stuff."""
def index(self):
username = cpg.request.sessionMap["username"]
myuser = database.Users.byUsername(username)
sessionid = cpg.request.sessionMap["_sessionId"]
if myuser.sessionid == sessionid:
return self.game_overview_form(username)
else:
return "You must be logged in to access this page."
def game_overview_form(self,username):
"""
gets:name of user,
prints a form with the option to select,create and delete games.
"""
myuser = database.Users.byUsername(username)
data = helper.header()+ """
Current Games:
"""
gamelist = myuser.gamelist()
#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 gamelist:
if (item != None) and (item != "None"):
tmp += '\n
\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 += ' Start a new game
'
else:
data+= "Sorry, all your game slots are in use."
data+=helper.footer()
return data
index.exposed = True