webgo/goban.py

60 lines
1.5 KiB
Python
Executable file

#!/usr/bin/python
DEBUG = 1
import sys,string
import helper,gnugo,database
from cherrypy import cpg
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."