game overview now works using clearsilver.

This commit is contained in:
phear 2005-12-13 10:10:34 +00:00
parent 34420b0598
commit 6abbcee533
6 changed files with 99 additions and 131 deletions

View file

@ -1,31 +1,21 @@
import login,helper,database
import helper,database
#helper = apache.import_module("helper")
def main(req,form):
def main(gamename):
"""
get request,util.FieldStorage.
manage the removal of game from game slots of players and
delete game from database.
TODO: add option do undo deletion when a failure happens
"""
helper.debug(req,form,str(form.keys())+" sessionid in form:"+form["sessionid"]+"<br>")
try:
gamename = form["game"]
except:
gamename = ""
if gamename != "":
#get player names from game table in database
mygame=database.GobanTable.byName(gamename)
players = mygame.players()
#free game slots of players
for player in players:
myuser = database.Users.byUsername(player)
myuser.removegame(gamename)
#get player names from game table in database
mygame=database.GobanTable.get(gamename)
players = mygame.players()
#free game slots of players
for player in players:
myuser = database.Users.byUsername(player)
myuser.removegame(gamename)
#delete table
mygame.destroySelf()
#now display game overview form.
login.game_overview_form(req,form)
else:
req.write('Error: You have to select a game to delete it!')
#delete table
mygame.destroySelf()

View file

@ -18,7 +18,7 @@ def cs_render(cs_path,settings = {},hdf_path = ""):
hdf=neo_util.HDF(hdf_path)
for key in settings.keys():
hdf.setValue(key,settings[key])
hdf.setValue(key,str(settings[key]))
cs= neo_cs.CS(hdf)
cs.parseFile(cs_path)
return cs.render()
@ -89,6 +89,7 @@ def format_time(mytime):
gets output of time.time()
formats it nicely and returns as string.
"""
mytime = int(mytime)
acttime = int(time.time())
if mytime == None:
retstring = ""
@ -101,7 +102,6 @@ def format_time(mytime):
else:
retstring = "%dh %dm ago" % (hour,minute)
else:
mytime = int(mytime)
retstring = time.strftime("%Y-%m-%d %H:%M",time.gmtime(mytime))
return retstring

View file

@ -1,5 +1,5 @@
#import database,init_webgo,helper,gamecreation
import helper,database
import helper,database,deletegame
from sets import Set as set
from cherrypy import cpg
from cherrypy.lib import httptools
@ -43,19 +43,6 @@ def logout(req,form):
myuser.timeout = 0
def main(req,form):
#debug
helper.debug(req,form)
#req.write(helper.footer())
if "sessionid" not in form.keys():
req.write(login_form(req,form))
elif ("game overview" in form.keys()) and ("username" in form.keys()) and ("sessionid" in form.keys()):
game_overview_form(req,form)
elif ("logout" in form.keys()) and ("username" in form.keys()):
logout(req,form)
process_form(req,form)
else:
process_form(req,form)
"""
class Login:
def index(self,username=None,password=None):
@ -93,7 +80,6 @@ class Login:
cpg.request.sessionMap["username"] = username
#now display list of games.
httptools.redirect("/overview")
#data += "Login accepted."
else:
message = "Login incorrect. Please try again.<br>"
data = self.login_form(message)
@ -107,14 +93,28 @@ class Login:
class Overview:
"""
give out a list of current games and stuff."""
def index(self):
def index(self,game=None,delete=None):
#load username and sesisonid from session data
username = cpg.request.sessionMap["username"]
myuser = database.Users.byUsername(username)
sessionid = cpg.request.sessionMap["_sessionId"]
if myuser.sessionid == sessionid:
return self.game_overview_form(username)
if username != None:
try:
myuser = database.Users.byUsername(username)
user = 1
except:
user = 0
if user:
if myuser.sessionid == sessionid:
if game!=None and delete != None: #delete given game
deletegame.main(game)
return self.game_overview_form(username)
else:
return helper.cs_render("templates/not_logged_in.cs")
else:
return helper.cs_render("templates/not_logged_in.cs")
else:
return "You must be logged in to access this page."
return helper.cs_render("templates/not_logged_in.cs")
def game_overview_form(self,username):
"""
@ -122,48 +122,34 @@ class Overview:
prints a form with the option to select,create and delete games.
"""
myuser = database.Users.byUsername(username)
data = helper.header()+ """
<h2> Current Games: </h2>
"""
gamelist = myuser.gamelist()
settings = {}
cleanlist = helper.clean_list(gamelist)
#display list of current games
counter = 10 - len(helper.clean_list(gamelist))
counter = 10 - len(cleanlist)
settings["Data.Counter"] = counter
settings["Data.Gamelist"] = cleanlist
tmp = ""
if helper.clean_list(gamelist) != []:
tmp += "<table><tr><td></td><td>Name of game</td><td>White</td>"
tmp += "<td>Black</td><td>Time of Creation</td><td>Time of last move</td>"
tmp += "<td></td></tr>"
if cleanlist != []:
settings["Data.GamelistNotEmpty"] = 1
else:
settings["Data.GamelistNotEmpty"] = 0
for item in gamelist:
if (item != None) and (item != "None"):
tmp += '<tr>\n<form action="/playgame/" method="get">\n'
tmp += '<input type="hidden" name="game" value="%s">\n' % item
tmp += '<td><input type=submit class="submit" value="Play"></td>\n'
mygame = database.GobanTable.get(item)
description = mygame.description
if (description == None) or (description == "None") or (description == ""):
description = item
tmp += '<td>%s</td>\n' % description
settings["Data.Games.%s.Description" % item] = mygame.description
settings["Data.Games.%s.Name" % item] = item
players = mygame.players()
tmp += '<td>%s</td>\n' % players[0]
tmp += '<td>%s</td>\n' % players[1]
tmp += '<td>%s</td>\n' % helper.format_time(mygame.created)
tmp += '<td>%s</td>\n' % helper.format_time(mygame.lastmove)
tmp += '<td><input type=submit class="submit" name="delete" value="Delete"></td>\n'
tmp += '</tr></form>\n'
if helper.clean_list(gamelist) != []:
tmp += '</table>'
if gamelist == []: #no current games
data += "You don't have any running games.\n"
else:
data += tmp
#now comes the option for creating new games.
data += "<h2>New Game:</h2>\n"
if counter > 0:
data+= "You have %s free game slots.<br>" % counter
data += '<a href="/gamecreation"> Start a new game</a><br>'
else:
data+= "Sorry, all your game slots are in use."
data+=helper.footer()
return data
settings["Data.Games.%s.Player1" % item] = mygame.players()[0]
settings["Data.Games.%s.Player2" % item] = mygame.players()[1]
settings["Data.Games.%s.Created" % item] = helper.format_time(mygame.created)
settings["Data.Games.%s.Lastmove" % item] = helper.format_time(mygame.lastmove)
return helper.cs_render("templates/overview.cs",settings)
index.exposed = True

