From 6abbcee5338f90166b157d8a8cc86fd6b9d9d19e Mon Sep 17 00:00:00 2001 From: phear Date: Tue, 13 Dec 2005 10:10:34 +0000 Subject: [PATCH] game overview now works using clearsilver. --- deletegame.py | 40 ++++++---------- helper.py | 4 +- login.py | 94 ++++++++++++++++---------------------- playgame.py | 51 +-------------------- templates/not_logged_in.cs | 3 ++ templates/overview.cs | 38 +++++++++++++++ 6 files changed, 99 insertions(+), 131 deletions(-) create mode 100644 templates/not_logged_in.cs create mode 100644 templates/overview.cs diff --git a/deletegame.py b/deletegame.py index 23bd88d..9393b4c 100644 --- a/deletegame.py +++ b/deletegame.py @@ -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"]+"
") - 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) - - #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!') \ No newline at end of file + #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() diff --git a/helper.py b/helper.py index 2959ee0..7b750a7 100644 --- a/helper.py +++ b/helper.py @@ -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 diff --git a/login.py b/login.py index 6ff0847..d8dc112 100755 --- a/login.py +++ b/login.py @@ -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.
" 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()+ """ -

Current Games:

- - """ + 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 += "" - tmp += "" - tmp += "" + if cleanlist != []: + settings["Data.GamelistNotEmpty"] = 1 + else: + settings["Data.GamelistNotEmpty"] = 0 for item in gamelist: if (item != None) and (item != "None"): - tmp += '\n\n' - tmp += '\n' % item - tmp += '\n' mygame = database.GobanTable.get(item) description = mygame.description if (description == None) or (description == "None") or (description == ""): description = item - tmp += '\n' % description + settings["Data.Games.%s.Description" % item] = mygame.description + settings["Data.Games.%s.Name" % item] = item players = mygame.players() - tmp += '\n' % players[0] - tmp += '\n' % players[1] - tmp += '\n' % helper.format_time(mygame.created) - tmp += '\n' % helper.format_time(mygame.lastmove) - tmp += '\n' - tmp += '\n' - if helper.clean_list(gamelist) != []: - tmp += '
Name of gameWhiteBlackTime of CreationTime of last move
%s%s%s%s%s
' - 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 += "

New Game:

\n" - if counter > 0: - data+= "You have %s free game slots.
" % counter - data += ' Start a new game
' - 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 \ No newline at end of file diff --git a/playgame.py b/playgame.py index ae6b2ac..a66e1cf 100644 --- a/playgame.py +++ b/playgame.py @@ -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 += """
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 += "
It's your turn.
\n" - else: - data += "
This is not your turn. You have to wait for the move of the other player.
\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: """ diff --git a/templates/not_logged_in.cs b/templates/not_logged_in.cs new file mode 100644 index 0000000..2619a7f --- /dev/null +++ b/templates/not_logged_in.cs @@ -0,0 +1,3 @@ + +You must be logged in to access this page. + \ No newline at end of file diff --git a/templates/overview.cs b/templates/overview.cs new file mode 100644 index 0000000..c2f8665 --- /dev/null +++ b/templates/overview.cs @@ -0,0 +1,38 @@ + + +

Current Games:

+ + + + + + + + + + + + + + + + + + + + +
Name of gameWhiteBlackTime of CreationTime of last move
+ +
+ + + +

New Game:

+ + You have free game slots.
+ Start a new game
+ + "Sorry, all your game slots are in use." + + + \ No newline at end of file