from mod_python import * import psql,helper,login 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("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 += """

Player One (Black): %s

Player Two (White): %s

Goban Size: %s fields

""" % (p1data,p2data,gobansize,sessionid,username) data+=helper.footer() 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. """ player1 = form["player1"] player2 = form["player2"] username = form["username"] #check if at least one of the players is the current user if (player1 == username) or (player2 == username): #create game gamename = psql.create_goban_table(player1,player2,int(form["gobansize"])) #update entries for player one and player two for player in [player1,player2]: gameslot = psql.get_free_game_slot(player) if gameslot != "": psql.set_game_slot(player,gameslot,gamename) #TODO:game created, now display game overview form game_overview_form(req,username,form["sessionid"]) 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) else: #give error message req.write("Sorry, you must be one of the players!
") #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 ' % listname for item in userlist: #check whether current item = item for preselection if item == selected: tmp = 'selected' else: tmp = '' data += '' % (tmp,item) data += '' 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 = """ """ % 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())+"
") 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: display_game_creation_form(req,sessionid,username)