From 055600e611b813f18fe9419eb352dfa3261946cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Tue, 4 Jun 2013 01:27:46 +0200 Subject: [PATCH] Almost all administration is done by now. --- app.py | 166 ++++++++++++++++++++++++++++++++++++------- database_template.db | Bin 6144 -> 8192 bytes views/config.html | 17 ++--- views/edit_stat.html | 2 +- 4 files changed, 145 insertions(+), 40 deletions(-) diff --git a/app.py b/app.py index 486f956..5ee6a33 100644 --- a/app.py +++ b/app.py @@ -14,45 +14,82 @@ db = web.database(dbn = 'sqlite', db = 'game.db') urls = ( '/app', 'summary', '/app/', 'summary', - '/app/config', 'config', - '/app/config/', 'config', - '/app/config/add', 'stat_add', - '/app/config/remove/(.+)', 'remove_stat', - '/app/config/edit/(.+)', 'edit_stat', + '/app/stats', 'statistics', + '/app/stats/', 'statistics', + '/app/stats/add', 'stat_add', + '/app/stats/remove/(.+)', 'remove_stat', + '/app/stats/edit/(.+)', 'edit_stat', '/app/register', 'register', '/app/register/', 'register', '/app/player/(.+)', 'player', + '/app/player_remove/(.+)', 'remove_player', + '/app/player_edit/(.+)', 'edit_player', + '/app/config', 'config', + '/app/config/', 'config', ) # Forms used by the app +# See dynamic_register_form for the +# character sheet configuration add_stat = form.Form( form.Textbox('stat_name', description = 'Stat name:'), form.Textbox('default_value', description = 'Default value:', value = 0), - form.Button('Add Stat') + form.Button('OK') ) +config_form = form.Form( + form.Textbox('game_name', + description = 'Game name'), + form.Button('Send')) + app = web.application(urls, globals()) # Auxiliary functions -def render_web(game_name, main_block): +def render_web(main_block): """ Cobbles together all the pieces we've broken our templates, so we can have a kind of modular design """ + game_name = get_game_name() sidebar = render.sidebar() return render.layout(main_block, sidebar, game_name) +def get_game_name(): + """ + Gets the name of the game + """ + try: + for e in db.select('game_config'): + name = e.game_name + + if name == '': + return "My Game" + + else: + return name + + except: + return "My Game" + def dynamic_register_form(player_name = 'New Player'): """ Creates a register new player dynamicly """ - reg_elems = [form.Textbox('name', - description = 'Player name:', - value = player_name)] + if player_name == 'New Player': + reg_elems = [form.Textbox('name', + description = 'Player name:', + value = player_name)] + + else: + reg_elems = [form.Textbox('name', + description = 'Player name:', + value = player_name, + readonly = 'on')] + if player_name == 'New Player': for stat in db.select('stats'): @@ -106,8 +143,7 @@ def get_stat_info_by_id(stat_id): class summary(object): def GET(self): - # TODO: Get from main configuration file - game_name = "My Game" + game_name = get_game_name() # Variable initialization player_num = 0 @@ -127,23 +163,23 @@ class summary(object): player_num, stats_num, players) - return render_web(game_name, summary) + return render_web(summary) -class config(object): +class statistics(object): def GET(self): - game_name = "My Game" + game_name = get_game_name() stats = [] for stat in db.select("stats"): stats.append(stat) add_stat_form = add_stat.render() - config = render.config(game_name, - stats, - add_stat_form) + config = render.statistics(game_name, + stats, + add_stat_form) - return render_web(game_name, config) + return render_web(config) class stat_add(object): @@ -169,7 +205,6 @@ class remove_stat(object): class edit_stat(object): def GET(self, index_to_remove): - game_name = "My Game" try: index = int(index_to_remove) @@ -180,7 +215,7 @@ class edit_stat(object): edit_stat_form = edit_form.render() edit_stat = render.edit_stat(stat_info, edit_stat_form) - return render_web(game_name, edit_stat) + return render_web(edit_stat) except Exception as e: return "Ooops! Wrong index!" @@ -210,13 +245,11 @@ class edit_stat(object): class register(object): def GET(self): - game_name = "My Game" form = dynamic_register_form() - register_block = render.register(form.render()) - return render_web(game_name, register_block) + register_block = render.register(form.render(), True) + return render_web(register_block) def POST(self): - game_name = "My Game" stats = {} form = dict(web.input()) name = form['name'] @@ -238,8 +271,87 @@ class register(object): value = final_value) stats[key] = final_value - result_render = render.player(name, code, stats) - return render_web(game_name, result_render) + result_render = render.player(name, code, stats, False) + return render_web(result_render) + + +class player(object): + def GET(self, player_name): + for p in db.select('players', where = 'name = $player_name', vars = locals()): + player_data = p + + stats = {} + for s in db.select('sheet', where = 'player_name = $player_name', vars = locals()): + try: + stat_name, def_val = get_stat_info_by_id(s.stat_id) + stats[stat_name] = s.value + + except Exception as e: + continue + + result_render = render.player(player_data.name, player_data.code, stats, True) + return render_web(result_render) + + +class edit_player(object): + def GET(self, player_name): + form = dynamic_register_form(player_name) + edit_block = render.register(form.render(), False) + return render_web(edit_block) + + def POST(self, player_name): + stats = {} + form = dict(web.input()) + name = form['name'] + code = generate_uuid(name) + + for key in form: + if key != 'name' and key != 'Register Player' and key != 'Modify Player': + stat_id, default_value = get_stat_info(key) + if form[key] != '' and form[key] != '0': + final_value = form[key] + + else: + final_value = default_value + + db.update('sheet', + where = 'stat_id = $stat_id AND player_name = $name', + value = final_value, + vars = locals()) + stats[key] = final_value + + result_render = render.player(name, code, stats, False) + return render_web(result_render) + + +class remove_player(object): + def GET(self, player_name): + try: + db.delete('players', where = 'name = $player_name', vars = locals()) + db.delete('sheet', where = 'player_name = $player_name', vars = locals()) + + except: + pass + + raise web.seeother('/app') + + +class config(object): + def GET(self): + form = config_form() + config = render.config(form.render()) + return render_web(config) + + def POST(self): + form = config_form() + if form.validates(): + new_game_name = form.d.game_name + db.delete('game_config', where = 'game_name = game_name') + db.insert('game_config', game_name = new_game_name) + raise web.seeother('/app') + + else: + return "Ooops! Something went wrong." if __name__ == "__main__": diff --git a/database_template.db b/database_template.db index 7ede5c313ac94fb4458b827f664d0b818a26f236..1e0f6ec706189bb446759968aea704ac1ea8ef19 100644 GIT binary patch delta 227 zcmZoLXmFSyEy%;bz`y~-Fu*fW$5@(&LHCg=FS7&#JEI5#(=q01=Ch0s8FLv$m>niI z&Wa4@ViQ+ZW-KpBOv*`3Ps~k?PtMOv%S=aNvO5R4I)=C^gg83+xGEq?C}@B=c|ctX zA+8Z23IRc$zK%hW3f``fnwxVO*Dx}dGiXd~R53T@W)t^RW@O0BOG&LLF3ia+NsUh| cEy)M-uaY%XW+6j{W>vWSBP0PEmE&;S4c delta 75 zcmZp0XfT){Ey&Hlz`zE?Fu*-g$5@=3LHCg=FS7&#D`PGL(=q01=Ch0s8FMEKvV7m1 X%eaP-`42<##KuS_=0BUunL9-Q{9zH5 diff --git a/views/config.html b/views/config.html index c49a5c2..c5e7260 100644 --- a/views/config.html +++ b/views/config.html @@ -1,14 +1,7 @@ -$def with (game_name, stats, add_stat_form) -

$game_name stat list

- -
-
- $:add_stat_form +$def with (config_form) +

Main game configuration

+
+ + $:config_form
diff --git a/views/edit_stat.html b/views/edit_stat.html index bdcf0ec..584dc5d 100644 --- a/views/edit_stat.html +++ b/views/edit_stat.html @@ -2,7 +2,7 @@ $def with (stat_info, edit_stat_form)

Editing $stat_info.name stat

Stat current default value: $stat_info.default_value

-
+ $:edit_stat_form