display of a board seems to work again.

This commit is contained in:
phear 2005-11-24 18:31:33 +00:00
parent f09a9e86e0
commit 6c3db3caf8
6 changed files with 119 additions and 100 deletions

View file

@ -1,5 +1,7 @@
import goban,helper,database,login,gnugo
import string
from cherrypy import cpg
from cherrypy.lib import httptools
DEBUG = 1
@ -87,3 +89,100 @@ def main(req,form):
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"]
print game
if myuser.sessionid == sessionid:
if coord != None:
return coord
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 += '<div id="board">'
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 += '<input type=image class="goban" src="/img/topleftline'+stone+'.png" name="coord" value="('+sx+','+sy+')"\n>'
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/topleftline%s.png"\n></a>' % (sx,sy,gamename,stone)
elif (x == 1) and (y == size): # upper right
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/toprightline%s.png"\n></a><br>' % (sx,sy,gamename,stone)
elif (x == size) and (y == size): # lower right
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/bottomrightline%s.png"\n></a><br>' % (sx,sy,gamename,stone)
elif (x == size) and (y == 1): # lower left
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/bottomleftline%s.png"\n></a>' % (sx,sy,gamename,stone)
elif (y == 1): #left line
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/leftline%s.png"\n></a>' % (sx,sy,gamename,stone)
elif (x == 1): # top line
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/topline%s.png"\n></a>' % (sx,sy,gamename,stone)
elif (y == size): # right line
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/rightline%s.png"\n></a><br>' % (sx,sy,gamename,stone)
elif (x == size): #bottom line
data += '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/bottomline%s.png"\n></a>' % (sx,sy,gamename,stone)
else: # hoshi or empty inner field
defaultfield = '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/centerline%s.png"\n></a>' % (sx,sy,gamename,stone)
#too lazy to make special images for hoshi fields with stones:
if gobandict[(x,y)] == 1:
hoshifield = '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/centerline_white.png"\n></a>' % (sx,sy,gamename)
elif gobandict[(x,y)] == 2:
hoshifield = '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/centerline_black.png"\n></a>' % (sx,sy,gamename)
else: #empty hoshi
hoshifield = '<a href="/playgame/?coord=%s,%s&game=%s"><img src="/img/hoshi.png"\n></a>' % (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 += '</div>'
data += helper.footer()
return data
index.exposed = True