130 lines
4 KiB
Python
Executable file
130 lines
4 KiB
Python
Executable file
import psql,init_webgo,helper
|
||
import time
|
||
from mod_python import *
|
||
|
||
def process_form(req,form):
|
||
"""
|
||
reads username and passsword from form
|
||
"""
|
||
#if form == empty (which means if page is displayed for the first time):
|
||
req.write(str(form.keys()))
|
||
#req.write("<br>"+"name="+form['name']+", password="+form['password']+"<hr>")
|
||
if form.keys() != []:
|
||
|
||
if ("name" in form.keys()) and ("password" in form.keys()):
|
||
#extract name and password
|
||
name = form["name"]
|
||
password = form["password"]
|
||
sessionid = form["sessionid"]
|
||
origpassword = psql.get_user_info(name,"password")
|
||
#check if user exists (else we would get an error string)
|
||
if origpassword != "no such user": #no error message, now check password
|
||
req.write("hier bin ich in der Schleife.<br>")
|
||
req.write(str(origpassword)+"<br>")
|
||
if password == origpassword:
|
||
#login accepted
|
||
psql.set_user_sessionid(name,sessionid)
|
||
psql.set_user_timeout(name)
|
||
#now display list of games.
|
||
game_overview_form(req,name,sessionid)
|
||
else:
|
||
req.write("Login incorrect. Please try again.<br>")
|
||
req.write(login_form())
|
||
else: #no such user
|
||
req.write("Login incorrect. Please try again.<br>")
|
||
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,user,sessionid):
|
||
"""
|
||
gets the name of a user and the queue of games.
|
||
prints a form with the option to select,create and delete games.
|
||
"""
|
||
data = helper.header()+ """
|
||
<h2> Current Games: </h2>
|
||
<form method="post">
|
||
"""
|
||
gamelist = psql.get_user_game_list(user)
|
||
req.write(str(gamelist)+"<hr>\n")
|
||
#display list of current games
|
||
counter = 10
|
||
tmp = ""
|
||
for item in gamelist:
|
||
if (item != None) and (item != "None"):
|
||
tmp += '<input type="radio"> name="game" value="%s"> %s\n' % (item,item)
|
||
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
|
||
tmp += '<input type="hidden" name="username" value="%s">\n' % user
|
||
#later write partners name and whether its our turn or not
|
||
#data += "Your partner: %s."
|
||
tmp +="<br>"
|
||
counter -= 1
|
||
if tmp == "": #no current games
|
||
data += "You don't have any running games.\n"
|
||
else:
|
||
data += tmp
|
||
data += '<input type=submit name="play" value="Play selected game">\n<input type=submit name="delete" value="Delete selected game">\n</form>'
|
||
#now comes the option for creating new games.
|
||
data += "<h2>Start a new Game</h2>\n"
|
||
if counter > 0:
|
||
data+= "You have %s free game slots.<br>" % counter
|
||
data += """
|
||
<form>
|
||
<input type=submit name="create" value="Start a new game">
|
||
<input type="hidden" name="sessionid" value="%s">
|
||
<input type="hidden" name="username" value="%s">
|
||
|
||
</form>
|
||
""" % (sessionid, user)
|
||
else:
|
||
data+= "Sorry, all your game slots are in use."
|
||
#display "You have ... free game slots" + Button "create game"
|
||
#bei "create game neue funktion zum Erstellen + Mitspieler aus Liste ausw<73>hlen.
|
||
data+=helper.footer()
|
||
#check for timeout:
|
||
if (psql.get_user_info(user,'timeout') >= int(time.time())) and (sessionid == psql.get_user_info(user,'sessionid')):
|
||
req.write(data)
|
||
else:
|
||
req.write(helper.header()+ 'your session timed out'+helper.footer())
|
||
|
||
def login_form():
|
||
"""
|
||
print welcome message and html form.
|
||
"""
|
||
data = helper.header() + """
|
||
|
||
<form method="post">
|
||
<p>Name:<br>
|
||
<input name="name" size="40"></p>
|
||
<p>Pasword:<br>
|
||
<input name="password" size="40"></p>
|
||
<input type="hidden" name="sessionid" value="%s">
|
||
<p><input type="submit" value="login"></p>
|
||
</form>
|
||
""" % helper.generate_session_id()
|
||
data += helper.footer()
|
||
|
||
|
||
return data
|
||
|
||
|
||
def create_new_game(req,user):
|
||
"""
|
||
create a new game for user.
|
||
gets: request object, username.
|
||
"""
|
||
|
||
|
||
|
||
def main(req):
|
||
#get instance of fieldstorage
|
||
form = util.FieldStorage(req)
|
||
if "sessionid" not in form.keys():
|
||
req.write(login_form())
|
||
elif ("create" in form.keys()) and ("username" in form.keys()):
|
||
#user wants to create a new game
|
||
create_new_game(req,form["username"])
|
||
else:
|
||
process_form(req,form)
|