webgo/gamecreation.py

124 lines
3.7 KiB
Python
Raw Normal View History

from mod_python import *
import helper,login,database
2005-10-10 00:11:29 +02:00
def display_game_creation_form(req,form):
"""
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.
"""
2005-10-10 00:11:29 +02:00
username = form["username"]
sessionid = form["sessionid"]
data = helper.header()
p1data = create_user_dropdown_list("player1",username)
p2data = create_user_dropdown_list("player2")
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>
<p>Optional name for game: <input type="text" name="description" size="20" class="text"></p>
<input type="hidden" name="sessionid" value="%s">
<input type="hidden" name="username" value="%s">
<input type="submit" class="submit" name="create" value="create game">
</form>
""" % (p1data,p2data,gobansize,sessionid,username)
2005-10-10 00:11:29 +02:00
data+=helper.footer(req,form)
req.write(data)
def process_creation_form(req,form):
"""
validates and processes the game creation form.
If everything was ok, a game will be created.
TODO: change this function, so that the original database state canbe
restored, if for example one of the users does not have a free game slot.
"""
player1 = form["player1"]
player2 = form["player2"]
username = form["username"]
try:
description = form["description"]
except:
description = ""
helper.debug(req,form,"game creation: will use '%s' as description." % description)
#check if at least one of the players is the current user
if (player1 == username) or (player2 == username):
#create game
mygame = database.GobanTable(player1=player1,player2=player2,size=int(form["gobansize"]),description = description)
gamename = mygame.name
#update entries for player one and player two
for player in [player1,player2]:
myplayer = database.Users.byUsername(player)
if myplayer.free_slot_left:
myplayer.add_game(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)
#TODO:game created, now display game overview form
login.game_overview_form(req,form)
else:
#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)
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 = database.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.
"""
req.write(str(form.keys())+"<hr>")
username = form["username"]
#TODO:check if valid session id
sessionid = form["sessionid"]
if "gobansize" in form.keys(): #user already clicked on create
process_creation_form(req,form)
else:
2005-10-10 00:11:29 +02:00
display_game_creation_form(req,form,)