-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleaderboard.js
50 lines (44 loc) · 1.33 KB
/
leaderboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Set up a collection to contain player information. On the server,
// it is backed by a MongoDB collection named "players".
Players = new Mongo.Collection("players");
if (Meteor.isClient) {
Template.leaderboard.helpers({
players: function () {
return Players.find({}, { sort: { score: -1, name: 1 } });
},
selectedName: function () {
var player = Players.findOne(Session.get("selectedPlayer"));
return player && player.name;
}
});
Template.leaderboard.events({
'click .inc': function () {
Players.update(Session.get("selectedPlayer"), {$inc: {score: 5}});
}
});
Template.player.helpers({
selected: function () {
return Session.equals("selectedPlayer", this._id) ? "selected" : '';
}
});
Template.player.events({
'click': function () {
Session.set("selectedPlayer", this._id);
}
});
}
// On server startup, create some players if the database is empty.
if (Meteor.isServer) {
Meteor.startup(function () {
if (Players.find().count() === 0) {
var names = ["Ada Lovelace", "Grace Hopper", "Marie Curie",
"Carl Friedrich Gauss", "Nikola Tesla", "Claude Shannon"];
_.each(names, function (name) {
Players.insert({
name: name,
score: Math.floor(Random.fraction() * 10) * 5
});
});
}
});
}