-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththebutton.js
184 lines (161 loc) · 4.8 KB
/
thebutton.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Clicks = new Mongo.Collection("clicks");
Timer = new Mongo.Collection("timer");
var TIMER_INIT = 1000 * 60;
if (Meteor.isClient) {
// We'll use this to update the pie chart more
// frequently (every 10ms) without hitting the server
var clientTimer = new ReactiveVar(TIMER_INIT);
Meteor.startup(function() {
Meteor.subscribe("userData");
Meteor.subscribe("clicks");
Meteor.subscribe("timer");
var interval;
Tracker.autorun(function() {
var timer = Timer.findOne();
if (timer && timer.value > 0 && timer.value % 1000 == 0) {
clearInterval(interval);
clientTimer.set(timer.value);
interval = Meteor.setInterval(function() {
clientTimer.set(clientTimer.get() - 10);
}, 10);
}
});
google.setOnLoadCallback(function() {
var options = {
backgroundColor: "transparent",
pieSliceBorderColor: "transparent",
slices: { 0: {color: "#C8C8C8"}, 1: {color: "#4A4A4A"} },
width: 120,
height: 120,
legend: 'none',
pieSliceText: 'none',
enableInteractivity: false
};
var chart = new google.visualization.PieChart($(".thebutton-piechart")[0]);
chart.draw(google.visualization.arrayToDataTable([
['', ''],
["gone", TIMER_INIT - clientTimer.get()],
["remaining", clientTimer.get()]
]), options);
Tracker.autorun(function() {
if (clientTimer.get() <= 0) return;
chart.draw(google.visualization.arrayToDataTable([
['', ''],
["gone", TIMER_INIT - clientTimer.get()],
["remaining", clientTimer.get()]
]), options);
});
});
});
Template.body.helpers({
numParticipants: function() {
return Clicks.find().count();
}
});
Template.thebutton.helpers({
timeRemainingWhenClicked: function() {
var click = Clicks.findOne({userId: Meteor.userId()});
if (click) {
var rem = Math.round(click.timeRemaining / 1000);
if (rem < 10)
rem = "0" + rem;
return rem;
}
},
clicked: function() {
if (!Meteor.userId()) return;
return moment(Meteor.user().date).format("MMM DD \\a\\t HH:mm:ss");
},
timeRemaining: function() {
if (!Timer.findOne()) return;
return Timer.findOne().value > 0;
}
});
Template.thebutton.events({
'click button': function(evt) {
if (!Meteor.user()) return;
var change = { timeRemaining: Timer.findOne().value, date: new Date() };
var userId = { userId: Meteor.userId() };
var click = Clicks.findOne(userId);
if (!click) {
Clicks.insert(_.extend(change, userId));
Meteor.users.update(Meteor.userId(), {$set: change});
}
}
});
Template.countdown.helpers({
countdown60s: function() {
if (!Timer.findOne()) return;
var secs = clientTimer.get() / 1000;
return secs >= 10 ? String(secs)[0] : 0;
},
countdown10s: function() {
if (!Timer.findOne()) return;
var secs = clientTimer.get() / 1000;
return secs >= 10 ? String(secs)[1] : String(secs)[0];
},
countdown100ms: function() {
if (!Timer.findOne()) return;
var ms = clientTimer.get() % 1000;
return ms > 100 ? String(ms)[0] : 0;
},
countdown10ms: function() {
if (!Timer.findOne()) return;
var ms = clientTimer.get() % 1000;
return ms > 100 ? String(ms)[1] : String(ms)[0];
}
});
}
if (Meteor.isServer) {
var serverTimer;
Meteor.startup(function() {
serverTimer = TIMER_INIT;
var timer = Timer.findOne();
if (timer) {
Meteor.setInterval(function() {
Timer.update(timer._id, {$set: {value: serverTimer}});
serverTimer -= 1000;
if (Timer.findOne().value < 0) {
Timer.update(timer._id, {$set: {value: TIMER_INIT}});
serverTimer = TIMER_INIT;
}
}, 1000);
}
else
Timer.insert({value: TIMER_INIT});
});
Meteor.methods({
reset: function() {
var timer = Timer.findOne();
if (timer) {
Timer.update(timer._id, {$set: {value: TIMER_INIT}});
serverTimer = TIMER_INIT;
}
}
});
Meteor.publish("timer", function() {
return Timer.find();
});
Meteor.publish("userData", function() {
return Meteor.users.find({}, {fields: {date: 1}});
});
Meteor.users.allow({
update: function(userId, doc) {
return Timer.findOne().value > 0 && userId === doc._id;
}
});
Meteor.publish("clicks", function() {
return Clicks.find();
});
Clicks.allow({
insert: function(userId, doc) {
if (serverTimer > 0 && userId === doc.userId) {
var timer = Timer.findOne();
Timer.update(timer._id, {$set: {value: TIMER_INIT}});
serverTimer = TIMER_INIT;
return true;
}
return false;
}
})
}