View file

@ -27,6 +27,7 @@ def create_gobandict(req,form,gamename):
gets a gamename
loads sgf, transforms it to dict, writes stuff like player names to dict
returns dict
TODO: is this function still in use?
"""
#read goban sgf from database
mygame = database.GobanTable.byName(gamename)
@ -39,56 +40,6 @@ def create_gobandict(req,form,gamename):
gobandict["sgf"] = sgf
return gobandict
def main(req,form):
"""
display selected goban and manage user input.
"""
try:
gamename = form["game"]
except:
gamename = ""
if gamename != "":
data = ""
req.write(helper.header())
#helper.debug(req,form,str(form.keys()))
gobandict = create_gobandict(req,form,gamename)
#check if user has already clicked onto a field:
click_on_field = False
for item in form.keys():
if string.find(item,").x") > 0:
click_on_field = True
if click_on_field:
#if yes: is this the user's turn? then process form.
if is_my_turn(req,form,gobandict):
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.
gobandict = create_gobandict(req,form,gamename)
else:
helper.debug(req,form,"it's not my turn.")
data += """<br>Turn number: %s. %s plays.\n
""" % (str(gobandict["turn_number"]), (string.capitalize(gobandict["play"])))
#check whether its our turn
#if yes: print 'your move' and display goban and process move
#if not: print '...s move' and display goban.
if is_my_turn(req,form,gobandict):
data += "<br>It's your turn.<br>\n"
else:
data += "<br>This is not your turn. You have to wait for the move of the other player.<br>\n"
#print goban
data += goban.display_goban(gobandict,req,form)
data += login.navigation_bar(req,form)
data += helper.footer(req,form,1)
req.write(data)
else:
req.write('Error: You have to select a game to play it!')
class PlayGame:
"""

View file

@ -0,0 +1,3 @@
<?cs include:"templates/header.cs" ?>
You must be logged in to access this page.
<?cs include:Settings.TemplateDir +"footer.cs" ?>

38
templates/overview.cs Normal file
View file

@ -0,0 +1,38 @@
<?cs include:"templates/header.cs" ?>
<h2> Current Games: </h2>
<?cs if:Data.GamelistNotEmpty ?>
<table><tr><td></td><td>Name of game</td><td>White</td>
<td>Black</td><td>Time of Creation</td><td>Time of last move</td>
<td></td></tr>
<?cs each:game = Data.Games ?>
<tr><form action="/playgame/" method="get">
<input type="hidden" name="game" value="<?cs var:game.Name ?>">
<td><input type=submit class="submit" value="Play"></form></td>
<td><?cs var:game.Description ?></td>
<td><?cs var:game.Player1 ?></td>
<td><?cs var:game.Player2 ?></td>
<td><?cs var:game.Created ?></td>
<td><?cs var:game.Lastmove ?></td>
<td><form action="/overview/" method="get">
<input type="hidden" name="game" value="<?cs var:game.Name ?>">
<input type=submit class="submit" name="delete" value="Delete"></form></td>
</tr>
<?cs /each ?>
</table>
<?cs /if ?>
<h2>New Game:</h2>
<?cs if:Data.Counter ?>
You have <?cs var:Data.Counter ?> free game slots.<br>
<a href="/gamecreation"> Start a new game</a><br>
<?cs else ?>
"Sorry, all your game slots are in use."
<?cs /if ?>
<?cs include:Settings.TemplateDir +"footer.cs" ?>