2005-11-24 16:39:22 +01:00
|
|
|
#import database,init_webgo,helper,gamecreation
|
|
|
|
import helper,database
|
2005-10-09 23:29:29 +02:00
|
|
|
from sets import Set as set
|
2005-11-24 16:39:22 +01:00
|
|
|
from cherrypy import cpg
|
|
|
|
from cherrypy.lib import httptools
|
|
|
|
"""
|
2005-09-06 19:52:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-11-24 16:39:22 +01:00
|
|
|
"""
|
|
|
|
"""
|
2005-09-20 11:51:12 +02:00
|
|
|
def navigation_bar(req,form):
|
2005-11-24 16:39:22 +01:00
|
|
|
"" "
|
2005-09-20 11:51:12 +02:00
|
|
|
gets request object and util.FieldStorage form.
|
2005-09-20 09:05:05 +02:00
|
|
|
writes the following to req:
|
|
|
|
- a button to return to the game overview
|
|
|
|
- a logout button
|
|
|
|
returns string
|
2005-11-24 16:39:22 +01:00
|
|
|
" ""
|
2005-09-20 11:51:12 +02:00
|
|
|
username = form["username"]
|
|
|
|
sessionid = form["sessionid"]
|
|
|
|
game = form["game"]
|
2005-09-20 09:05:05 +02:00
|
|
|
#TODO: buttons
|
2005-11-24 16:39:22 +01:00
|
|
|
data=" ""
|
2005-09-20 09:05:05 +02:00
|
|
|
<form method="post">
|
|
|
|
<input type="hidden" name="username" value="%s">
|
2005-09-20 11:16:13 +02:00
|
|
|
<input type="hidden" name="sessionid" value="%s">
|
2005-09-20 11:51:12 +02:00
|
|
|
<input type="hidden" name="game" value="%s">
|
2005-10-09 23:43:20 +02:00
|
|
|
<input type="submit" class="submit" name="logout" value="logout">
|
|
|
|
<input type="submit" class="submit" name="game overview" value="game overview">
|
|
|
|
<input type="submit" class="submit" name="refresh" value="refresh">
|
2005-09-20 09:05:05 +02:00
|
|
|
</form>
|
2005-11-24 16:39:22 +01:00
|
|
|
"" " % (username,sessionid,game)
|
2005-09-20 09:05:05 +02:00
|
|
|
return(data)
|
|
|
|
|
|
|
|
|
2005-09-20 11:51:12 +02:00
|
|
|
def logout(req,form):
|
2005-11-24 16:39:22 +01:00
|
|
|
"" "
|
2005-09-20 11:51:12 +02:00
|
|
|
gets request object and util.FieldStorage form.
|
|
|
|
reads username from form and clears timeout and sessionid from users table.
|
2005-11-24 16:39:22 +01:00
|
|
|
"" "
|
2005-09-20 11:51:12 +02:00
|
|
|
username = form["username"]
|
2005-11-23 18:34:20 +01:00
|
|
|
myuser = database.Users.byUsername(username)
|
|
|
|
myuser.sessionid = ""
|
|
|
|
myuser.timeout = 0
|
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
|
2005-09-12 13:28:11 +02:00
|
|
|
def main(req,form):
|
2005-09-20 09:05:05 +02:00
|
|
|
#debug
|
|
|
|
helper.debug(req,form)
|
|
|
|
#req.write(helper.footer())
|
2005-09-06 19:52:46 +02:00
|
|
|
if "sessionid" not in form.keys():
|
2005-10-10 00:11:29 +02:00
|
|
|
req.write(login_form(req,form))
|
2005-09-20 09:05:05 +02:00
|
|
|
elif ("game overview" in form.keys()) and ("username" in form.keys()) and ("sessionid" in form.keys()):
|
2005-09-20 11:16:13 +02:00
|
|
|
game_overview_form(req,form)
|
2005-09-20 11:51:12 +02:00
|
|
|
elif ("logout" in form.keys()) and ("username" in form.keys()):
|
|
|
|
logout(req,form)
|
|
|
|
process_form(req,form)
|
2005-09-06 19:52:46 +02:00
|
|
|
else:
|
|
|
|
process_form(req,form)
|
2005-11-24 16:39:22 +01:00
|
|
|
"""
|
|
|
|
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 += """
|
|
|
|
<form method="post">
|
|
|
|
<p>Name:<br>
|
|
|
|
<input name="username" type="text" class="text" size="20"></p>
|
|
|
|
<p>Password:<br>
|
|
|
|
<input name="password" type="text" class="text" size="20"></p>
|
|
|
|
<p><input type="submit" class="submit" value="login"></p>
|
|
|
|
</form>
|
|
|
|
<a href="/createaccount"> create account</a><br>
|
|
|
|
"""
|
|
|
|
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.<br>"
|
|
|
|
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()+ """
|
|
|
|
<h2> Current Games: </h2>
|
|
|
|
|
|
|
|
"""
|
|
|
|
gamelist = myuser.gamelist()
|
|
|
|
#display list of current games
|
|
|
|
counter = 10 - len(helper.clean_list(gamelist))
|
|
|
|
tmp = ""
|
|
|
|
if helper.clean_list(gamelist) != []:
|
|
|
|
tmp += "<table><tr><td></td><td>Name of game</td><td>White</td>"
|
|
|
|
tmp += "<td>Black</td><td>Time of Creation</td><td>Time of last move</td>"
|
|
|
|
tmp += "<td></td></tr>"
|
2005-11-24 19:31:33 +01:00
|
|
|
for item in gamelist:
|
2005-11-24 16:39:22 +01:00
|
|
|
if (item != None) and (item != "None"):
|
2005-11-24 19:31:33 +01:00
|
|
|
tmp += '<tr>\n<form action="/playgame/" method="get">\n'
|
2005-11-24 16:39:22 +01:00
|
|
|
tmp += '<input type="hidden" name="game" value="%s">\n' % item
|
2005-11-24 19:31:33 +01:00
|
|
|
tmp += '<td><input type=submit class="submit" value="Play"></td>\n'
|
2005-11-24 16:39:22 +01:00
|
|
|
mygame = database.GobanTable.get(item)
|
|
|
|
description = mygame.description
|
|
|
|
if (description == None) or (description == "None") or (description == ""):
|
|
|
|
description = item
|
|
|
|
tmp += '<td>%s</td>\n' % description
|
|
|
|
players = mygame.players()
|
|
|
|
tmp += '<td>%s</td>\n' % players[0]
|
|
|
|
tmp += '<td>%s</td>\n' % players[1]
|
|
|
|
tmp += '<td>%s</td>\n' % helper.format_time(mygame.created)
|
|
|
|
tmp += '<td>%s</td>\n' % helper.format_time(mygame.lastmove)
|
|
|
|
tmp += '<td><input type=submit class="submit" name="delete" value="Delete"></td>\n'
|
|
|
|
tmp += '</tr></form>\n'
|
|
|
|
if helper.clean_list(gamelist) != []:
|
|
|
|
tmp += '</table>'
|
|
|
|
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 += "<h2>New Game:</h2>\n"
|
|
|
|
if counter > 0:
|
|
|
|
data+= "You have %s free game slots.<br>" % counter
|
|
|
|
data += '<a href="/gamecreation"> Start a new game</a><br>'
|
|
|
|
else:
|
|
|
|
data+= "Sorry, all your game slots are in use."
|
|
|
|
data+=helper.footer()
|
|
|
|
return data
|
|
|
|
index.exposed = True
|