-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLaborTracker.js
More file actions
544 lines (449 loc) · 18 KB
/
LaborTracker.js
File metadata and controls
544 lines (449 loc) · 18 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
var READONLY = false
// How much labor you generate per minute
var LABORGENRATE = 2;
if (READONLY){
// This is just for setting up a display-only example of this app
Characters = new Meteor.Collection(null)
Timers = new Meteor.Collection(null)
} else {
Characters = new Meteor.Collection("characters");
Timers = new Meteor.Collection("timers");
}
DayStrings = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;1
}
return str;
}
function formattime(hour, minutes) {
if (hour >= 12) {
ampm = 'pm'
} else {
ampm = 'am'
}
hour = hour % 12;
if (hour == 0) {
hour = 12;
}
return hour+":"+pad(minutes,2)+ampm;
}
function parsetimerlength(timerstring) {
var totaltime = 0;
// Find days
var re = /(\d+) ?(?:days|day|d)/g;
var matches = re.exec(timerstring);
if(matches) {
totaltime += (Number(matches[1]) * 86400);
}
// Find hours
var re = /(\d+) ?(?:hours|hour|h|hr|hrs)/g;
var matches = re.exec(timerstring);
if(matches) {
totaltime += (Number(matches[1]) * 3600);
}
// Find minutes
var re = /(\d+) ?(?:minutes|minute|min|m)/g;
var matches = re.exec(timerstring);
if(matches) {
totaltime += (Number(matches[1]) * 60);
}
// Find seconds
var re = /(\d+) ?(?:seconds|second|secs|sec|s)/g;
var matches = re.exec(timerstring);
if(matches) {
totaltime += Number(matches[1]);
}
return totaltime;
}
function maxtime(now, max) {
return Date.now() + (max - now) * 1000 * 60 / LABORGENRATE;
}
if (Meteor.isClient) {
var highestMaxLabor = function () {
var highestchar = Characters.findOne({owner: Session.get('sessionid')}, {sort: {labormax: -1}})
if (highestchar)
return highestchar.labormax;
else
return 1000;
};
Session.set('sessionid', location.search);
// When editing a character name, ID of the character
Session.set('editing_charactername', null);
// When editing current labor, ID of the character
Session.set('editing_characterlabor', null);
// When editing current labormax, ID of the character
Session.set('editing_characterlabormax', null);
/* New version
Deps.autorun(function () {
Meteor.subscribe("characters");
});
*/
Meteor.autosubscribe(function () {
Meteor.subscribe('characters', {owner: Session.get('sessionid')});
Meteor.subscribe('timers', {owner: Session.get('sessionid')});
});
if (READONLY) {
// Super duper quickl and dirty hack for creating a read-only version of the app to show as an example from GitHub
newchar = Characters.insert({name: 'OverloadUT', labor: 4000, labormax: 4320, labortimestamp: Date.now(), maxtime: maxtime(4320, 4000), owner: Session.get('sessionid')});
newchar = Characters.insert({name: 'DiscoC', labor: 2400, labormax: 1650, labortimestamp: Date.now(), maxtime: maxtime(1650, 2400), owner: Session.get('sessionid')});
newchar = Characters.insert({name: 'RoughRaptors', labor: 1250, labormax: 5000, labortimestamp: Date.now(), maxtime: maxtime(5000, 1250), owner: Session.get('sessionid')});
var length = 3600
var percent = 0.75
Timers.insert({name: 'Strawberries', starttime: Date.now() - percent * length * 1000, timerlength: length, owner: Session.get('sessionid'), endtime: Date.now() + (1-percent) * length * 1000});
var length = 3600 * 72
var percent = 0.10
Timers.insert({name: 'Pine trees', starttime: Date.now() - percent * length * 1000, timerlength: length, owner: Session.get('sessionid'), endtime: Date.now() + (1-percent) * length * 1000});
var length = 3600 * 18
var percent = 0.90
Timers.insert({name: 'Cows', starttime: Date.now() - percent * length * 1000, timerlength: length, owner: Session.get('sessionid'), endtime: Date.now() + (1-percent) * length * 1000});
var length = 3600 * 24 * 7
var percent = 0.5
Timers.insert({name: 'Pay Taxes', starttime: Date.now() - percent * length * 1000, timerlength: length, owner: Session.get('sessionid'), endtime: Date.now() + (1-percent) * length * 1000});
var length = 3600 * 7
var percent = 1.5
Timers.insert({name: 'Goats', starttime: Date.now() - percent * length * 1000, timerlength: length, owner: Session.get('sessionid'), endtime: Date.now() + (1-percent) * length * 1000});
}
//{//////// Helpers for in-place editing //////////
// Returns an event map that handles the "escape" and "return" keys and
// "blur" events on a text input (given by selector) and interprets them
// as "ok" or "cancel".
var okCancelEvents = function (selector, callbacks) {
var ok = callbacks.ok || function () {};
var cancel = callbacks.cancel || function () {};
var events = {};
events['keyup '+selector+', keydown '+selector+', focusout '+selector] =
function (evt) {
if (evt.type === "keydown" && evt.which === 27) {
// escape = cancel
cancel.call(this, evt);
} else if (evt.type === "keyup" && evt.which === 13 ||
evt.type === "focusout") {
// blur/return/enter = ok/submit if non-empty
var value = String(evt.target.value || "");
if (value)
ok.call(this, value, evt);
else
cancel.call(this, evt);
}
};
return events;
};
var activateInput = function (input) {
input.focus();
input.select();
};
//} END IN-PLACE EDITING HELPERS
//{///////// NEED SESSION PAGE ///////////
Template.needsession.events({
'click input.sessionnamesubmit' : function () {
window.location = Meteor.absoluteUrl('?' + $("#sessionname").val())
}
});
//} END NEED SESSION PAGE
//{//////////// MAIN TEMPLATE //////////////
Template.main.need_session = function () {
return Session.get('sessionid') == "" || Session.get('sessionid') == "?undefined" || Session.get('sessionid') == "?";
};
Template.main.is_readonly = function () {
return READONLY;
};
Template.main.show_timers = function() {
//TODO: Make a way for the user to pick which modules are visible
return true;
}
//} END MAIN TEMPLATE
//{//////////// TIMERS LIST ///////////////////
// When editing timer name, ID of the timer
Session.set('editing_timername', null);
// When editing timer length, ID of the timer
Session.set('editing_timertimeleft', null);
// Preference to hide seconds from timers
Session.setDefault('pref_show_seconds', false);
var timersTimerDep = new Deps.Dependency;
var timersTimerUpdate = function () {
timersTimerDep.changed();
};
Meteor.setInterval(timersTimerUpdate, 1000);
Template.timers.timers = function () {
return Timers.find({owner: Session.get('sessionid')}, {sort: {endtime: 1}});
};
Template.timers.events({
'click a.add' : function () {
var newtimer = Timers.insert({name: 'Timer', starttime: Date.now(), timerlength: 3600, owner: Session.get('sessionid'), endtime: Date.now() + 3600 * 1000});
Session.set('editing_timername', newtimer);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput($("#timer-name-input"));
},
'click th.timeleft' : function () {
Session.set('pref_show_seconds', !Session.get('pref_show_seconds'));
}
});
//} END TIMERS LIST
//{////////// EACH TIMER //////////////
Template.timer.displaytimeleft = function() {
return this.timerlength;
};
Template.timer.timerdone = function() {
return (this.endtime <= Date.now())
};
var format_time_left = function(totalsecondsleft) {
var daysleft = Math.floor(totalsecondsleft / 60 / 60 / 24);
var hoursleft = Math.floor(totalsecondsleft / 60 / 60 % 24);
var minutesleft = Math.floor(totalsecondsleft / 60 % 60);
var secondsleft = Math.floor(totalsecondsleft % 60);
var timestring = '';
if(totalsecondsleft > 86400) {
timestring += daysleft + 'd ';
}
if(totalsecondsleft > 3600) {
timestring += hoursleft + 'h ';
}
if(totalsecondsleft > 60) {
timestring += minutesleft + 'm ';
}
if (Session.get('pref_show_seconds')) {
timestring += secondsleft + 's';
}
return timestring;
}
Template.timer.timeleft = function() {
timersTimerDep.depend();
var totalsecondsleft = Math.abs((this.timerlength*1000 - (Date.now() - this.starttime)) / 1000);
return format_time_left(totalsecondsleft);
}
Template.timer.timeleftinput = function() {
if(this.endtime <= Date.now()) {
// Timer finished, so show the original timer length
return format_time_left(this.timerlength);
} else {
// Timer not finished, so show the current time left
var totalsecondsleft = Math.abs((this.timerlength*1000 - (Date.now() - this.starttime)) / 1000);
return format_time_left(totalsecondsleft);
}
}
Template.timer.endtimestring = function() {
timersTimerDep.depend();
var date = new Date(this.endtime);
var hour = date.getHours();
var minutes = date.getMinutes();
var day = date.getDay();
var hoursleft = Math.floor(Math.abs((this.endtime - Date.now()) / 1000 / 60 / 60))
var minutesleft = Math.floor(Math.abs((this.endtime - Date.now()) / 1000 / 60 % 60))
return DayStrings[day] + ' ' + formattime(hour,minutes);
}
Template.timer.percentage = function() {
timersTimerDep.depend();
var end = this.starttime + this.timerlength * 1000;
var now = Date.now();
return Math.min(100,Math.floor((now - this.starttime) / (end - this.starttime) * 100));
}
Template.timer.events({
'click a.remove' : function () {
Timers.remove({_id: this._id});
},
'click div.name': function (evt, tmpl) { // start editing list name
Session.set('editing_timername', this._id);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput(tmpl.find("#timer-name-input"));
},
'click div.timeleft': function (evt, tmpl) { // start editing list name
Session.set('editing_timertimeleft', this._id);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput(tmpl.find("#timer-timeleft-input"));
}
});
Template.timer.events(okCancelEvents(
'#timer-name-input', {
ok: function (value) {
Timers.update(this._id, {$set: {name: value}});
Session.set('editing_timername', null);
},
cancel: function () {
Session.set('editing_timername', null);
}
}
));
Template.timer.events(okCancelEvents(
'#timer-timeleft-input', {
ok: function (value) {
var timerlength = parsetimerlength(value);
Timers.update(this._id, {$set: {timerlength: timerlength, starttime: Date.now(), endtime: Date.now() + timerlength * 1000}});
Session.set('editing_timertimeleft', null);
},
cancel: function () {
Session.set('editing_timertimeleft', null);
}
}
));
Template.timer.editingname = function () {
return Session.equals('editing_timername', this._id);
};
Template.timer.editingtimeleft = function () {
return Session.equals('editing_timertimeleft', this._id);
};
//} END EACH TIMER
//{///////// CHARACTERS LIST //////////
// Preference to hide seconds from timers
Session.setDefault('pref_scale_maxlabor', true);
Session.setDefault('pref_sort_maxtime', false);
Template.characters.characters = function () {
if(Session.get('pref_sort_maxtime')) {
return Characters.find({owner: Session.get('sessionid')}, {sort: {maxtime: 1}});
} else {
return Characters.find({owner: Session.get('sessionid')}, {});
}
};
Template.characters.events({
'click a.add' : function () {
var newmaxtime = Date.now() + (this.labormax - this.labor) * 1000 * 60 / LABORGENRATE;
var newchar = Characters.insert({name: 'NewCharacter', labor: 50, labormax: 1000, labortimestamp: Date.now(), maxtime: newmaxtime, owner: Session.get('sessionid')});
Session.set('editing_charactername', newchar);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput($("#character-name-input"));
},
'click th.labor' : function () {
Session.set('pref_scale_maxlabor', !Session.get('pref_scale_maxlabor'))
},
'click th.maxtime' : function () {
Session.set('pref_sort_maxtime', !Session.get('pref_sort_maxtime'))
}
});
//}
//{///////// EACH CHARACTER ///////////
var timerDep = new Deps.Dependency;
var timerUpdate = function () {
timerDep.changed();
};
Meteor.setInterval(timerUpdate, 60000 / LABORGENRATE);
Template.character.currentlabor = function() {
timerDep.depend();
var currentlabor = Math.floor((Date.now() - this.labortimestamp) / 1000 / 60 * LABORGENRATE) + this.labor;
return currentlabor;
};
Template.character.currentlaborcapped = function() {
timerDep.depend();
return Math.min(this.labormax,Math.floor((Date.now() - this.labortimestamp) / 1000 / 60 * LABORGENRATE) + this.labor);
};
// Returns the percentage of max labor, in integer format (50 for 50%)
Template.character.percentage = function() {
timerDep.depend();
var currentlabor = Math.floor((Date.now() - this.labortimestamp) / 1000 / 60 * LABORGENRATE) + this.labor;
return Math.min(100,Math.floor(currentlabor / this.labormax * 100))
};
// Returns the percentage of this character's max labor compared to,
// the character with the MOST max labor. Integer format (50 for 50%)
Template.character.percentagemax = function() {
if(Session.get('pref_scale_maxlabor')) {
return Math.min(100,Math.floor(this.labormax / highestMaxLabor() * 100))
} else {
return 100;
}
};
Template.character.laborcapped = function() {
timerDep.depend();
var currentlabor = Math.floor((Date.now() - this.labortimestamp) / 1000 / 60 * LABORGENRATE) + this.labor;
return (currentlabor >= this.labormax);
}
Template.character.laborwaste = function() {
timerDep.depend();
var currentlabor = Math.floor((Date.now() - this.labortimestamp) / 1000 / 60 * LABORGENRATE) + this.labor;
return Math.max(0,currentlabor - this.labormax);
}
Template.character.maxtimestring = function() {
var maxtimestamp = this.labortimestamp + (this.labormax - this.labor) * 1000 * 60 / LABORGENRATE;
var date = new Date(maxtimestamp);
var hour = date.getHours();
var minutes = date.getMinutes();
var day = date.getDay();
var hoursleft = Math.floor(Math.abs((maxtimestamp - Date.now()) / 1000 / 60 / 60))
var minutesleft = Math.floor(Math.abs((maxtimestamp - Date.now()) / 1000 / 60 % 60))
return DayStrings[day] + " " + formattime(hour,minutes)+" ("+hoursleft+"h "+minutesleft+"m)";
};
Template.character.events({
'click a.remove' : function () {
Characters.remove({_id: this._id});
},
'click div.name': function (evt, tmpl) { // start editing list name
Session.set('editing_charactername', this._id);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput(tmpl.find("#character-name-input"));
},
'click div.labor': function (evt, tmpl) { // start editing list name
Session.set('editing_characterlabor', this._id);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput(tmpl.find("#character-labor-input"));
},
'click div.labormax': function (evt, tmpl) { // start editing list name
Session.set('editing_characterlabormax', this._id);
Meteor.flush(); // force DOM redraw, so we can focus the edit field
activateInput(tmpl.find("#character-labormax-input"));
}
});
Template.character.events(okCancelEvents(
'#character-name-input', {
ok: function (value) {
Characters.update(this._id, {$set: {name: value}});
Session.set('editing_charactername', null);
},
cancel: function () {
Session.set('editing_charactername', null);
}
}
));
Template.character.events(okCancelEvents(
'#character-labor-input', {
ok: function (value) {
var newmaxtime = Date.now() + (this.labormax - Number(value)) * 1000 * 60 / LABORGENRATE;
Characters.update(this._id, {$set: {labor: Number(value), labortimestamp: Date.now(), maxtime: newmaxtime}});
Session.set('editing_characterlabor', null);
},
cancel: function () {
Session.set('editing_characterlabor', null);
}
}
));
Template.character.events(okCancelEvents(
'#character-labormax-input', {
ok: function (value) {
var newmaxtime = Date.now() + (Number(value) - this.labor) * 1000 * 60 / LABORGENRATE;
Characters.update(this._id, {$set: {labormax: Number(value), maxtime: newmaxtime}});
Session.set('editing_characterlabormax', null);
},
cancel: function () {
Session.set('editing_characterlabormax', null);
}
}
));
Template.character.editingname = function () {
return Session.equals('editing_charactername', this._id);
};
Template.character.editinglabor = function () {
return Session.equals('editing_characterlabor', this._id);
};
Template.character.editinglabormax = function () {
return Session.equals('editing_characterlabormax', this._id);
};
//} END EACH CHARACTER
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
// Upgrade database from earlier version
Timers.find({}, {}).fetch().forEach(function(timer) {
if (timer.endtime == null) {
console.log('Updating timer ' + timer._id);
Timers.update(timer._id, {$set: {endtime: timer.starttime + timer.timerlength * 1000}});
}
});
// Upgrade database from earlier version
Characters.find({}, {}).fetch().forEach(function(character) {
if (character.maxtime == null) {
console.log('Updating character ' + character._id);
var newmaxtime = character.labortimestamp + (character.labormax - character.labor) * 1000 * 60 / LABORGENRATE;
Characters.update(character._id, {$set: {maxtime: newmaxtime}});
}
});
});
}