-
Notifications
You must be signed in to change notification settings - Fork 17
/
CoderClicker.js
80 lines (69 loc) · 2.31 KB
/
CoderClicker.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
78
79
80
/*coderClicker.js
By: Edward Jiang
A clone of Cookie Clicker that's multiplayer for a CodeDay Workshop */
//Define items.
Items = [{name: "Monkey", cost: 500}];
if (Meteor.isClient) {
//Switch logins to username
Accounts.ui.config({
passwordSignupFields: 'USERNAME_ONLY'
});
//Subscribe to Meteor.users
Meteor.subscribe('userData');
//Export a list of all players (sorted by money), all things you can buy, and the current user to the template system
Template.leaderboard.players = function () {
return Meteor.users.find({}, {sort: {'money': -1}});
};
Template.leaderboard.items = function () {
return Items;
}
Template.leaderboard.user = function () {
return Meteor.user();
}
//Call click / buy functions on server when you click on the buttons
Template.leaderboard.events({
'click input.code': function () {
Meteor.call('click');
}
});
Template.leaderboard.events({
'click input.buy': function (event) {
Meteor.call('buy', event.target.id); //the 'id' of the button is the cost. So there aren't really items, per se
}
});
//Registering a helper function to Handlebars so we can format currency correctly.
Handlebars.registerHelper('formatCurrency', function(number) {
return number.toLocaleString();
});
}
if (Meteor.isServer) {
//Construct user with no money & DPS
Accounts.onCreateUser(function(options, user) {
user.money = 0;
user.rate = 0;
return user;
})
//Publish user data to the clients
Meteor.publish("userData", function () {
return Meteor.users.find({}, {sort: {'money': -1}});
});
//Update people's money every second.
Meteor.startup(function () {
Meteor.setInterval(function() {
Meteor.users.find({}).map(function(user) {
Meteor.users.update({_id: user._id}, {$inc: {'money': user.rate}})
});
}, 1000)
});
}
Meteor.methods({
buy: function(amount) {
if(Meteor.user().money >= amount && amount > 0) //check that people have enough money, and it's positive
//Give people DPS at 1/500 DPS per $, rounded down. Subtract money
Meteor.users.update({_id: this.userId}, {$inc: {'rate': (Math.floor(amount/500)), 'money': (0-amount)}});
},
click: function () {
//Give people $25 per click
Meteor.users.update({_id: this.userId}, {$inc: {'money': 25}});
},
})