2005-09-11 19:11:09 +02:00
|
|
|
import psql,init_webgo,helper,gamecreation
|
2005-10-09 23:29:29 +02:00
|
|
|
from sets import Set as set
|
2005-09-06 19:52:46 +02:00
|
|
|
from mod_python import *
|
|
|
|
|
|
|
|
def process_form(req,form):
|
|
|
|
"""
|
2005-09-12 13:28:11 +02:00
|
|
|
reads username and password from form
|
2005-09-06 19:52:46 +02:00
|
|
|
"""
|
|
|
|
#req.write("<br>"+"name="+form['name']+", password="+form['password']+"<hr>")
|
|
|
|
if form.keys() != []:
|
2005-09-20 11:16:13 +02:00
|
|
|
if ("username" in form.keys()) and ("password" in form.keys()):
|
2005-09-06 19:52:46 +02:00
|
|
|
#extract name and password
|
2005-09-20 11:16:13 +02:00
|
|
|
username = form["username"]
|
2005-09-06 19:52:46 +02:00
|
|
|
password = form["password"]
|
|
|
|
sessionid = form["sessionid"]
|
2005-09-20 11:16:13 +02:00
|
|
|
origpassword = psql.get_user_info(username,"password")
|
2005-09-20 09:05:05 +02:00
|
|
|
#debug:
|
|
|
|
helper.debug(req,form,'<hr>--password:'+str(password)+' ---origpassword:'+str(origpassword)+'<hr>')
|
2005-09-14 16:29:58 +02:00
|
|
|
#check if user exists (else we would get an error string)
|
2005-09-06 19:52:46 +02:00
|
|
|
if origpassword != "no such user": #no error message, now check password
|
|
|
|
if password == origpassword:
|
|
|
|
#login accepted
|
2005-09-20 11:16:13 +02:00
|
|
|
psql.set_user_sessionid(username,sessionid)
|
|
|
|
psql.set_user_timeout(username)
|
2005-09-06 19:52:46 +02:00
|
|
|
#now display list of games.
|
2005-09-20 11:16:13 +02:00
|
|
|
game_overview_form(req,form)
|
2005-09-06 19:52:46 +02:00
|
|
|
else:
|
|
|
|
req.write("Login incorrect. Please try again.<br>")
|
|
|
|
req.write(login_form())
|
|
|
|
else: #no such user
|
2005-09-14 16:29:58 +02:00
|
|
|
req.write("Login incorrect. Please try again.-<br>")
|
2005-09-06 19:52:46 +02:00
|
|
|
req.write(login_form())
|
|
|
|
else: #one of (name,password) is missing:
|
|
|
|
req.write("Please enter your name and password.")
|
|
|
|
req.write(login_form())
|
|
|
|
|
|
|
|
|
2005-09-20 11:16:13 +02:00
|
|
|
def game_overview_form(req,form):
|
2005-09-06 19:52:46 +02:00
|
|
|
"""
|
2005-09-20 09:05:05 +02:00
|
|
|
gets: request object, util.FieldStorage form, name of user, sessionid.
|
2005-09-06 19:52:46 +02:00
|
|
|
prints a form with the option to select,create and delete games.
|
|
|
|
"""
|
2005-09-20 11:16:13 +02:00
|
|
|
username = form["username"]
|
|
|
|
sessionid = form["sessionid"]
|
|
|
|
|
2005-10-09 23:29:29 +02:00
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
data = helper.header()+ """
|
|
|
|
<h2> Current Games: </h2>
|
2005-10-09 23:29:29 +02:00
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
"""
|
2005-09-20 11:16:13 +02:00
|
|
|
gamelist = psql.get_user_game_list(username)
|
2005-09-20 09:05:05 +02:00
|
|
|
#debug
|
|
|
|
helper.debug(req,form,str(gamelist)+"<hr>\n")
|
2005-09-06 19:52:46 +02:00
|
|
|
#display list of current games
|
2005-10-09 23:29:29 +02:00
|
|
|
counter = 10 - len(helper.clean_list(gamelist))
|
2005-09-06 19:52:46 +02:00
|
|
|
tmp = ""
|
2005-10-09 23:29:29 +02:00
|
|
|
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>"
|
|
|
|
for item in set(helper.clean_list(gamelist)):
|
2005-09-06 19:52:46 +02:00
|
|
|
if (item != None) and (item != "None"):
|
2005-10-09 23:29:29 +02:00
|
|
|
tmp += '<tr><form method="post">\n'
|
|
|
|
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
|
|
|
|
tmp += '<input type="hidden" name="username" value="%s">\n' % username
|
|
|
|
tmp += '<input type="hidden" name="game" value="%s">\n' % item
|
2005-10-09 23:43:20 +02:00
|
|
|
tmp += '<td><input type=submit class="submit" name="play" value="Play"></td>\n'
|
2005-10-09 23:29:29 +02:00
|
|
|
description = psql.get_description(item)
|
|
|
|
if (description == None) or (description == "None") or (description == ""):
|
|
|
|
description = item
|
|
|
|
tmp += '<td>%s</td>\n' % description
|
|
|
|
players = psql.get_players_for_game(item)
|
|
|
|
tmp += '<td>%s</td>\n' % players[0]
|
|
|
|
tmp += '<td>%s</td>\n' % players[1]
|
|
|
|
tmp += '<td>%s</td>\n' % helper.format_time(psql.get_time(item,"created"))
|
|
|
|
tmp += '<td>%s</td>\n' % helper.format_time(psql.get_time(item,"lastmove"))
|
2005-10-09 23:43:20 +02:00
|
|
|
tmp += '<td><input type=submit class="submit" name="delete" value="Delete"></td>\n'
|
2005-10-09 23:29:29 +02:00
|
|
|
tmp += '</tr></form>\n'
|
|
|
|
if helper.clean_list(gamelist) != []:
|
|
|
|
tmp += '</table>'
|
|
|
|
|
|
|
|
if gamelist == []: #no current games
|
2005-09-06 19:52:46 +02:00
|
|
|
data += "You don't have any running games.\n"
|
|
|
|
else:
|
2005-10-09 23:29:29 +02:00
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
data += tmp
|
|
|
|
#now comes the option for creating new games.
|
2005-09-20 09:05:05 +02:00
|
|
|
data += "<h2>New Game:</h2>\n"
|
2005-09-06 19:52:46 +02:00
|
|
|
if counter > 0:
|
|
|
|
data+= "You have %s free game slots.<br>" % counter
|
|
|
|
data += """
|
2005-09-12 13:28:11 +02:00
|
|
|
<form method="post">
|
2005-09-06 19:52:46 +02:00
|
|
|
<input type="hidden" name="sessionid" value="%s">
|
|
|
|
<input type="hidden" name="username" value="%s">
|
2005-10-09 23:43:20 +02:00
|
|
|
<input type=submit class="submit" name="create" value="Start a new game">
|
2005-09-06 19:52:46 +02:00
|
|
|
</form>
|
2005-09-20 11:16:13 +02:00
|
|
|
""" % (sessionid, username)
|
2005-09-06 19:52:46 +02:00
|
|
|
else:
|
|
|
|
data+= "Sorry, all your game slots are in use."
|
|
|
|
data+=helper.footer()
|
2005-09-14 17:08:44 +02:00
|
|
|
req.write(data)
|
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
def login_form():
|
|
|
|
"""
|
|
|
|
print welcome message and html form.
|
|
|
|
"""
|
|
|
|
|
2005-09-12 19:30:02 +02:00
|
|
|
data = helper.header() + """
|
2005-09-06 19:52:46 +02:00
|
|
|
<form method="post">
|
|
|
|
<p>Name:<br>
|
2005-10-09 23:43:20 +02:00
|
|
|
<input name="username" type="text" class="text" size="20"></p>
|
2005-10-03 23:45:39 +02:00
|
|
|
<p>Password:<br>
|
2005-10-09 23:43:20 +02:00
|
|
|
<input name="password" type="text" class="text" size="20"></p>
|
2005-09-06 19:52:46 +02:00
|
|
|
<input type="hidden" name="sessionid" value="%s">
|
2005-10-09 23:43:20 +02:00
|
|
|
<p><input type="submit" class="submit" value="login"></p>
|
2005-09-06 19:52:46 +02:00
|
|
|
</form>
|
2005-10-03 23:45:39 +02:00
|
|
|
<form method="post">
|
|
|
|
<input type="hidden" name="createaccount" value="init">
|
2005-10-09 23:43:20 +02:00
|
|
|
<input type="submit" class="submit" value="create account">
|
2005-10-03 23:45:39 +02:00
|
|
|
</form>
|
2005-09-06 19:52:46 +02:00
|
|
|
""" % helper.generate_session_id()
|
|
|
|
data += helper.footer()
|
|
|
|
return data
|
|
|
|
|
2005-09-20 11:51:12 +02:00
|
|
|
def navigation_bar(req,form):
|
2005-09-20 09:05:05 +02: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-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
|
|
|
|
data="""
|
|
|
|
<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-09-20 11:51:12 +02: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):
|
|
|
|
"""
|
|
|
|
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,"")
|
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():
|
|
|
|
req.write(login_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)
|