90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
|
from mod_python import *
|
||
|
import psql,helper
|
||
|
|
||
|
def display_game_creation_form(req,sessionid,username):
|
||
|
"""
|
||
|
prints a html form with multiple drop-down lists for choosing players,
|
||
|
goban size and so on.
|
||
|
gets a mod_python request, prints to req.write.
|
||
|
"""
|
||
|
data = helper.header()
|
||
|
p1data = create_user_dropdown_list("playerone",username)
|
||
|
p2data = create_user_dropdown_list("playertwo")
|
||
|
gobansize = create_goban_size_dropdown_list("gobansize")
|
||
|
#start form
|
||
|
#choose player one (black)
|
||
|
#choose player two (white)
|
||
|
#choose goban size
|
||
|
#'hidden' session id and username
|
||
|
data += """
|
||
|
<form method="post">
|
||
|
<p>Player One (Black): %s </p>
|
||
|
<p>Player Two (White): %s </p>
|
||
|
<p>Goban Size: %s fields </p>
|
||
|
<input type="hidden" name="sessionid" value="%s"><p>
|
||
|
<input type="hidden" name="username" value="%s"><p>
|
||
|
<input type="submit" value="create game"></p>
|
||
|
</form>
|
||
|
""" % (p1data,p2data,gobansize,sessionid,username)
|
||
|
|
||
|
|
||
|
|
||
|
data+=helper.footer()
|
||
|
req.write(data)
|
||
|
|
||
|
def process_creation_form(req):
|
||
|
"""
|
||
|
validates and processes the game creation form.
|
||
|
If everything was ok, a game will be created.
|
||
|
"""
|
||
|
#check if at least one of the players is the current user
|
||
|
#create game name
|
||
|
#create_game
|
||
|
#update entries for player one and player two
|
||
|
pass
|
||
|
|
||
|
def create_user_dropdown_list(listname,selected = ""):
|
||
|
"""
|
||
|
gets a name for the generated list and, optionally, the preselected value.
|
||
|
returns a <select> form as string.
|
||
|
"""
|
||
|
userlist = psql.get_users_with_free_game_slots()
|
||
|
data = '<select name="%s">' % listname
|
||
|
for item in userlist:
|
||
|
#check whether current item = item for preselection
|
||
|
if item == selected:
|
||
|
tmp = 'selected'
|
||
|
else:
|
||
|
tmp = ''
|
||
|
data += '<option %s> %s </option>' % (tmp,item)
|
||
|
data += '</select>'
|
||
|
return data
|
||
|
|
||
|
def create_goban_size_dropdown_list(listname):
|
||
|
"""
|
||
|
gets a name, returns a string with a html form for selecting the goban size.
|
||
|
"""
|
||
|
data = """
|
||
|
<select name="%s">
|
||
|
<option> 9 </option>
|
||
|
<option> 13 </option>
|
||
|
<option> 19 </option>
|
||
|
</select>
|
||
|
""" % listname
|
||
|
return data
|
||
|
|
||
|
def main(req,form):
|
||
|
"""
|
||
|
display and process forms for game creation.
|
||
|
gets a request object and a util.FieldStorage form.
|
||
|
returns nothing.
|
||
|
"""
|
||
|
username = form["username"]
|
||
|
#TODO:check if valid session id
|
||
|
sessionid = form["sessionid"]
|
||
|
if "create" in form.keys(): #first call of this function
|
||
|
display_game_creation_form(req,sessionid,username)
|
||
|
else:
|
||
|
process_creation_form(req)
|
||
|
form = util.FieldStorage(req)
|
||
|
req.write(str(form))
|