-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.js
77 lines (69 loc) · 2.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
PlayersList = new Mongo.Collection('players');
UserAccounts = new Mongo.Collection('user');
if(Meteor.isClient){
Template.leaderboard.helpers({
//Sorts players created by the currently logged in user by score and name
'player': function(){
var currentUserId = Meteor.userId()
return PlayersList.find({createdBy: currentUserId}, {sort: {score: -1, name: 1} })
},
//Determines if a player has been selected
'selectedClass': function(){
var playerId = this._id;
var selectedPlayer = Session.get('selectedPlayer');
if(playerId == selectedPlayer){
return "selected"
}
},
//Shows the selected player at the bottom of the screen
'showSelectedPlayer': function(){
var selectedPlayer = Session.get('selectedPlayer');
return PlayersList.findOne(selectedPlayer)
},
//Displays the player count for the current user
'countPlayers': function() {
var currentUserId = Meteor.userId()
return PlayersList.find({createdBy: currentUserId}).count();
},
});
Template.leaderboard.events({
//Checks if the click is on a player
'click .player': function(){
var playerId = this._id;
Session.set('selectedPlayer', playerId);
},
//Button to increment points (+5)
'click .increment': function(){
var selectedPlayer = Session.get('selectedPlayer');
PlayersList.update(selectedPlayer, {$inc: {score: 5} });
},
//Button to decrement points (-5)
'click .decrement': function(){
var selectedPlayer = Session.get('selectedPlayer');
PlayersList.update(selectedPlayer, {$inc: {score: -5} });
},
//Button to remove a player
'click .remove': function(){
var selectedPlayer = Session.get('selectedPlayer');
if(confirm('Are you sure you want to remove'))
PlayersList.remove(selectedPlayer);
}
});
//Adds a player into the leaderboard
Template.addPlayerForm.events({
'submit form': function(event, template){
event.preventDefault();
var currentUserId = Meteor.userId();
var playerNameVar = event.target.playerName.value;
PlayersList.insert({
name: playerNameVar,
score: 0,
createdBy: currentUserId
});
template.find("form").reset(); //Resets the form after entering a name
}
});
}
if(Meteor.isServer){
// this code only runs on the server
}