2005-11-23 18:34:20 +01:00
|
|
|
import goban,helper,database,login,gnugo
|
2005-09-14 18:48:07 +02:00
|
|
|
import string
|
2005-11-24 19:31:33 +01:00
|
|
|
from cherrypy import cpg
|
|
|
|
from cherrypy.lib import httptools
|
2005-09-14 18:48:07 +02:00
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
DEBUG = 1
|
|
|
|
|
2005-09-14 18:48:07 +02:00
|
|
|
def is_my_turn(req,form,gobandict):
|
|
|
|
"""
|
|
|
|
gets request and util.FiedStorage form.
|
|
|
|
check wether or not the current this is the players turn.
|
|
|
|
return true or false.
|
|
|
|
"""
|
|
|
|
me = form["username"]
|
|
|
|
player1 = gobandict["player1"]
|
|
|
|
player2 = gobandict["player2"]
|
2005-10-09 19:27:17 +02:00
|
|
|
play = gobandict["play"]
|
2005-09-14 18:48:07 +02:00
|
|
|
|
2005-10-09 19:27:17 +02:00
|
|
|
if ((player1 == me) and (play == "white")) or ((player2 == me) and (play == "black")):
|
|
|
|
return True
|
|
|
|
else:
|
2005-10-11 23:48:50 +02:00
|
|
|
return False
|
2005-09-14 18:48:07 +02:00
|
|
|
|
|
|
|
|
2005-10-09 19:27:17 +02:00
|
|
|
def create_gobandict(req,form,gamename):
|
|
|
|
"""
|
|
|
|
gets a gamename
|
|
|
|
loads sgf, transforms it to dict, writes stuff like player names to dict
|
|
|
|
returns dict
|
|
|
|
"""
|
2005-11-23 18:34:20 +01:00
|
|
|
#read goban sgf from database
|
|
|
|
mygame = database.GobanTable.byName(gamename)
|
|
|
|
sgf = mygame.sgf
|
2005-10-09 19:27:17 +02:00
|
|
|
gobandict = gnugo.parse_static_gnugo_sgf(sgf)
|
2005-11-23 18:34:20 +01:00
|
|
|
gobandict["player1"] = mygame.player1
|
|
|
|
gobandict["player2"] = mygame.player2
|
|
|
|
gobandict["turn_number"] = mygame.turn_number
|
|
|
|
gobandict["name"] = mygame.name
|
|
|
|
gobandict["sgf"] = sgf
|
2005-10-09 19:27:17 +02:00
|
|
|
return gobandict
|
2005-09-12 19:30:02 +02:00
|
|
|
|
|
|
|
def main(req,form):
|
|
|
|
"""
|
|
|
|
display selected goban and manage user input.
|
|
|
|
"""
|
2005-09-23 16:37:07 +02:00
|
|
|
|
2005-09-14 16:29:58 +02:00
|
|
|
try:
|
|
|
|
gamename = form["game"]
|
|
|
|
except:
|
|
|
|
gamename = ""
|
|
|
|
if gamename != "":
|
2005-09-23 16:37:07 +02:00
|
|
|
data = ""
|
|
|
|
req.write(helper.header())
|
|
|
|
#helper.debug(req,form,str(form.keys()))
|
2005-10-09 19:27:17 +02:00
|
|
|
gobandict = create_gobandict(req,form,gamename)
|
2005-09-14 18:48:07 +02:00
|
|
|
#check if user has already clicked onto a field:
|
2005-09-23 16:37:07 +02:00
|
|
|
click_on_field = False
|
2005-09-14 18:48:07 +02:00
|
|
|
for item in form.keys():
|
|
|
|
if string.find(item,").x") > 0:
|
2005-09-23 16:37:07 +02:00
|
|
|
click_on_field = True
|
|
|
|
if click_on_field:
|
|
|
|
#if yes: is this the user's turn? then process form.
|
2005-09-14 18:48:07 +02:00
|
|
|
if is_my_turn(req,form,gobandict):
|
2005-09-23 16:37:07 +02:00
|
|
|
helper.debug(req,form,"its my turn , i am going to process the form: --")
|
|
|
|
retstring = goban.process_form(req,form,gobandict)
|
|
|
|
if retstring != "":
|
|
|
|
helper.debug(req,form,"playgame.main says: "+str(retstring))
|
|
|
|
else:
|
|
|
|
#reload gobandict, it has been changed.
|
2005-10-09 19:27:17 +02:00
|
|
|
gobandict = create_gobandict(req,form,gamename)
|
|
|
|
|
2005-09-23 16:37:07 +02:00
|
|
|
else:
|
2005-11-23 18:34:20 +01:00
|
|
|
helper.debug(req,form,"it's not my turn.")
|
2005-09-23 16:37:07 +02:00
|
|
|
data += """<br>Turn number: %s. %s plays.\n
|
2005-10-09 19:27:17 +02:00
|
|
|
""" % (str(gobandict["turn_number"]), (string.capitalize(gobandict["play"])))
|
2005-09-14 16:29:58 +02:00
|
|
|
|
|
|
|
#check whether its our turn
|
|
|
|
#if yes: print 'your move' and display goban and process move
|
|
|
|
#if not: print '...s move' and display goban.
|
2005-09-14 18:48:07 +02:00
|
|
|
if is_my_turn(req,form,gobandict):
|
2005-11-23 18:34:20 +01:00
|
|
|
data += "<br>It's your turn.<br>\n"
|
2005-09-14 18:48:07 +02:00
|
|
|
else:
|
2005-09-23 16:37:07 +02:00
|
|
|
data += "<br>This is not your turn. You have to wait for the move of the other player.<br>\n"
|
2005-09-14 18:48:07 +02:00
|
|
|
#print goban
|
2005-09-14 16:29:58 +02:00
|
|
|
data += goban.display_goban(gobandict,req,form)
|
2005-09-20 11:51:12 +02:00
|
|
|
data += login.navigation_bar(req,form)
|
2005-10-10 00:26:07 +02:00
|
|
|
data += helper.footer(req,form,1)
|
2005-09-14 16:29:58 +02:00
|
|
|
req.write(data)
|
2005-09-12 19:30:02 +02:00
|
|
|
|
2005-09-14 16:29:58 +02:00
|
|
|
else:
|
2005-09-14 18:48:07 +02:00
|
|
|
req.write('Error: You have to select a game to play it!')
|
2005-11-24 19:31:33 +01:00
|
|
|
|
|
|
|
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
|