2005-10-09 15:58:06 +02:00
|
|
|
import string,re
|
2005-09-11 19:11:09 +02:00
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
DEBUG = 1
|
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
def header():
|
|
|
|
"""return html header"""
|
|
|
|
data = """
|
2005-09-23 16:37:07 +02:00
|
|
|
<html>
|
2005-09-20 09:05:05 +02:00
|
|
|
<head>
|
|
|
|
<title>WebGo</title>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<meta http-equiv="pragma" content="no-cache" />
|
|
|
|
<meta http-equiv="cache-control" content="no-cache" />
|
|
|
|
<meta http-equiv="expires" content="0" />
|
|
|
|
<link rel="stylesheet" media="screen" href="default.css" type="text/css" />
|
|
|
|
</head>
|
|
|
|
<h1> WebGo </h1>
|
2005-09-23 16:37:07 +02:00
|
|
|
<body>
|
2005-09-06 19:52:46 +02:00
|
|
|
"""
|
|
|
|
return data
|
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
def debug(req,form, optstr = ""):
|
|
|
|
"""
|
|
|
|
print various debug infos, e.g. form content.
|
|
|
|
gets request, util.FieldStorage form, optional extra string.
|
|
|
|
returns nothing, writes with request.write.
|
|
|
|
"""
|
|
|
|
if DEBUG:
|
|
|
|
if optstr == "":
|
2005-09-23 16:37:07 +02:00
|
|
|
req.write("Debug: "+str(form.keys())+"<br>\n")
|
2005-09-20 09:05:05 +02:00
|
|
|
else:
|
2005-09-23 16:37:07 +02:00
|
|
|
req.write("Debug: "+optstr+"<br>\n")
|
2005-09-20 09:05:05 +02:00
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
def footer():
|
|
|
|
"""return html footer"""
|
|
|
|
data = """
|
|
|
|
</body></html>
|
|
|
|
"""
|
|
|
|
return data
|
|
|
|
|
|
|
|
# create a unique session id
|
|
|
|
def generate_session_id():
|
|
|
|
import md5, time, base64, random, string
|
|
|
|
m = md5.new()
|
|
|
|
m.update(str(time.time()))
|
|
|
|
m.update(str(random.random()))
|
2005-09-12 15:51:18 +02:00
|
|
|
s = string.replace(base64.encodestring(m.digest())[:-3], '/', '$')
|
|
|
|
return s
|
2005-09-06 19:52:46 +02:00
|
|
|
|
2005-09-12 15:51:18 +02:00
|
|
|
def generate_game_name():
|
|
|
|
from whrandom import choice
|
|
|
|
chars = string.letters
|
|
|
|
name = ''
|
|
|
|
for i in range(16):
|
|
|
|
name = name + choice(chars)
|
|
|
|
return name.lower()
|
|
|
|
|
2005-09-06 19:52:46 +02:00
|
|
|
|
|
|
|
def check_for_int(data):
|
|
|
|
"""
|
|
|
|
gets a string. if string is an integer: return integer.
|
|
|
|
else return given string.
|
|
|
|
"""
|
|
|
|
#check if value is int
|
|
|
|
num = [n for n in data if n.isdigit()]
|
|
|
|
tmp = "".join(num)
|
|
|
|
if tmp == data:
|
|
|
|
ret = int(data)
|
|
|
|
else:
|
|
|
|
ret = data
|
2005-09-06 21:05:10 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
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)
|
2005-09-23 16:37:07 +02:00
|
|
|
return tuple(returnlist)
|
|
|
|
|
|
|
|
def dict_coords_to_gnugo_coords(coords,size):
|
|
|
|
"""
|
|
|
|
gets a (x,y) coordinate tuple and boardsize.
|
|
|
|
returns a string in gnugo syntax. examples:
|
|
|
|
gets (1,1), returns "A7".
|
|
|
|
gets (6,2), returns "B2".
|
|
|
|
"""
|
|
|
|
letterlist = [" "]
|
|
|
|
letterlist.extend(list(string.letters[26:]))
|
|
|
|
letterlist.remove("I")
|
|
|
|
letter = letterlist[coords[1]]
|
|
|
|
digit = size+1-coords[0]
|
|
|
|
return letter+str(digit)
|
|
|
|
|
2005-10-09 15:58:06 +02:00
|
|
|
|
|
|
|
def parse_static_gnugo_sgf(s):
|
|
|
|
"""
|
|
|
|
gets a string containing the data saved by the gnugo "printsgf" order.
|
|
|
|
"""
|
|
|
|
#create a list of letters
|
|
|
|
letters = [" "]
|
|
|
|
letters.extend(list(string.letters)[0:26])
|
|
|
|
|
|
|
|
#dicitonary to return
|
|
|
|
ret = {}
|
|
|
|
|
|
|
|
#removing newlines vom given string
|
|
|
|
s = s.replace("\r\n","") #windoze
|
|
|
|
s = s.replace("\n","") #unix
|
|
|
|
|
|
|
|
#getting the board size
|
|
|
|
# looks like SZ[19]
|
|
|
|
# the pattern finds a number ([0-9]) with length (1-2) between "SZ[" and "]"
|
|
|
|
boardsize = re.search("SZ\[([0-9]{1,2})\]",s).groups()[0]
|
|
|
|
boardsize = int(boardsize)
|
|
|
|
ret["size"] = boardsize
|
|
|
|
|
|
|
|
|
|
|
|
#some regexp patterns:
|
|
|
|
#(?: ...) groups items
|
|
|
|
stones = "%s((?:\[[a-z][a-z]\])*)" #dont forget to set %s!
|
|
|
|
whitestones = stones % "AW"
|
|
|
|
blackstones = stones % "AB"
|
|
|
|
stonestolist = "\[([a-z][a-z])\]"
|
|
|
|
|
|
|
|
#getting white stones
|
|
|
|
#looks like AW[bb][cb][cc][cd][de][df]
|
|
|
|
|
|
|
|
stonestring = re.search(whitestones,s).groups()[0]
|
|
|
|
rawlist = re.findall(stonestolist,stonestring)
|
|
|
|
for item in rawlist:
|
|
|
|
#fill in specific values
|
|
|
|
ret[(letters.index(item[0]),letters.index(item[1]))] = 1
|
|
|
|
|
|
|
|
|
|
|
|
#getting black stones
|
|
|
|
stonestring = re.search(blackstones,s).groups()[0]
|
|
|
|
rawlist = re.findall(stonestolist,stonestring)
|
|
|
|
for item in rawlist:
|
|
|
|
#fill in specific values
|
|
|
|
ret[(letters.index(item[0]),letters.index(item[1]))] = 2
|
|
|
|
|
|
|
|
#who's turn is it?
|
|
|
|
if string.find(s,"PL[B]")>0:
|
|
|
|
ret["player"] = "black"
|
|
|
|
else:
|
|
|
|
ret["player"] = "white"
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-09-23 16:37:07 +02:00
|
|
|
def test():
|
|
|
|
print dict_coords_to_gnugo_coords((6,5),7)
|
2005-10-09 15:58:06 +02:00
|
|
|
|
|
|
|
#sample output
|
|
|
|
s = """(;
|
|
|
|
GM[1]
|
|
|
|
FF[4]
|
|
|
|
SZ[19]
|
|
|
|
GN[GNU Go 3.7.4 load and print]
|
|
|
|
DT[2005-10-09]
|
|
|
|
KM[5.5]
|
|
|
|
RU[Japanese]
|
|
|
|
AP[GNU Go:3.7.4]
|
|
|
|
AW[bb][cb][cc][cd][de][df]
|
|
|
|
[ag][cg][ah][ch][dh][ai][bi][ci]
|
|
|
|
AB[ba][ab][ac][bc][bd][be][ce][af][cf][bg][bh]
|
|
|
|
PL[B]
|
|
|
|
)"""
|
|
|
|
print parse_static_gnugo_sgf(s)
|
2005-09-23 16:37:07 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test()
|
|
|
|
|
|
|
|
|