game creation seems to work, but I don't know for sure before game selection for playing works ;>
This commit is contained in:
parent
92e41a5b92
commit
298faf0a92
6 changed files with 100 additions and 34 deletions
|
@ -1,5 +1,5 @@
|
||||||
from mod_python import *
|
from mod_python import *
|
||||||
import psql,helper
|
import psql,helper,login
|
||||||
|
|
||||||
def display_game_creation_form(req,sessionid,username):
|
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.
|
gets a mod_python request, prints to req.write.
|
||||||
"""
|
"""
|
||||||
data = helper.header()
|
data = helper.header()
|
||||||
p1data = create_user_dropdown_list("playerone",username)
|
p1data = create_user_dropdown_list("player1",username)
|
||||||
p2data = create_user_dropdown_list("playertwo")
|
p2data = create_user_dropdown_list("player2")
|
||||||
gobansize = create_goban_size_dropdown_list("gobansize")
|
gobansize = create_goban_size_dropdown_list("gobansize")
|
||||||
#start form
|
#start form
|
||||||
#choose player one (black)
|
#choose player one (black),choose player two (white),choose goban size
|
||||||
#choose player two (white)
|
|
||||||
#choose goban size
|
|
||||||
#'hidden' session id and username
|
#'hidden' session id and username
|
||||||
data += """
|
data += """
|
||||||
<form method="post">
|
<form method="post">
|
||||||
|
@ -37,14 +35,31 @@ def process_creation_form(req,form):
|
||||||
validates and processes the game creation form.
|
validates and processes the game creation form.
|
||||||
If everything was ok, a game will be created.
|
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
|
#check if at least one of the players is the current user
|
||||||
if (form["playerone"] == form["username"]) or (form["playertwo"] == form["username"]):
|
if (player1 == username) or (player2 == username):
|
||||||
#create game name
|
#create game
|
||||||
#create_game
|
gamename = psql.create_goban_table(player1,player2,int(form["gobansize"]))
|
||||||
#update entries for player one and player two
|
#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:
|
else:
|
||||||
|
#give error message
|
||||||
pass
|
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 = ""):
|
def create_user_dropdown_list(listname,selected = ""):
|
||||||
"""
|
"""
|
||||||
|
@ -91,5 +106,4 @@ def main(req,form):
|
||||||
process_creation_form(req,form)
|
process_creation_form(req,form)
|
||||||
else:
|
else:
|
||||||
display_game_creation_form(req,sessionid,username)
|
display_game_creation_form(req,sessionid,username)
|
||||||
form = util.FieldStorage(req)
|
|
||||||
req.write(str(form))
|
|
11
helper.py
11
helper.py
|
@ -22,8 +22,17 @@ def generate_session_id():
|
||||||
m = md5.new()
|
m = md5.new()
|
||||||
m.update(str(time.time()))
|
m.update(str(time.time()))
|
||||||
m.update(str(random.random()))
|
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):
|
def check_for_int(data):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -9,7 +9,7 @@ def clear():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def create():
|
def create():
|
||||||
psql.create_goban_table(9)
|
#psql.create_goban_table("gast","gast2",9)
|
||||||
psql.create_user_table()
|
psql.create_user_table()
|
||||||
psql.add_webgo_user("gast","gast")
|
psql.add_webgo_user("gast","gast")
|
||||||
psql.add_webgo_user("gast2","gast2")
|
psql.add_webgo_user("gast2","gast2")
|
||||||
|
|
13
login.py
13
login.py
|
@ -52,16 +52,15 @@ def game_overview_form(req,user,sessionid):
|
||||||
tmp = ""
|
tmp = ""
|
||||||
for item in gamelist:
|
for item in gamelist:
|
||||||
if (item != None) and (item != "None"):
|
if (item != None) and (item != "None"):
|
||||||
tmp += '<input type="radio"> name="game" value="%s"> %s\n' % (item,item)
|
tmp += '<input type="radio" name="game" value="%s"> %s<br>\n' % (item,item)
|
||||||
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
|
#later write partners name and whether its our turn or not
|
||||||
tmp += '<input type="hidden" name="username" value="%s">\n' % user
|
#data += "Your partner: %s."
|
||||||
#later write partners name and whether its our turn or not
|
counter -= 1
|
||||||
#data += "Your partner: %s."
|
|
||||||
tmp +="<br>"
|
|
||||||
counter -= 1
|
|
||||||
if tmp == "": #no current games
|
if tmp == "": #no current games
|
||||||
data += "You don't have any running games.\n"
|
data += "You don't have any running games.\n"
|
||||||
else:
|
else:
|
||||||
|
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
|
||||||
|
tmp += '<input type="hidden" name="username" value="%s">\n' % user
|
||||||
data += tmp
|
data += tmp
|
||||||
data += '<input type=submit name="play" value="Play selected game">\n<input type=submit name="delete" value="Delete selected game">\n</form>'
|
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.
|
#now comes the option for creating new games.
|
||||||
|
|
8
main.py
8
main.py
|
@ -18,20 +18,22 @@ def handler(req):
|
||||||
req.content_type = "text/html"#was:text/html
|
req.content_type = "text/html"#was:text/html
|
||||||
try: # use explicit exception handling
|
try: # use explicit exception handling
|
||||||
#reinitialize database
|
#reinitialize database
|
||||||
|
|
||||||
|
#init_webgo.clear()
|
||||||
|
#init_webgo.create()
|
||||||
#init_webgo.main()
|
#init_webgo.main()
|
||||||
|
|
||||||
|
|
||||||
#load form, then delegate request
|
#load form, then delegate request
|
||||||
form = util.FieldStorage(req)
|
form = util.FieldStorage(req)
|
||||||
if "create" in form.keys():
|
if "create" in form.keys():
|
||||||
gamecreation = apache.import_module("gamecreation")
|
gamecreation = apache.import_module("gamecreation")
|
||||||
gamecreation.main(req,form)
|
gamecreation.main(req,form)
|
||||||
return apache.OK
|
|
||||||
else:
|
else:
|
||||||
#call login.py
|
#call login.py
|
||||||
login = apache.import_module("login")
|
login = apache.import_module("login")
|
||||||
login.main(req,form)
|
login.main(req,form)
|
||||||
return apache.OK
|
return apache.OK
|
||||||
|
|
||||||
except:
|
except:
|
||||||
import time
|
import time
|
||||||
errtime = '----- '+ time.ctime(time.time()) +' -----\n'
|
errtime = '----- '+ time.ctime(time.time()) +' -----\n'
|
||||||
|
|
60
psql.py
60
psql.py
|
@ -63,9 +63,9 @@ def sql_one_liner(data):
|
||||||
cursor.close()
|
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.
|
creates postgresql table containing goban data.
|
||||||
returns: name of created table.
|
returns: name of created table.
|
||||||
|
|
||||||
|
@ -79,19 +79,25 @@ def create_goban_table(size):
|
||||||
turn_number
|
turn_number
|
||||||
size
|
size
|
||||||
name
|
name
|
||||||
|
player1
|
||||||
|
player2
|
||||||
|
|
||||||
and the meaning of these fields:
|
and the meaning of these fields:
|
||||||
(xn,yn) is a field of the goban,
|
(xn,yn) is a field of the goban,
|
||||||
(turn_number,x1) is the current turn,
|
(turn_number,x1) is the current turn,
|
||||||
(size,x1) is the length of a side of the goban,
|
(size,x1) is the length of a side of the goban,
|
||||||
(name,x1) is the name of this 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"
|
tablename = helper.generate_game_name()
|
||||||
data="line varchar(15)"
|
data="line text"
|
||||||
for i in range(1,size+1):
|
for i in range(1,size+1):
|
||||||
if data != "":
|
if data != "":
|
||||||
data += ", "
|
data += ", "
|
||||||
data += "x"+str(i) + ' varchar(15)'
|
data += "x"+str(i) + ' text'
|
||||||
create_table(tablename,data)
|
create_table(tablename,data)
|
||||||
#table created, now fill the table
|
#table created, now fill the table
|
||||||
for i in range(1,size+1):
|
for i in range(1,size+1):
|
||||||
|
@ -116,6 +122,16 @@ def create_goban_table(size):
|
||||||
tmplist.append("name")
|
tmplist.append("name")
|
||||||
tmplist.append(tablename)
|
tmplist.append(tablename)
|
||||||
insert_into_table(tablename,str(tuple(tmplist)))
|
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
|
return tablename
|
||||||
|
|
||||||
def create_user_table():
|
def create_user_table():
|
||||||
|
@ -130,9 +146,9 @@ def create_user_table():
|
||||||
timeout - when does session time out?
|
timeout - when does session time out?
|
||||||
"""
|
"""
|
||||||
data = "username varchar(15)"
|
data = "username varchar(15)"
|
||||||
data += ", password varchar(15)"
|
data += ", password text"
|
||||||
for i in range(1,11):
|
for i in range(1,11):
|
||||||
data += ", game"+str(i)+" varchar(15)"
|
data += ", game"+str(i)+" text"
|
||||||
data += ", sessionid text"
|
data += ", sessionid text"
|
||||||
data += ", timeout int"
|
data += ", timeout int"
|
||||||
create_table("users",data)
|
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.
|
gets: table name, column name, line name, new content for field.
|
||||||
executes an SQL UPDATE statement for line.
|
executes an SQL UPDATE statement for line.
|
||||||
"""
|
"""
|
||||||
#TODO:schreiben
|
|
||||||
executestring ="UPDATE %s SET %s = '%s' WHERE line = '%s'" %(table,column,data,line)
|
executestring ="UPDATE %s SET %s = '%s' WHERE line = '%s'" %(table,column,data,line)
|
||||||
sql_one_liner(executestring)
|
sql_one_liner(executestring)
|
||||||
|
|
||||||
|
@ -287,7 +302,34 @@ def get_users_with_free_game_slots():
|
||||||
tmplist = cursor.fetchall()
|
tmplist = cursor.fetchall()
|
||||||
ret = [item[0] for item in tmplist]
|
ret = [item[0] for item in tmplist]
|
||||||
return ret
|
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():
|
def test():
|
||||||
#create_table("test")
|
#create_table("test")
|
||||||
drop_table("test")
|
drop_table("test")
|
||||||
|
|
Loading…
Reference in a new issue