174 lines
No EOL
5.3 KiB
Python
Executable file
174 lines
No EOL
5.3 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
"""
|
|
the main file for WebGo.
|
|
"""
|
|
import sys,string
|
|
import cgi
|
|
import pickle
|
|
import psql,helper
|
|
|
|
picklefile = "goban.pickledump"
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
def display_goban(goban):
|
|
"""
|
|
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 += '<form method="post">'
|
|
try:
|
|
size = goban["size"]
|
|
except:
|
|
#TODO: proper error management
|
|
raise "[EE] display_goban: got broken goban dictionary."
|
|
sys.exit(1)
|
|
for x in range(1,size+1):
|
|
for y in range(1,size+1):
|
|
# check for white or black stone
|
|
if goban[(x,y)] == 1:
|
|
stone = "_white"
|
|
elif goban[(x,y)] == 2:
|
|
stone = "_black"
|
|
else:
|
|
stone = ""
|
|
sx = str(x)
|
|
sy = str(y)
|
|
# check position:
|
|
if (x == 1) and (y == 1): # upper left
|
|
data += '<input type=image src="img/topleftline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
elif (x == 1) and (y == size): # upper right
|
|
data += '<input type=image src="img/toprightline'+stone+'.png" name="('+sx+','+sy+')"><br>'
|
|
elif (x == size) and (y == size): # lower right
|
|
data += '<input type=image src="img/bottomrightline'+stone+'.png" name="('+sx+','+sy+')"><br>'
|
|
elif (x == size) and (y == 1): # lower left
|
|
data += '<input type=image src="img/bottomleftline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
elif (y == 1): #left line
|
|
data += '<input type=image src="img/leftline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
elif (x == 1): # top line
|
|
data += '<input type=image src="img/topline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
elif (y == size): # right line
|
|
data += '<input type=image src="img/rightline'+stone+'.png" name="('+sx+','+sy+')"><br>'
|
|
elif (x == size): #bottom line
|
|
data += '<input type=image src="img/bottomline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
else: # hoshi or empty inner field
|
|
defaultfield = '<input type=image src="img/centerline'+stone+'.png" name="('+sx+','+sy+')">'
|
|
#too lazy to make special images for hoshi fields with stones:
|
|
if goban[(x,y)] == 1:
|
|
hoshifield = '<input type=image src="img/centerline_white.png" name="('+sx+','+sy+')">'
|
|
elif goban[(x,y)] == 2:
|
|
hoshifield = '<input type=image src="img/centerline_black.png" name="('+sx+','+sy+')">'
|
|
else: #empty hoshi
|
|
hoshifield = '<input type=image src="img/hoshi.png" name="('+sx+','+sy+')">'
|
|
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 += '</form>'
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def string_to_tuple(str):
|
|
"""
|
|
gets a string. If the string contains '(',')' and ',', then return
|
|
a tuple processed from the string. If the partial string is empty, then
|
|
-1 will be returned for that value.
|
|
"""
|
|
if (str[0] =='(') and (str[-1] ==')') and (string.find(str,',')):
|
|
splitlist = string.split(str[1:-1],",")
|
|
returnlist = []
|
|
for item in splitlist:
|
|
try:
|
|
returnlist.append(int(item))
|
|
except: #empty string
|
|
returnlist.append(-1)
|
|
return tuple(returnlist)
|
|
|
|
|
|
def process_form(goban):
|
|
"""
|
|
gets a goban dictionary.
|
|
|
|
reads out the returned CGI form.
|
|
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.keys() != []:
|
|
#cut out the name of the clicked button
|
|
namestring = string.split(form.keys()[0],".x")[0]
|
|
position = string_to_tuple(namestring)
|
|
ret = set_stone(goban, position)
|
|
if (type(ret) == type("")):
|
|
return (goban,ret) #return old goban and error string
|
|
else:
|
|
goban = ret
|
|
return (goban,"") # return new goban and empty string
|
|
|
|
|
|
def set_stone(goban, position):
|
|
"""gets a goban dictionary and a (x,y)-tuple. Returns a modified goban."""
|
|
turn = goban["turn_number"]
|
|
if (goban[position] == 0): #empty field
|
|
goban[position] = (turn % 2) + 1 #even turn: white tone (1), else black stone(2)
|
|
goban["turn_number"] += 1
|
|
#now write changed values to database
|
|
psql.update_goban_field(goban["name"],position[0],position[1],(turn % 2) + 1)
|
|
psql.update_turn_number(goban["name"],goban["turn_number"])
|
|
return goban
|
|
else:
|
|
return "Could not make move: Field not empty."
|
|
|
|
###############################################################################
|
|
|
|
def main():
|
|
# 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"
|
|
|
|
#do stuff
|
|
data = helper.header()
|
|
|
|
#read goban table from database
|
|
tmplist = psql.read_table("test")
|
|
#make a dictionary out of the list
|
|
goban = psql.fetchall_list_to_goban_dict(tmplist)
|
|
#print goban
|
|
|
|
(goban,str) = process_form(goban)
|
|
data += "<p> Turn number: %s, %s Player's Move.</p>" % (goban["turn_number"],("White","Black")[goban["turn_number"] % 2]) #eleet ;>
|
|
data += display_goban(goban)
|
|
if str != "":
|
|
data +="\n<p>"+str+"</p>\n"
|
|
|
|
|
|
data += helper.footer()
|
|
print data
|
|
|
|
|