webgo/playgame.py

132 lines
No EOL
4.2 KiB
Python

import helper,database,login,gnugo
import string
from cherrypy import cpg
from cherrypy.lib import httptools
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"]
if myuser.sessionid == sessionid:
if coord != None:
return self.process_form(game,coord)
else:
return self.display_goban(game)
else:
httptools.redirect("/login")
def is_my_turn(self,gobandict):
"""
gets request and util.FiedStorage form.
check wether or not the current this is the players turn.
return true or false.
"""
me = gobandict["username"]
player1 = gobandict["player1"]
player2 = gobandict["player2"]
play = gobandict["play"]
if ((player1 == me) and (play == "black")) or ((player2 == me) and (play == "white")):
return True
else:
return False
def display_goban(self,gamename,settings={}):
"""
gets: dictionary containing the layout of the used goban.
returns: string containing the HTML code for a clickable goban.
"""
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)
gobandict["username"] = cpg.request.sessionMap["username"]
gobandict["player1"],gobandict["player2"] = mygame.players()
size = mygame.size
settings["Data.TurnNumber"] = mygame.turn_number
settings["Data.GobanSize"] = size
settings["Data.GameName"] = gamename
settings["Data.Play"] = gobandict["play"]
if self.is_my_turn(gobandict):
settings["Data.MyTurn"] = "True"
else:
settings["Data.MyTurn"] = "False"
print gobandict
for x in range(1,size+1):
for y in range(1,size+1):
settings["Data.Goban.%d.%d.x" % (x,y)] = x
settings["Data.Goban.%d.%d.y" % (x,y)] = y
if gobandict[(x,y)] == 1:
settings["Data.Goban.%d.%d.color" % (x,y)] = "_white"
elif gobandict[(x,y)] == 2:
settings["Data.Goban.%d.%d.color" % (x,y)] = "_black"
else:
settings["Data.Goban.%d.%d.color" % (x,y)] = ""
#now check wether or not this field is hoshi
if size == 19: # 9 hoshis
if (x,y) in hoshis19x19:
settings["Data.Goban.%d.%d.hoshi" % (x,y)] = 1
else: pass
elif size == 13:
if (x,y) in hoshis13x13:
settings["Data.Goban.%d.%d.hoshi" % (x,y)] = 1
else: pass
elif size == 9:
if (x,y) in hoshis9x9:
settings["Data.Goban.%d.%d.hoshi" % (x,y)] = 1
else: pass
return helper.cs_render("templates/playgame.cs",settings)
def process_form(self,gamename,coord):
"""
gets name of a go game, coordinates of user's move.
processes move.
"""
x,y = string.split(coord,",")
position = (int(x),int(y))
mygame = database.GobanTable.get(gamename)
size = mygame.size
turn = mygame.turn_number
sgf = mygame.sgf
gobandict = gnugo.parse_static_gnugo_sgf(sgf)
gobandict["player1"],gobandict["player2"] = mygame.players()
gobandict["username"] = cpg.request.sessionMap["username"]
gobandict["name"] = gamename
gobandict["turn_number"] = turn
gobandict["sgf"] = sgf
settings = {}
if (gobandict[position] == 0): #empty field
if self.is_my_turn(gobandict):
if gnugo.is_legal(gobandict,position): #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(gobandict,position)
#write new sgf file into database
mygame.sgf = new_sgf
mygame.turn_number = turn + 1
mygame.set_time()
return self.display_goban(gamename)
else: #move not ok
settings["Data.Message"] = "This is not a legal move (says Gnugo)."
return self.display_goban(gamename,settings)
else:
settings["Data.Message"] = "This is not your turn. You have to wait for the other player."
return self.display_goban(gamename,settings)
else: #position not empty
settings["Data.Message"] = "Could not make move: Field not empty."
return self.display_goban(gamename,settings)
index.exposed = True