import helper,login,database from cherrypy import cpg from cherrypy.lib import httptools 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 class GameCreation: """ manages the creation of games. """ def index(self,player1 = None, player2 = None, description = None,gobansize = None): try: username = cpg.request.sessionMap["username"] myuser = database.Users.byUsername(username) except: return helper.cs_render("templates/not_logged_in.cs") username = "" sessionid = cpg.request.sessionMap["_sessionId"] if myuser.sessionid == sessionid and username != "": if player1 != None and player2 != None and gobansize != None: return self.process_creation_form(player1,player2,gobansize,description) else: return self.display_game_creation_form() else: return helper.cs_render("templates/not_logged_in.cs") def display_game_creation_form(self, message = ""): """ prints a html form with multiple drop-down lists for choosing players, goban size and so on. TODO: authentication? """ username = cpg.request.sessionMap["username"] myuser = database.Users.byUsername(username) sessionid = cpg.request.sessionMap["_sessionId"] #p1data = create_user_dropdown_list("player1",username) #p2data = create_user_dropdown_list("player2") settings = {} settings["Data.Message"] = message userlist = database.get_users_with_free_game_slots() i = 0 for item in userlist: settings["Data.Userlist.%d" % i] = item i += 1 settings["Data.Username"] = username return helper.cs_render("templates/gamecreation.cs",settings) def process_creation_form(self,player1,player2,gobansize,description=None): """ 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. """ username = cpg.request.sessionMap["username"] myuser = database.Users.byUsername(username) sessionid = cpg.request.sessionMap["_sessionId"] #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=gobansize,description = description) gamename = mygame.id #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. #display form with error msg return self.display_game_creation_form("Error: No free game slots for player %s!
" % player) httptools.redirect("/overview") else:#display form with error msg return self.display_game_creation_form("Sorry, but you must be one of the players!
") index.exposed = True