basic gameplay works again. What does not yet work: Things like proper error management, display of current turn and player etc.

This commit is contained in:
phear 2005-11-24 19:21:37 +00:00
parent 6c3db3caf8
commit 611d67ddaf
4 changed files with 35 additions and 47 deletions

View file

@ -52,7 +52,7 @@ class GobanTable(SQLObject):
def set_time(self):
"""updates self.lastmove to current time"""
self.lastmove = int(time.time)
self.lastmove = int(time.time())

View file

@ -149,7 +149,7 @@ class GTP_connection:
return "ERROR: Unrecognized answer: " + result
def is_legal(gobandict,coords,req,form):
def is_legal(gobandict,coords):
"""
gets a goban dict and a (x,y) tuple.
tests wether proposed move is legal.
@ -191,7 +191,7 @@ def create_sgf_file(size, filename=""):
return ret
def make_move_in_sgf(req,form,gobandict,coords,filename = ""):
def make_move_in_sgf(gobandict,coords,filename = ""):
"""
gets: goban dict, (x,y) tuple for move, optionally a filename for tempfile.
writes the string to a file, opens gnugo, makes the move, writes new sgf
@ -210,7 +210,6 @@ def make_move_in_sgf(req,form,gobandict,coords,filename = ""):
color = ["white","black"][turn % 2]
"""
color = gobandict["play"]
helper.debug(req,form,"color: %s -- turn: %s " % (color,turn))
#generate tmpfile
if filename == "":
filename = filehandling.gen_temp_file()

View file

@ -18,43 +18,5 @@ from cherrypy.lib import httptools
def process_form(req,form,gobandict):
"""
gets a goban dictionary.
reads out the returned CGI form.
if the goban has been clicked, return a (x,y) tuple of the position.
"""
ret = ""
#if form == empty (which means if page is displayed for the first time):
if form.keys() != []:
#cut out the name of the clicked button
for item in form.keys():
if string.find(item,").x")>0:
coordstring = string.split(item,".x")[0]
position = helper.string_to_tuple(coordstring)
ret = set_stone(gobandict, position,req,form)
return ret
def set_stone(gobandict, position,req,form):
"""gets a goban dictionary and a (x,y)-tuple. Returns a modified goban."""
size = gobandict["size"]
name = gobandict["name"]
turn = gobandict["turn_number"]
if (gobandict[position] == 0): #empty field
if gnugo.is_legal(gobandict,position,req,form): #gnugo says the move is ok
#let gnugo make the above move, let gnugo write move to file
new_sgf = gnugo.make_move_in_sgf(req,form,gobandict,position)
#write new sgf file into database
mygame = database.GobanTable.byName(name)
mygame.sgf = new_sgf
mygame.turn_number = turn + 1
mygame.set_time()
return ""
else: #move not ok
return "This is not a legal move (says Gnugo)."
else: #position not empty
return "Could not make move: Field not empty."

View file

@ -98,10 +98,11 @@ class PlayGame:
username = cpg.request.sessionMap["username"]
myuser = database.Users.byUsername(username)
sessionid = cpg.request.sessionMap["_sessionId"]
print game
if myuser.sessionid == sessionid:
if coord != None:
return coord
ret = self.process_form(game,coord)
if ret == "":
return self.display_goban(game)
else:
return self.display_goban(game)
@ -183,6 +184,32 @@ class PlayGame:
data += helper.footer()
return data
def process_form(self,gamename,coord):
"""
gets name of a go game, coordinates of user's move.
processes move.
"""
x,y = string.split(coord,",")
position = (int(x),int(y))
mygame = database.GobanTable.get(gamename)
size = mygame.size
turn = mygame.turn_number
sgf = mygame.sgf
gobandict = gnugo.parse_static_gnugo_sgf(sgf)
gobandict["name"] = gamename
gobandict["turn_number"] = turn
gobandict["sgf"] = sgf
if (gobandict[position] == 0): #empty field
if gnugo.is_legal(gobandict,position): #gnugo says the move is ok
#let gnugo make the above move, let gnugo write move to file
new_sgf = gnugo.make_move_in_sgf(gobandict,position)
#write new sgf file into database
mygame.sgf = new_sgf
mygame.turn_number = turn + 1
mygame.set_time()
return ""
else: #move not ok
return "This is not a legal move (says Gnugo)."
else: #position not empty
return "Could not make move: Field not empty."
index.exposed = True