some more code for the creation of new games through the user

This commit is contained in:
phear 2005-09-11 17:11:09 +00:00
parent 81fea29c24
commit 7b36398521
6 changed files with 116 additions and 11 deletions

View file

@ -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.

90
gamecreation.py Normal file
View file

@ -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 += """
<form method="post">
<p>Player One (Black): %s </p>
<p>Player Two (White): %s </p>
<p>Goban Size: %s fields </p>
<input type="hidden" name="sessionid" value="%s"><p>
<input type="hidden" name="username" value="%s"><p>
<input type="submit" value="create game"></p>
</form>
""" % (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 <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.
"""
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))

View file

@ -1,3 +1,5 @@
import string
def header():
"""return html header"""
data = """

View file

@ -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.<br>")
req.write(str(origpassword)+"<br>")
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)

View file

@ -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)

17
psql.py
View file

@ -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")