Hooking, patching moving...

This commit is contained in:
José Carlos Cuevas 2014-08-12 01:37:55 +02:00
parent 5dae2e797e
commit 33ffaa190e
3 changed files with 71 additions and 30 deletions

31
app.py
View file

@ -17,6 +17,7 @@ urls = (
'/tyedye/', 'summary',
'/tyedye/stats', 'statistics',
'/tyedye/stats/', 'statistics',
'/tyedye/stats/(.+)', 'statistics',
'/tyedye/register', 'register',
'/tyedye/register/', 'register',
'/tyedye/player/(.+)', 'player',
@ -167,13 +168,33 @@ class summary(object):
class statistics(object):
def GET(self):
game_name = get_game_name()
def GET(self, player_id = None):
stats = []
for stat in db.select("stats"):
stats.append(stat)
if not player_id:
for stat in db.select("stats"):
stats.append(stat)
return json.dumps(stats)
return json.dumps(stats)
else:
for p in db.select('players', where = 'code = $player_id', vars = locals()):
player_data = dict(p)
player_name = player_data['name']
print player_name
for s in db.select('sheet', where = 'player_name = $player_name', vars = locals()):
try:
stats_val = dict()
stat_name, def_val = get_stat_info_by_id(s.stat_id)
stats_val[stat_name] = s.value
stats.append(stats_val)
except Exception as e:
continue
return json.dumps(stats)
def POST(self):
pass

View file

@ -1,7 +1,7 @@
// Model definition
var Stat = Backbone.Model.extend({
urlRoot: '/tyedye/stat',
urlRoot: '/tyedye/stat/',
defaults: {
name: 'New Stat',
value: 0,
@ -14,9 +14,28 @@ var Stat = Backbone.Model.extend({
var Stats = Backbone.Collection.extend({
model: Stat,
url: "/tyedye/stats/",
initialize: function () {
this.fetch();
url: function () {
if (this.generic) {
return "/tyedye/stats/";
} else {
return "/tyedye/stats/" + this.id;
}
},
initialize: function (models, player_id) {
this.generic = true;
if (typeof player_id != 'undefined') {
this.id = player_id;
this.generic = false;
}
this.fetch({ reset: true });
if (!this.generic) {
this.on('reset', function () {
this.each(function (statistic) {
$("#player_stats" + this.id )
.append('<label>' + statistic.name + '</label><input type="text" name="' + statistic.name + this.id + '">');
});
});
}
}
});
@ -25,9 +44,14 @@ var Player = Backbone.Model.extend({
defaults: {
name: 'New Player',
stats: new Stats,
code: 0,
},
initialize: function () {
this.on("change", function (model) {});
// this.on("change", function (model) {});
if (this.get('code') != 0) {
var player_stats = new Stats([], this.get('code'));
this.set('stats', player_stats);
}
}
});
@ -35,7 +59,7 @@ var Players = Backbone.Collection.extend({
model: Player,
url: "/tyedye/players/",
initialize: function () {
this.fetch();
this.fetch({ reset: true });
}
});
@ -63,26 +87,20 @@ Backbone.history.start({root: '/tyedye'});
var PlayerListView = Backbone.View.extend({
initialize: function () {
this.listenTo(this.collection, 'add', this.render);
this.listenTo(this.collection, 'reset', this.render);
},
collection: players,
render: function () {
// Load and compile the template
var template = _.template( $("#player_list_template").html(), { 'player_collection' : players,
'player_num' : this.collection.length,
'stats_num' : stats.length } );
var template = _.template( $("#player_list_template").html(), {});
// Put the compiled html into our 'el' element, putting in on the webpage
this.$el.html(template);
this.collection.each(function (playerItem) {
$("div#player_ul_list").append('<h3 style="font-size: large;"><a href="/tyedye/#/player/' + playerItem.get('code') + '/">' + playerItem.get('name') + '</a></h3><div id="player_stats' + playerItem.get('code') + '" style="display: none;"></div>');
});
$("#player_number").text(this.collection.length);
}
});
var player_list_view = new PlayerListView({ el: $("#player_list") });
function listUlPlayer (listPlayers) {
var html = '';
listPlayers.each(function (item) {
html += '<li><a href="/tyedye/player/' + item.code '">' + item.name + '</a></li>';
};
return html;
}

View file

@ -38,6 +38,9 @@ $def with (main_block,sidebar_block, game_name)
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/public/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="/static/public/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="/static/public/ico/favicon.png">
<!-- jQuery UI -->
<link href="/static/public/jquery-ui/jquery-ui.css" rel="stylesheet">
</head>
<body>
<!-- begin navbar -->
@ -75,15 +78,14 @@ $def with (main_block,sidebar_block, game_name)
<div class='span9'>
$:main_block
<!-- Out main app blocks are now in control of our backbone app -->
<h2>$game_name quick summary</h2>
<p>Registered players: <span id="player_number">0</span></p>
<p>Registered stats: <span id="stat_number">0</span></p>
<div id="player_list"></div>
<script type="text/template" id="player_list_template">
<h2>$game_name quick summary</h2>
<p>Registered players: <%= player_num %></p>
<p>Registered stats: <%= stats_num %></p>
<p>Player list:<br />
<ul id="player_ul_list">
<%= listUlPlayer(player_collection) %>
</ul>
<div id="player_ul_list">
</div>
</script>
</div>
</div>