2005-09-11 19:11:09 +02:00
|
|
|
from mod_python import *
|
2005-09-12 15:51:18 +02:00
|
|
|
import psql,helper,login
|
2005-09-11 19:11:09 +02:00
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
DEBUG = 1
|
|
|
|
|
2005-09-11 19:11:09 +02:00
|
|
|
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()
|
2005-09-12 15:51:18 +02:00
|
|
|
p1data = create_user_dropdown_list("player1",username)
|
|
|
|
p2data = create_user_dropdown_list("player2")
|
2005-09-11 19:11:09 +02:00
|
|
|
gobansize = create_goban_size_dropdown_list("gobansize")
|
|
|
|
#start form
|
2005-09-12 15:51:18 +02:00
|
|
|
#choose player one (black),choose player two (white),choose goban size
|
2005-09-11 19:11:09 +02:00
|
|
|
#'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>
|
2005-09-12 19:30:02 +02:00
|
|
|
<input type="hidden" name="sessionid" value="%s">
|
|
|
|
<input type="hidden" name="username" value="%s">
|
2005-09-20 09:05:05 +02:00
|
|
|
<input type="submit" id="submit" name="create" value="create game">
|
2005-09-11 19:11:09 +02:00
|
|
|
</form>
|
|
|
|
""" % (p1data,p2data,gobansize,sessionid,username)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data+=helper.footer()
|
|
|
|
req.write(data)
|
|
|
|
|
2005-09-12 13:28:11 +02:00
|
|
|
def process_creation_form(req,form):
|
2005-09-11 19:11:09 +02:00
|
|
|
"""
|
|
|
|
validates and processes the game creation form.
|
|
|
|
If everything was ok, a game will be created.
|
|
|
|
"""
|
2005-09-12 15:51:18 +02:00
|
|
|
player1 = form["player1"]
|
|
|
|
player2 = form["player2"]
|
|
|
|
username = form["username"]
|
|
|
|
|
2005-09-11 19:11:09 +02:00
|
|
|
#check if at least one of the players is the current user
|
2005-09-12 15:51:18 +02:00
|
|
|
if (player1 == username) or (player2 == username):
|
|
|
|
#create game
|
|
|
|
gamename = psql.create_goban_table(player1,player2,int(form["gobansize"]))
|
2005-09-12 13:28:11 +02:00
|
|
|
#update entries for player one and player two
|
2005-09-12 15:51:18 +02:00
|
|
|
for player in [player1,player2]:
|
|
|
|
gameslot = psql.get_free_game_slot(player)
|
|
|
|
if gameslot != "":
|
|
|
|
psql.set_game_slot(player,gameslot,gamename)
|
|
|
|
else:#should not happen: no free game slot.
|
|
|
|
#print error msg
|
|
|
|
req.write("Error: No free game slots for player "+player+"!")
|
|
|
|
#display form again
|
|
|
|
display_game_creation_form(req,form["sessionid"],username)
|
2005-09-14 16:29:58 +02:00
|
|
|
#TODO:game created, now display game overview form
|
2005-09-20 09:05:05 +02:00
|
|
|
login.game_overview_form(req,form,username,form["sessionid"])
|
2005-09-12 13:28:11 +02:00
|
|
|
else:
|
2005-09-12 15:51:18 +02:00
|
|
|
#give error message
|
|
|
|
req.write("Sorry, you must be one of the players!<br>")
|
|
|
|
#display form again
|
|
|
|
display_game_creation_form(req,form["sessionid"],username)
|
2005-09-11 19:11:09 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2005-09-20 09:05:05 +02:00
|
|
|
if DEBUG:
|
|
|
|
req.write(str(form.keys())+"<hr>")
|
2005-09-12 13:28:11 +02:00
|
|
|
|
2005-09-11 19:11:09 +02:00
|
|
|
username = form["username"]
|
|
|
|
#TODO:check if valid session id
|
|
|
|
sessionid = form["sessionid"]
|
2005-09-12 13:28:11 +02:00
|
|
|
if "gobansize" in form.keys(): #user already clicked on create
|
|
|
|
process_creation_form(req,form)
|
2005-09-11 19:11:09 +02:00
|
|
|
else:
|
2005-09-12 13:28:11 +02:00
|
|
|
display_game_creation_form(req,sessionid,username)
|
2005-09-14 16:29:58 +02:00
|
|
|
|
2005-09-12 15:51:18 +02:00
|
|
|
|