From 7b3639852148d86deb7d8cbd8ea6545fc2060eb4 Mon Sep 17 00:00:00 2001 From: phear Date: Sun, 11 Sep 2005 17:11:09 +0000 Subject: [PATCH] some more code for the creation of new games through the user --- documentation/development/README_DEVEL | 3 + gamecreation.py | 90 ++++++++++++++++++++++++++ helper.py | 2 + login.py | 13 +--- main.py | 2 +- psql.py | 17 +++++ 6 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 gamecreation.py diff --git a/documentation/development/README_DEVEL b/documentation/development/README_DEVEL index 91d90c2..60a57d7 100644 --- a/documentation/development/README_DEVEL +++ b/documentation/development/README_DEVEL @@ -49,3 +49,6 @@ How to extract the data from this POST with the cgi module: print string.split(form.keys()[0],".x")[0] ------------------------------------------ +known bugs: +"DatabaseError: error 'ERROR: current transaction is aborted, commands ignored until end of transaction block" +seems to be a problem with mod_python and a cached database connection. /etc/init.d/apache2 restart helps. \ No newline at end of file diff --git a/gamecreation.py b/gamecreation.py new file mode 100644 index 0000000..d7bab5f --- /dev/null +++ b/gamecreation.py @@ -0,0 +1,90 @@ +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 += """ +
+

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): + """ + 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 ' % 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. + """ + 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)) \ No newline at end of file diff --git a/helper.py b/helper.py index 3b3ce6a..d8b92ef 100644 --- a/helper.py +++ b/helper.py @@ -1,3 +1,5 @@ +import string + def header(): """return html header""" data = """ diff --git a/login.py b/login.py index dee3a1a..913a45a 100755 --- a/login.py +++ b/login.py @@ -1,4 +1,4 @@ -import psql,init_webgo,helper +import psql,init_webgo,helper,gamecreation import time from mod_python import * @@ -19,8 +19,6 @@ def process_form(req,form): origpassword = psql.get_user_info(name,"password") #check if user exists (else we would get an error string) if origpassword != "no such user": #no error message, now check password - req.write("hier bin ich in der Schleife.
") - req.write(str(origpassword)+"
") if password == origpassword: #login accepted psql.set_user_sessionid(name,sessionid) @@ -107,15 +105,10 @@ def login_form(): data += helper.footer() + return data -def create_new_game(req,user): - """ - create a new game for user. - gets: request object, username. - """ - def main(req): @@ -125,6 +118,6 @@ def main(req): req.write(login_form()) elif ("create" in form.keys()) and ("username" in form.keys()): #user wants to create a new game - create_new_game(req,form["username"]) + gamecreation.main(req,form) else: process_form(req,form) diff --git a/main.py b/main.py index 5ca513b..76f80f1 100755 --- a/main.py +++ b/main.py @@ -18,7 +18,7 @@ def handler(req): req.content_type = "text/html" try: # use explicit exception handling #reinitialize database - init_webgo.main() + #init_webgo.main() login = apache.import_module("login") login.main(req) diff --git a/psql.py b/psql.py index 3de261b..f75c495 100755 --- a/psql.py +++ b/psql.py @@ -270,6 +270,23 @@ def fetchall_list_to_goban_dict(list): return ret +def get_users_with_free_game_slots(): + """ + gets nothing. + returns a list of all users who have at least one game slot free. + """ + cursor=db.cursor() + data = """select username from users where + (game1 IS NULL) OR (game2 IS NULL) OR (game3 IS NULL) OR + (game4 IS NULL) OR (game5 IS NULL) OR (game6 IS NULL) OR + (game7 IS NULL) OR (game8 IS NULL) OR (game9 IS NULL) OR + (game10 IS NULL)""" + cursor.execute(data) + # Commit the changes + db.commit() + tmplist = cursor.fetchall() + ret = [item[0] for item in tmplist] + return ret def test(): #create_table("test")