game creation seems to work, but I don't know for sure before game selection for playing works ;>

This commit is contained in:
phear 2005-09-12 13:51:18 +00:00
parent 92e41a5b92
commit 298faf0a92
6 changed files with 100 additions and 34 deletions

View file

@ -1,5 +1,5 @@
from mod_python import *
import psql,helper
import psql,helper,login
def display_game_creation_form(req,sessionid,username):
"""
@ -8,13 +8,11 @@ def display_game_creation_form(req,sessionid,username):
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")
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
#choose player one (black),choose player two (white),choose goban size
#'hidden' session id and username
data += """
<form method="post">
@ -37,14 +35,31 @@ 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 (form["playerone"] == form["username"]) or (form["playertwo"] == form["username"]):
#create game name
#create_game
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:
pass
#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 = ""):
"""
@ -91,5 +106,4 @@ def main(req,form):
process_creation_form(req,form)
else:
display_game_creation_form(req,sessionid,username)
form = util.FieldStorage(req)
req.write(str(form))

View file

@ -22,8 +22,17 @@ def generate_session_id():
m = md5.new()
m.update(str(time.time()))
m.update(str(random.random()))
return string.replace(base64.encodestring(m.digest())[:-3], '/', '$')
s = string.replace(base64.encodestring(m.digest())[:-3], '/', '$')
return s
def generate_game_name():
from whrandom import choice
chars = string.letters
name = ''
for i in range(16):
name = name + choice(chars)
return name.lower()
def check_for_int(data):
"""

View file

@ -9,7 +9,7 @@ def clear():
pass
def create():
psql.create_goban_table(9)
#psql.create_goban_table("gast","gast2",9)
psql.create_user_table()
psql.add_webgo_user("gast","gast")
psql.add_webgo_user("gast2","gast2")

View file

@ -52,16 +52,15 @@ def game_overview_form(req,user,sessionid):
tmp = ""
for item in gamelist:
if (item != None) and (item != "None"):
tmp += '<input type="radio"> name="game" value="%s"> %s\n' % (item,item)
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
tmp += '<input type="hidden" name="username" value="%s">\n' % user
#later write partners name and whether its our turn or not
#data += "Your partner: %s."
tmp +="<br>"
counter -= 1
tmp += '<input type="radio" name="game" value="%s"> %s<br>\n' % (item,item)
#later write partners name and whether its our turn or not
#data += "Your partner: %s."
counter -= 1
if tmp == "": #no current games
data += "You don't have any running games.\n"
else:
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
tmp += '<input type="hidden" name="username" value="%s">\n' % user
data += tmp
data += '<input type=submit name="play" value="Play selected game">\n<input type=submit name="delete" value="Delete selected game">\n</form>'
#now comes the option for creating new games.

View file

@ -18,20 +18,22 @@ def handler(req):
req.content_type = "text/html"#was:text/html
try: # use explicit exception handling
#reinitialize database
#init_webgo.clear()
#init_webgo.create()
#init_webgo.main()
#load form, then delegate request
form = util.FieldStorage(req)
if "create" in form.keys():
gamecreation = apache.import_module("gamecreation")
gamecreation.main(req,form)
return apache.OK
else:
#call login.py
login = apache.import_module("login")
login.main(req,form)
return apache.OK
return apache.OK
except:
import time
errtime = '----- '+ time.ctime(time.time()) +' -----\n'

60
psql.py
View file

@ -63,9 +63,9 @@ def sql_one_liner(data):
cursor.close()
def create_goban_table(size):
def create_goban_table(player1,player2,size):
"""
gets:size of goban.
gets:player1, player2, size of goban.
creates postgresql table containing goban data.
returns: name of created table.
@ -79,19 +79,25 @@ def create_goban_table(size):
turn_number
size
name
player1
player2
and the meaning of these fields:
(xn,yn) is a field of the goban,
(turn_number,x1) is the current turn,
(size,x1) is the length of a side of the goban,
(name,x1) is the name of this goban.
(player1,x1) is the name of one player.
(player2,x1) is the name of the other player.
"""
tablename="test"
data="line varchar(15)"
tablename = helper.generate_game_name()
data="line text"
for i in range(1,size+1):
if data != "":
data += ", "
data += "x"+str(i) + ' varchar(15)'
data += "x"+str(i) + ' text'
create_table(tablename,data)
#table created, now fill the table
for i in range(1,size+1):
@ -116,6 +122,16 @@ def create_goban_table(size):
tmplist.append("name")
tmplist.append(tablename)
insert_into_table(tablename,str(tuple(tmplist)))
#name of player1
tmplist=[]
tmplist.append("player1")
tmplist.append(player1)
insert_into_table(tablename,str(tuple(tmplist)))
#name of player2
tmplist=[]
tmplist.append("player2")
tmplist.append(player2)
insert_into_table(tablename,str(tuple(tmplist)))
return tablename
def create_user_table():
@ -130,9 +146,9 @@ def create_user_table():
timeout - when does session time out?
"""
data = "username varchar(15)"
data += ", password varchar(15)"
data += ", password text"
for i in range(1,11):
data += ", game"+str(i)+" varchar(15)"
data += ", game"+str(i)+" text"
data += ", sessionid text"
data += ", timeout int"
create_table("users",data)
@ -215,7 +231,6 @@ def update_database_field(table,column,line,data):
gets: table name, column name, line name, new content for field.
executes an SQL UPDATE statement for line.
"""
#TODO:schreiben
executestring ="UPDATE %s SET %s = '%s' WHERE line = '%s'" %(table,column,data,line)
sql_one_liner(executestring)
@ -287,7 +302,34 @@ def get_users_with_free_game_slots():
tmplist = cursor.fetchall()
ret = [item[0] for item in tmplist]
return ret
def get_free_game_slot(username):
"""
gets a username
returns the name of a free game slot or "" if none found.
"""
ret = ""
for i in range(1,11):
cursor=db.cursor()
data = "SELECT username FROM users WHERE username='%s' AND game%s IS NULL" % (username,i)
cursor.execute(data)
# Commit the changes
db.commit()
tmp = [item[0] for item in cursor.fetchall()]
if tmp != []:
ret = "game"+str(i)
return ret
def set_game_slot(username,gameslot,gamename):
"""
gets username,game slot,game name.
sets the game slot for user username to game name ;>
"""
executestring ="UPDATE users SET %s = '%s' WHERE username = '%s'" %(gameslot,gamename, username)
sql_one_liner(executestring)
def test():
#create_table("test")
drop_table("test")