#!/usr/bin/python DEBUG = 1 import sys,string import cgi import psql,helper,gnugo picklefile = "goban.pickledump" ############################################################################### def display_goban(goban,req,form): """ gets: dictionary containing the layout of the used goban. returns: string containing the HTML code for a clickable goban. """ 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)] data += '\n
\n
' return data 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 psql.update_goban_table_field(name,"x1","sgf",new_sgf) psql.update_turn_number(name,turn+1) psql.set_time(name) 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." def update_goban_dict_and_table(gobandict,gobanlite): """ gets a gobandict and a gobdict light (just coordinates and size). updates the fields in gobandict and database. returns changed gobandict and True (or False) if something has been changed (or not). """ tf = False for key in gobanlite.keys(): if gobandict[key] != gobanlite[key]: #found difference in dicts. #update gobandict gobandict[key] = gobanlite[key] #update databse table. the only valid difference can be changed goban field positions. psql.update_goban_field(gobandict["name"],key[0],key[1],gobandict[key]) tf = True return gobandict,tf ############################################################################### def main(req,form): """# Print the required header that tells the browser how to render the text. #(currently done by error logger) #print "Content-Type: text/plain\n\n" gamename = form["game"] #do stuff data = helper.header() #read goban table from database tmplist = psql.read_table(gamename) #make a dictionary out of the list goban = psql.fetchall_list_to_goban_dict(tmplist) #print goban (goban,retstring) = process_form(goban) data += "Turn number: %s, %s Player's Move.
" % (goban["turn_number"],("White","Black")[goban["turn_number"] % 2]) #eleet ;> data += display_goban(goban) if retstring != "": data +="\n"+retstring+"
\n" data += helper.footer(req,form) print data """