import helper,database,login,gnugo import string from cherrypy import cpg from cherrypy.lib import httptools DEBUG = 1 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. """ me = form["username"] player1 = gobandict["player1"] player2 = gobandict["player2"] play = gobandict["play"] if ((player1 == me) and (play == "white")) or ((player2 == me) and (play == "black")): return True else: return False def create_gobandict(req,form,gamename): """ gets a gamename loads sgf, transforms it to dict, writes stuff like player names to dict returns dict """ #read goban sgf from database mygame = database.GobanTable.byName(gamename) sgf = mygame.sgf gobandict = gnugo.parse_static_gnugo_sgf(sgf) gobandict["player1"] = mygame.player1 gobandict["player2"] = mygame.player2 gobandict["turn_number"] = mygame.turn_number gobandict["name"] = mygame.name gobandict["sgf"] = sgf return gobandict def main(req,form): """ display selected goban and manage user input. """ try: gamename = form["game"] except: gamename = "" if gamename != "": data = "" req.write(helper.header()) #helper.debug(req,form,str(form.keys())) gobandict = create_gobandict(req,form,gamename) #check if user has already clicked onto a field: click_on_field = False for item in form.keys(): if string.find(item,").x") > 0: click_on_field = True if click_on_field: #if yes: is this the user's turn? then process form. if is_my_turn(req,form,gobandict): helper.debug(req,form,"its my turn , i am going to process the form: --") retstring = goban.process_form(req,form,gobandict) if retstring != "": helper.debug(req,form,"playgame.main says: "+str(retstring)) else: #reload gobandict, it has been changed. gobandict = create_gobandict(req,form,gamename) else: helper.debug(req,form,"it's not my turn.") data += """
Turn number: %s. %s plays.\n """ % (str(gobandict["turn_number"]), (string.capitalize(gobandict["play"]))) #check whether its our turn #if yes: print 'your move' and display goban and process move #if not: print '...s move' and display goban. if is_my_turn(req,form,gobandict): data += "
It's your turn.
\n" else: data += "
This is not your turn. You have to wait for the move of the other player.
\n" #print goban data += goban.display_goban(gobandict,req,form) data += login.navigation_bar(req,form) data += helper.footer(req,form,1) req.write(data) else: req.write('Error: You have to select a game to play it!') class PlayGame: """ displays a game and processes user input. """ def index(self,game,coord=None): username = cpg.request.sessionMap["username"] myuser = database.Users.byUsername(username) sessionid = cpg.request.sessionMap["_sessionId"] if myuser.sessionid == sessionid: if coord != None: ret = self.process_form(game,coord) if ret == "": return self.display_goban(game) else: return self.display_goban(game) else: httptools.redirect("/login") def display_goban(self,gamename): """ gets: dictionary containing the layout of the used goban. returns: string containing the HTML code for a clickable goban. """ data = helper.header() data += '
' hoshis19x19 = [(4,4),(4,10),(4,16),(10,4),(10,10),(10,16),(16,4),(16,10),(16,16)] hoshis13x13 = [(4,4),(4,10),(7,7),(10,4),(10,10)] hoshis9x9 = [(3,3),(3,7),(5,5),(7,3),(7,7)] #TODO: check form[game] before the following asignment mygame = database.GobanTable.get(gamename) sgf = mygame.sgf gobandict = gnugo.parse_static_gnugo_sgf(sgf) size = mygame.size for x in range(1,size+1): for y in range(1,size+1): # check for white or black stone if gobandict[(x,y)] == 1: stone = "_white" elif gobandict[(x,y)] == 2: stone = "_black" else: stone = "" sx = str(x) sy = str(y) # check position: if (x == 1) and (y == 1): # upper left #data += '' data += '' % (sx,sy,gamename,stone) elif (x == 1) and (y == size): # upper right data += '
' % (sx,sy,gamename,stone) elif (x == size) and (y == size): # lower right data += '
' % (sx,sy,gamename,stone) elif (x == size) and (y == 1): # lower left data += '' % (sx,sy,gamename,stone) elif (y == 1): #left line data += '' % (sx,sy,gamename,stone) elif (x == 1): # top line data += '' % (sx,sy,gamename,stone) elif (y == size): # right line data += '
' % (sx,sy,gamename,stone) elif (x == size): #bottom line data += '' % (sx,sy,gamename,stone) else: # hoshi or empty inner field defaultfield = '' % (sx,sy,gamename,stone) #too lazy to make special images for hoshi fields with stones: if gobandict[(x,y)] == 1: hoshifield = '' % (sx,sy,gamename) elif gobandict[(x,y)] == 2: hoshifield = '' % (sx,sy,gamename) else: #empty hoshi hoshifield = '' % (sx,sy,gamename) if size == 19: # 9 hoshis if (x,y) in hoshis19x19: data += hoshifield else: data += defaultfield elif size == 13: if (x,y) in hoshis13x13: data += hoshifield else: data += defaultfield elif size == 9: if (x,y) in hoshis9x9: data += hoshifield else: data += defaultfield data += '
' 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