-
Notifications
You must be signed in to change notification settings - Fork 23
/
leaderboard.js
56 lines (49 loc) · 1.55 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
51
52
53
54
55
56
import { Meteor } from 'meteor/meteor';
import { Template } from "meteor/templating";
import { Mongo } from "meteor/mongo";
import { Session } from "meteor/session";
import { Random } from "meteor/random";
// Set up a collection to contain player information. On the server,
// it is backed by a MongoDB collection named "players".
const Players = new Mongo.Collection("players");
if (Meteor.isClient) {
Template.leaderboard.helpers({
players: function () {
return Players.find({}, { sort: { score: -1, name: 1 } });
},
selectedName: function () {
const 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) {
const names = ["Ada Lovelace", "Grace Hopper", "Marie Curie",
"Carl Friedrich Gauss", "Nikola Tesla", "Claude Shannon"];
names.forEach(function (name) {
Players.insert({
name: name,
score: Math.floor(Random.fraction() * 10) * 5
});
});
}
});
}