"basic multiuser gameplay possible. Milestone m6 reached!

This commit is contained in:
phear 2005-09-14 16:48:07 +00:00
parent e7e9c72e54
commit 8f5568bcd0
4 changed files with 72 additions and 24 deletions

View file

@ -29,8 +29,6 @@ m5: goban inside database
on side of the board, so that gobans of size 9x9, 13x13 and 19x19 can be on side of the board, so that gobans of size 9x9, 13x13 and 19x19 can be
displayed. displayed.
. <--- YOU ARE HERE.
m6: manage users and game sessions inside database m6: manage users and game sessions inside database
manage user accounts and the game(s) they currently play inside the data- manage user accounts and the game(s) they currently play inside the data-
base. each game has its own table, which contains players, current turn, base. each game has its own table, which contains players, current turn,
@ -39,7 +37,9 @@ m6: manage users and game sessions inside database
(up to 8 or so). So if the user calls the cgi script, she has to send (up to 8 or so). So if the user calls the cgi script, she has to send
username and password and is then given the choice of game to play. If username and password and is then given the choice of game to play. If
there is only one active game, directly present the goban. there is only one active game, directly present the goban.
. <--- YOU ARE HERE.
m7: implement Go rules m7: implement Go rules
Do not accept player moves which violate go rules. Remove beaten stones. Do not accept player moves which violate go rules. Remove beaten stones.
Count and display the score for each player. Count and display the score for each player.

View file

@ -88,6 +88,7 @@ def display_goban(goban,req,form):
data += hoshifield data += hoshifield
else: else:
data += defaultfield data += defaultfield
data += '\n<input type="hidden" name="play" value="foo">'
data += '\n</form>' data += '\n</form>'
return data return data
@ -98,7 +99,7 @@ def display_goban(goban,req,form):
def process_form(goban): def process_form(req,form,goban):
""" """
gets a goban dictionary. gets a goban dictionary.
@ -106,12 +107,13 @@ def process_form(goban):
if the goban has been clicked, return a (x,y) tuple of the position. if the goban has been clicked, return a (x,y) tuple of the position.
""" """
#get instance of fieldstorage
form = cgi.FieldStorage()
#if form == empty (which means if page is displayed for the first time): #if form == empty (which means if page is displayed for the first time):
if form.keys() != []: if form.keys() != []:
#cut out the name of the clicked button #cut out the name of the clicked button
namestring = string.split(form.keys()[0],".x")[0] for item in form.keys():
if string.find(item,").x")>0:
namestring = string.split(item,".x")[0]
position = helper.string_to_tuple(namestring) position = helper.string_to_tuple(namestring)
ret = set_stone(goban, position) ret = set_stone(goban, position)
if (type(ret) == type("")): if (type(ret) == type("")):
@ -125,7 +127,7 @@ def set_stone(goban, position):
"""gets a goban dictionary and a (x,y)-tuple. Returns a modified goban.""" """gets a goban dictionary and a (x,y)-tuple. Returns a modified goban."""
turn = goban["turn_number"] turn = goban["turn_number"]
if (goban[position] == 0): #empty field if (goban[position] == 0): #empty field
goban[position] = (turn % 2) + 1 #even turn: white tone (1), else black stone(2) goban[position] = (turn % 2) + 1 #even turn: white stone (1), else black stone(2)
goban["turn_number"] += 1 goban["turn_number"] += 1
#now write changed values to database #now write changed values to database
psql.update_goban_field(goban["name"],position[0],position[1],(turn % 2) + 1) psql.update_goban_field(goban["name"],position[0],position[1],(turn % 2) + 1)
@ -150,11 +152,11 @@ def main(gamename):
goban = psql.fetchall_list_to_goban_dict(tmplist) goban = psql.fetchall_list_to_goban_dict(tmplist)
#print goban #print goban
(goban,str) = process_form(goban) (goban,retstring) = process_form(goban)
data += "<p> Turn number: %s, %s Player's Move.</p>" % (goban["turn_number"],("White","Black")[goban["turn_number"] % 2]) #eleet ;> data += "<p> Turn number: %s, %s Player's Move.</p>" % (goban["turn_number"],("White","Black")[goban["turn_number"] % 2]) #eleet ;>
data += display_goban(goban) data += display_goban(goban)
if str != "": if retstring != "":
data +="\n<p>"+str+"</p>\n" data +="\n<p>"+retstring+"</p>\n"
data += helper.footer() data += helper.footer()

View file

@ -30,11 +30,6 @@ def handler(req):
sessionid="" sessionid=""
username="" username=""
foundx = False
for item in form.keys():
if string.find(item,").x") > 0:
foundx = True
#if sessionid=sesssionid and time < timeout: #if sessionid=sesssionid and time < timeout:
# set_timeout # set_timeout
@ -46,7 +41,7 @@ def handler(req):
elif "delete" in form.keys(): elif "delete" in form.keys():
deletegame = apache.import_module("deletegame") deletegame = apache.import_module("deletegame")
deletegame.main(req,form) deletegame.main(req,form)
elif ("play" in form.keys()) or foundx: elif ("play" in form.keys()):
playgame = apache.import_module("playgame") playgame = apache.import_module("playgame")
playgame.main(req,form) playgame.main(req,form)
else: else:

View file

@ -1,4 +1,39 @@
import goban,helper,psql import goban,helper,psql
import string
def is_my_turn(req,form,gobandict):
"""
gets request and util.FiedStorage form.
check wether or not the current this is the players turn.
return true or false.
"""
#INFO: player1 is black,player2 is white. black starts the game.
me = form["username"]
player1 = gobandict["player1"]
player2 = gobandict["player2"]
#get turn_number.
turn_number = gobandict["turn_number"]
#if turn_number modulo 2 == 0: are we player two?
# yes:return True, else return False
if turn_number % 2 == 0:
if me == player2:
return True
else:
return False
#else:are we player1?
# yes:return True, else return False
else:
if me == player1:
return True
else:
return False
def main(req,form): def main(req,form):
""" """
@ -11,25 +46,41 @@ def main(req,form):
except: except:
gamename = "" gamename = ""
if gamename != "": if gamename != "":
#do stuff
data = helper.header()
#read goban table from database #read goban table from database
tmplist = psql.read_table(gamename) tmplist = psql.read_table(gamename)
#make a dictionary out of the list #make a dictionary out of the list
gobandict = psql.fetchall_list_to_goban_dict(tmplist) gobandict = psql.fetchall_list_to_goban_dict(tmplist)
#print goban #check if user has already clicked onto a field:
#data += "<p> Turn number: %s, %s Player's Move.</p>" % (gobandict["turn_number"],("White","Black")[gobandict["turn_number"] % 2]) #eleet ;> foundx = False
for item in form.keys():
if string.find(item,").x") > 0:
foundx = True
if foundx:
if is_my_turn(req,form,gobandict):
(gobandict,retstring) = goban.process_form(req,form,gobandict)
else:
pass
#do stuff
data = helper.header()
data += "Turn number: "+str(gobandict["turn_number"])+". "
#check whether its our turn #check whether its our turn
#if yes: print 'your move' and display goban and process move #if yes: print 'your move' and display goban and process move
#if not: print '...s move' and display goban. #if not: print '...s move' and display goban.
if is_my_turn(req,form,gobandict):
data += ("Its your turn.<br>")
else:
data+= ("This is not your turn. Look, but don't touch ;)<br>")
#print goban
data += goban.display_goban(gobandict,req,form) data += goban.display_goban(gobandict,req,form)
data += helper.footer() data += helper.footer()
req.write(data) req.write(data)
else: else:
req.write('Error: You have to select a game to play it!') req.write('Error: You have to select a game to play it!')