This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer.js
1189 lines (1018 loc) · 51.9 KB
/
timer.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The majority of the code (not related to splitting functions or anything else
// that looks like spaghetti code) was originally written by Ian "bmn" Bennett,
// from http://www.w00ty.com/sda/timer/
//
//
// Shoutouts to him, I probably couldn't have built everything from scratch
// - iotku
// I want these available to every function now that I'm seperating things
var splitsObject = Object.create(null);
var splitsList = Object.create(null);
var splitID = 0;
function option () {
// Options that should be user modifyable sometime in the future
self = this;
this.useWebsockets = false; // Use Websocket interface?
this.startDelayAmount = 0; // How many *seconds* of delay (TODO: Verify :: Is this taken into account everywhere?)
this.maxSplits = 10; // Max splits to display at once :: Not yet implemented.
// Main timer
this.playColor1 = "#00FF68";
this.playColor2 = "#00A541";
this.pauseColor1 = "white";
this.pauseColor2 = "gray";
this.ahaedColor1 = "#00B3FF";
this.ahaedColor2 = "#00A1E6";
this.behindColor1 = "#FF0000";
this.behindColor2 = "#E30000";
// Segment Colors
this.goldSplitColor = "gold";
this.aheadSplitColor = "lime";
this.aheadSplitBehindTotalSegment = "#CC0000"; // Eww, find better name?
this.behindSplitColor = "red";
this.behindSplitAheadTotalSegmentColor = "#00CC00";// Eww, find better name?
}
function editor () {
// Split Editor
this.genEditorSplits = function () {
console.log(splitsObject);
t.timerReset();
t.editorEnabled = true;
var addtime = 0;
// Hide regular splits
document.getElementById("splits").style.display = "none";
document.getElementById("prevsplit").style.color = "white";
document.getElementById("prevsplit").textContent = "Edit Mode.";
document.getElementById("splits-game-name").innerHTML = '<input id="splits-game-input" value="' + splitsObject.info[0] + '"/>';
document.getElementById("splits-goal-name").innerHTML = '<input id="splits-goal-input" value="' + splitsObject.info[1] + '"/>';
document.getElementById("attempt-counter").innerHTML = '<input id="attempt-counter-input" value="' + splitsObject.info[2] + '"/>';
// Show editor
document.getElementById("splits-editor").style.display = "block";
document.getElementById("splits-editor-table").style.display = "table";
document.getElementById("splits-editor-table").innerHTML = ""; // Make sure table is empty
document.getElementById("splits-editor-table").innerHTML = '<input disabled value="Names" /><input disabled value="Best" /><input disabled value="Seg" /><br>';
// Generate input boxes for each split
for (var step = 1; step <= t.totalSplits; step++) {
var container = document.createElement('span');
container.id = "editor-row" + step;
container.innerHTML = '<input id="editor-splitname' + step + '" type="text" value="' + splitsObject[step][0] + '" />' + '<input id="editor-bestsegment' + step + '" type="text" value="' + t.realTime(splitsObject[step][2], true) + '">' + '<input id="editor-difference' + step + '" type="text" value="' + t.realTime(splitsObject[step][1], true) + '"><br/><p class="editor-split-controls"><a class="btn-addSplit" onclick="editor.addSplit(' +step+ ')">+</a> / <a class="btn-removeSplit" onclick="editor.removeSplit(' + step + ')">-</a> / <a class="btn-moveSplitUp" onclick="editor.moveSplitUp(' + step + ')">^</a> / <a class="btn-moveSplitDown" onclick="editor.moveSplitDown(' + step + ')">V</a></p>';
document.getElementById("splits-editor-table").appendChild(container);
}
document.getElementById("editor-controls").innerHTML = '<input type="button" value="Add split" onclick="editor.addSplit()"/> <input type="button" value="Del split" onclick="editor.removeSplit()"/><input type="button" value="Save" onclick="editor.saveNewSplits()"/> <input type="button" value="Exit" onclick="t.genSplits()"/>';
};
this.saveNewSplits = function () {
var splitNames, enteredTime, bestsegTime;
console.log(t.totalSplits)
for (var step = 1; step <= t.totalSplits; step++) {
splitNames = document.getElementById("editor-splitname" + step).value;
enteredTime = document.getElementById("editor-difference" + step).value;
bestsegTime = document.getElementById("editor-bestsegment" + step).value;
splitsObject[step][0] = splitNames;
splitsObject[step][1] = t.parseTime(enteredTime);
splitsObject[step][2] = t.parseTime(bestsegTime);
}
splitsObject.info[0] = document.getElementById("splits-game-input").value;
splitsObject.info[1] = document.getElementById("splits-goal-input").value;
splitsObject.info[2] = document.getElementById("attempt-counter-input").value;
localStorage["PB" + t.splitID] = JSON.stringify(splitsObject);
splitsList[splitID] = [splitsObject.info[0], splitsObject.info[1]];
localStorage.splitsListTracker = JSON.stringify(splitsList);
t.genSplits();
};
this.addSplit = function (split) {
// Either addsplit or removesplit leaves a mess behind in the DOM when used
// not a huge issue (I hope), but worth investigating later
if (t.editorEnabled === false) {return false;}
var replaceMe = split + 1 || t.totalSplits + 1;
var tmpSplitObject = Object.create(null);
tmpSplitObject.info = splitsObject.info;
for (i = 1; i <= replaceMe - 1; i++){
tmpSplitObject[i] = splitsObject[i];
}
tmpSplitObject[replaceMe -1] = splitsObject[replaceMe -1];
tmpSplitObject[replaceMe] = [replaceMe,0,0,0];
for (i = replaceMe; i <= t.totalSplits; i++){
tmpSplitObject[i + 1] = splitsObject[i];
}
// There's probably a much better way to change the IDs, but for now this works
// and doesn't seem to be very slow
for (i = split + 1; i <= t.totalSplits; i++) {
document.getElementById("editor-row" + i).id = "editor-row-tmp" + (i + 1);
document.getElementById("editor-splitname" + i).id = "editor-splitname-tmp" + (i + 1);
document.getElementById("editor-difference" + i).id = "editor-difference-tmp" + (i + 1);
document.getElementById("editor-bestsegment" + i).id = "editor-bestsegment-tmp" + (i + 1);
}
var splitRow = document.getElementById("editor-row-tmp" + (replaceMe + 1));
var container = document.createElement("span");
container.id = "editor-row" + replaceMe;
container.innerHTML = '<input id="editor-splitname' + replaceMe + '" type="text" value="' + replaceMe + '"><input id="editor-bestsegment' + replaceMe + '" type="text" value="00:00.00"><input id="editor-difference' + replaceMe + '" type="text" value="00:00.00"><br/><p class="editor-split-controls"><a class="btn-addSplit" onclick="editor.addSplit(' + replaceMe + ')">+</a> / <a class="btn-removeSplit" onclick="editor.removeSplit(' + replaceMe + ')">-</a> / <a class="btn-moveSplitUp" onclick="editor.moveSplitUp(' + replaceMe + ')">^</a> / <a class="btn-moveSplitDown" onclick="editor.moveSplitDown(' + replaceMe + ')">V</a></p>';
document.getElementById("splits-editor-table").insertBefore(container, splitRow);
this.editorUpdateSplitButtons(); // make sure split buttons are current, even though it seemed to work fine without this.
t.totalSplits++;
for (i = split + 2; i <= t.totalSplits; i++) {
document.getElementById("editor-row-tmp" + i).id = ("editor-row" + i);
document.getElementById("editor-splitname-tmp" + i).id = ("editor-splitname" + i);
document.getElementById("editor-difference-tmp" + i).id = ("editor-difference" + i);
document.getElementById("editor-bestsegment-tmp" + i).id = ("editor-bestsegment" + i);
}
splitsObject = tmpSplitObject;
if (!split) {
// Scroll to bottom automatically
var objDiv = document.getElementById("splits-editor");
objDiv.scrollTop = objDiv.scrollHeight;
}
};
this.removeSplit = function (split) {
if (this.editorEnabled === false) {return false;}
if (t.totalSplits === 1) {return false;} // Can't have 0 splits
var removedRow;
splitToDelete = split || t.totalSplits;
if (!split || split == t.totalSplits) {
delete splitsObject[splitToDelete];
removedRow = document.getElementById("editor-row" + splitToDelete);
removedRow.parentNode.removeChild(removedRow);
t.totalSplits = t.totalSplits - 1;
} else {
removedRow = document.getElementById("editor-row" + splitToDelete);
removedRow.parentNode.removeChild(removedRow);
t.totalSplits = t.totalSplits - 1;
delete splitsObject[split];
splitsObject[split] = splitsObject[split+1];
if (split == t.totalSplits){
delete splitsObject[split + 1];
}
for (i = split + 1; i <= t.totalSplits; i++) {
splitsObject[i] = splitsObject[i + 1];
delete splitsObject[i + 1];
}
// There's probably a much better way to change the IDs, but for now this works
// and doesn't seem to be very slow
for (i = split; i <= t.totalSplits; i++) {
document.getElementById("editor-row" + (i+1)).id = "editor-row-tmp" + (i);
document.getElementById("editor-splitname" + (i+1)).id = "editor-splitname-tmp" + (i);
document.getElementById("editor-difference" + (i+1)).id = "editor-difference-tmp" + (i);
document.getElementById("editor-bestsegment" + (i+1)).id = "editor-bestsegment-tmp" + (i);
}
for (i = split; i <= t.totalSplits; i++) {
document.getElementById("editor-row-tmp" + i).id = "editor-row" + i;
document.getElementById("editor-splitname-tmp" + i).id = "editor-splitname" + i;
document.getElementById("editor-difference-tmp" + i).id = "editor-difference" + i;
document.getElementById("editor-bestsegment-tmp" + i).id = "editor-bestsegment" + i;
}
}
this.editorUpdateSplitButtons();
};
this.moveSplitUp = function (split) {
// Functional, but could probbaly be refactored to be more readable
// Also uses a different order for swapping than moveSplitdown, which
// could be confusing
if (split == 1) {return false;}
var swap1, swap2;
swap1 = splitsObject[split];
swap2 = splitsObject[split-1];
splitsObject[split-1] = swap1;
splitsObject[split] = swap2;
// document.getElementById("editor-row" + split).insert(document.getElementById("editor-row" + (split -1))); // Use insertBefore?
var div1 = document.getElementById("editor-row" + split);
var div2 = document.getElementById("editor-row" + (split -1));
var container = document.getElementById("editor-row" + split);
document.getElementById("splits-editor-table").insertBefore(container, div2);
div1.id = "editor-row" + (split - 1);
div2.id = "editor-row" + (split);
var splitname1 = document.getElementById("editor-splitname" + (split - 1));
var difference1 = document.getElementById("editor-difference" + (split - 1));
var bestsegment1 = document.getElementById("editor-bestsegment" + (split - 1));
var splitname2 = document.getElementById("editor-splitname" + split);
var difference2 = document.getElementById("editor-difference" + split);
var bestsegment2 = document.getElementById("editor-bestsegment" + split);
splitname1.id = "editor-splitname" + split;
splitname2.id = "editor-splitname" + (split - 1);
difference1.id = "editor-difference" + split;
difference2.id = "editor-difference" + (split - 1);
bestsegment1.id = "editor-bestsegment" + split;
bestsegment2.id = "editor-bestsegment" + (split - 1);
this.editorUpdateSplitButtons();
};
this.moveSplitDown = function (split) {
// Functional, but could probbaly be refactored to be more readable
if (split == t.totalSplits) { return false;}
var swap1, swap2;
swap1 = splitsObject[split];
swap2 = splitsObject[split+1];
splitsObject[split+1] = swap1;
splitsObject[split] = swap2;
var div1 = document.getElementById("editor-row" + split);
var div2 = document.getElementById("editor-row" + (split + 1));
var container = document.getElementById("editor-row" + (split +1));
document.getElementById("splits-editor-table").insertBefore(container, div1);
div1.id = "editor-row" + (split + 1);
div2.id = "editor-row" + (split);
var splitname1 = document.getElementById("editor-splitname" + (split + 1));
var difference1 = document.getElementById("editor-difference" + (split + 1));
var bestsegment1 = document.getElementById("editor-bestsegment" + (split + 1));
var splitname2 = document.getElementById("editor-splitname" + split);
var difference2 = document.getElementById("editor-difference" + split);
var bestsegment2 = document.getElementById("editor-bestsegment" + split);
splitname1.id = "editor-splitname" + split;
splitname2.id = "editor-splitname" + (split + 1);
difference1.id = "editor-difference" + split;
difference2.id = "editor-difference" + (split + 1);
bestsegment1.id = "editor-bestsegment" + split;
bestsegment2.id = "editor-bestsegment" + (split + 1);
this.editorUpdateSplitButtons();
};
this.editorUpdateSplitButtons = function () {
// TODO: Replace innerHTML with something else, it's pretty slow.
var changeButtons = document.getElementsByClassName('editor-split-controls');
for (var i = 0; i <= changeButtons.length - 1; i++) {
changeButtons[i].innerHTML = '<a class="btn-addSplit" onclick="editor.addSplit(' + (i + 1) + ')">+</a> / <a class="btn-removeSplit" onclick="editor.removeSplit(' + (i + 1) + ')">-</a> / <a class="btn-moveSplitUp" onclick="editor.moveSplitUp(' + (i + 1) + ')">^</a> / <a class="btn-moveSplitDown" onclick="editor.moveSplitDown(' + (i + 1) + ')">V</a>';
}
};
}
function debugMsg(text) {
var currentTime = new Date();
var container = document.createElement('span');
container.innerHTML = currentTime.getHours() + ":" + t.pad(currentTime.getMinutes(), 2) + ":" + t.pad(currentTime.getSeconds(), 2) + ": " + text + '<br>';
document.getElementById("debug-output").appendChild(container);
// Scroll to bottom automatically?
var objDiv = document.getElementById("debug-output");
objDiv.scrollTop = objDiv.scrollHeight;
}
function webSocket(f){
var websocketURL = 'ws://localhost:8080/';
ws = new WebSocket(websocketURL);
ws.onopen = function() {
debugMsg("Connected to " + websocketURL);
document.getElementById("websock-status").textContent = "Connected to " + websocketURL;
};
this.closeSocket = function () {
// Should autorespawn
ws.close();
};
ws.onmessage = function(event) {
switch (event.data) {
case "start": t.split(); break;
case "reset": t.reset(); break;
case "unsplit": t.unsplit(); break;
case "skipsplit": t.skipSplit(); break;
default: t.split(t.parseTime(event.data)); break;
}
debugMsg("Recived: " + event.data);
};
ws.onerror = function (error) {
debugMsg('WebSocket Error ' + error);
};
ws.onclose = function(){
//try to reconnect in 5 seconds
debugMsg("Connection Lost!");
document.getElementById("websock-status").textContent = "Not Connected.";
setTimeout(function(){webSocket();}, 5000);};
var self = this,
d = d || {}; // I really don't know about this.
}
function GameTimer(d) {
/* User configurable settings */
/* Timer variables (do not change unless you're sure) */
this.currentSplit = 1; // Initialize at 1st split
this.goldCounter = 0; // How Many gold splits?
this.splitID = 0; // Initialize, should be set by split selection function
this.startTime = 0; // Keep track of inital start time for unsplit. (TODO: change to somethign more meaningful, and modify comment to mention that it saves the offset, including after pausing)
if (localStorage.splitsListTracker) {
splitsList = JSON.parse(localStorage.splitsListTracker);
}
/* [0]Split Name, [1]PBsplit, [2]Best Split, [3]Current Split */
var defaultSplitsObject = Object.create(null); // Load this if, no other splits
defaultSplitsObject = {
"info": ["Game Name", "Goal", 0],
"1": ["1", 0, 0, 0],
};
this.start = function (start) {
pref = performance.now();
start = start || 0;
this.timer = {
start: pref + (start * 1000),
now: 0,
realtime: 0
};
this.startTime = this.timer.start;
this.updateElements();
this.clearTimeout();
this.setTimeout();
this.setState("play");
this.updateAttemptCounter();
return this.timer.start;
};
this.update = function (no_timeout, clear_timeout) {
var t = this.timer;
t.now = performance.now();
t.realtime = t.now - t.start;
this.updateElements();
if (clear_timeout === true) {
this.clearTimeout();
}
if (no_timeout !== true) {
this.setTimeout();
}
return this.realTime(t.realtime);
};
this.pause = function () {
if (this.disableControls === true) {return false;}
if (this.currently === 'play') {
this.setState("pause");
this.update(true, true);
} else if (this.currently === 'stop') {
this.start();
return false;
} else if (this.currently === 'done') {
return false;
} else {
this.setState("play");
var timeOffset = this.now() - this.timer.realtime;
this.timer.start = timeOffset;
this.update();
this.startTime = timeOffset; // For unsplitting after timer stops
}
};
this.reset = function () {
if (this.disableControls === true) {return false;}
if (t.currently === 'stop') {
t.start();
return false;
} else if (t.currently === 'play') {
t.pause();
}
this.currently = 'reset';
if (this.goldCounter > 0) {if (window.confirm("Would you like to save your gold splits?")){this.saveGoldSplit();}} // Wow
this.currentSplit = 1;
t.split(); /* What does this even do? */
this.genSplits(); /* reset splits */
};
this.split = function (splitTime) {
var actualTime = this.timer.realtime;
if (this.disableControls === true) {return false;}
splittime = splitTime || actualTime;
if (this.currently === 'pause') {
this.pause(); // Unpause on split, if paused
return false;
} else if (this.currently === 'play') {
this.update(true, true);
this.setTimeout(0);
this.updateSplit(splittime);
} else if (this.currentSplit === this.totalSplits && this.totalSplits != 1) {
this.reset();
} else if (this.timer.start === 0) {
return this.start(option.startDelayAmount); // Startup delay in seconds
} else {
this.timerReset();
this.genSplits();
}
};
this.updateSplit = function (splittime) {
var timerText = document.getElementById("split" + this.currentSplit),
currentSegment = splittime - this.getSegmentTime(),
bestSegment = splitsObject[this.currentSplit][2],
prevSplit = document.getElementById("prevsplit"),
prevText = document.getElementById("prevtext");
// Double Tap Prevention
if (currentSegment < 300) { return false; }
if (this.totalSplits === this.currentSplit) {
// Stop timer and match time with last split
this.pause();
this.currently = 'done';
document.getElementById("timer_realtime").textContent = this.realTime(splittime);
}
// Add Current Segment to splitsObject
splitsObject[this.currentSplit][3] = currentSegment;
// Calculate Total Time Elapsed
if (splitsObject[this.currentSplit][1] !== 0) {
timerText.textContent = this.realTime(splittime - this.getTotalTime());
// Calculate difference between currentSegment and PBsegment
prevSplit.textContent = this.realTime(currentSegment - splitsObject[this.currentSplit][1]);
} else {
timerText.textContent = "-";
prevSplit.textContent = "-";
}
// Set finished split time *bold* / Set color for segment and prevsplit
document.getElementById("difference" + this.currentSplit).textContent = this.realTime(this.getSegmentTime());
document.getElementById("difference" + this.currentSplit).style.fontWeight = "bolder";
this.setSegmentColor(currentSegment);
this.resizeSplitColumn();
// Increment gold counter to know how many golds there are
if (currentSegment < bestSegment || bestSegment === 0) { // If better than best segment
this.goldCounter++;
}
// Setup for next split
if (this.totalSplits !== this.currentSplit) {
prevText.textContent = 'Prev. Segment:';
this.currentSplit = this.currentSplit + 1;
document.getElementById('row' + (this.currentSplit)).className += " active-split";
document.getElementById('row' + (this.currentSplit - 1)).className = " ";
// advance visible splits when truncated
if (this.currentSplit > 5 && this.totalSplits > 10) {// Don't start until half way thru visible splits
if ((this.totalSplits - this.currentSplit) >= 5) {
document.getElementById("row" + (this.currentSplit + 4)).style.display = "table-row";
document.getElementById("row" + (this.currentSplit - 5)).style.display = "none";
}
}
} else {
document.getElementById("row" + this.currentSplit).className = " ";
// (Total Time of PB > Total of Current Segs || No Total, so new splits || Last Split is empty, so we assume that the run is new, even if behind)
if (this.getTotalTime() > this.getSegmentTime() || this.getTotalTime() === 0 || splitsObject[this.totalSplits][1] === 0) { /*Dude nice*/
prevText.innerHTML = '<b>New Record</b>';
this.setStyle("ahead");
this.saveGoldSplit();
this.goldCounter = 0; // Make sure reset doesn't cause double gold save
this.saveSplits();
} else {
prevText.innerHTML = '<b>No Record</b>';
this.setStyle("behind");
this.saveGoldSplit();
this.goldCounter = 0; // Make sure reset doesn't cause double gold save
}
}
};
this.unsplit = function () {
if (this.disableControls === true || this.currentSplit === 1) { return false; }
document.getElementById("difference" + this.currentSplit).style.fontWeight = "Normal";
if (this.currently === "done" && this.currentSplit === this.totalSplits) {
this.setState("play");
this.timer.start = this.startTime;
this.update();
// Decrement gold split counter if unsplitting a gold split
if (splitsObject[this.currentSplit][3] < splitsObject[this.currentSplit][2]) {
goldCounter--;
}
// Reset current (Still last) split
splitsObject[this.currentSplit][3] = 0;
document.getElementById('row' + this.currentSplit).className += " active-split";
document.getElementById("split" + this.currentSplit).textContent = ' ';
document.getElementById("difference" + this.currentSplit).textContent = this.realTime(this.getTotalTime());
return false;
}
// Decrement gold split counter if unsplitting a gold split
if (splitsObject[this.currentSplit - 1][3] < splitsObject[this.currentSplit -1][2]) {
goldCounter--;
}
// Reset Previous split before switching to it
splitsObject[this.currentSplit - 1][3] = 0;
document.getElementById('row' + (this.currentSplit)).className = " ";
document.getElementById('row' + (this.currentSplit - 1)).className += " active-split";
this.currentSplit--;
document.getElementById("difference" + this.currentSplit).style.fontWeight = "Normal";
document.getElementById("split" + this.currentSplit).textContent = ' ';
if (splitsObject[this.currentSplit][1] === 0) {
document.getElementById("difference" + this.currentSplit).textContent = '-';
} else {
document.getElementById("difference" + this.currentSplit).textContent = this.realTime(this.getTotalTime());
}
if (this.currentSplit >= 5 && this.totalSplits > option.maxSplits && (this.totalSplits - this.currentSplit) > 5) {// Don't start until half way thru visible splits, currently hard-coded to 10 splits
document.getElementById("row" + (this.currentSplit - 4)).style.display = "table-row";
document.getElementById("row" + (this.currentSplit + 5)).style.display = "none";
}
};
this.skipSplit = function () {
if (this.currentSplit === this.totalSplits || this.currently === "stop") {return false;} // can't skip last split
splitsObject[this.currentSplit][3] = 0;
document.getElementById("difference" + this.currentSplit).textContent = '-';
document.getElementById("split" + this.currentSplit).style.color = "white";
document.getElementById("split" + this.currentSplit).textContent = '-';
if (this.currentSplit == 1) { document.getElementById("prevtext").textContent = "Prev. Segment:"; }
document.getElementById("prevsplit").style.color = "white";
document.getElementById("prevsplit").textContent = '-';
this.currentSplit++;
document.getElementById('row' + (this.currentSplit)).className += " active-split";
document.getElementById('row' + (this.currentSplit - 1)).className = " ";
// advance visible splits when truncated
if (this.currentSplit > 5 && this.totalSplits > 10) {// Don't start until half way thru visible splits
if ((this.totalSplits - this.currentSplit) >= 5) {
document.getElementById("row" + (this.currentSplit + 4)).style.display = "table-row";
document.getElementById("row" + (this.currentSplit - 5)).style.display = "none";
}
}
};
this.getTotalTime = function () {
var totalTime = 0;
for (var step = 0; step !== this.currentSplit; step++) {
totalTime = splitsObject[step + 1][1] + totalTime;
}
return totalTime;
};
this.getSegmentTime = function () {
var segmentTime = 0;
for (var step = 0; step !== this.currentSplit; step++) {
segmentTime = splitsObject[step + 1][3] + segmentTime;
}
return segmentTime;
};
this.genSplits = function () {
// Disable while generating splits (even though it should be fast.)
this.disableControls = true;
// Show controls after hiding them for the split menu
document.getElementById("controls").style.display = "block";
// It's fairly safe to assume if this function is running the editor
// has either been closed, or never opened.
document.getElementById("splits-editor").style.display = "none";
document.getElementById("splits-editor-table").style.display = "none";
document.getElementById("splits-table").style.display = "table"; // Make sure table is empty
document.getElementById("splits").style.display = "block"; // Make sure table is empty
this.editorEnabled = false;
this.goldCounter = 0;
this.currentSplit = 1;
// How many splits do we have? Don't count info.
this.totalSplits = Object.keys(splitsObject).length - 1;
// Cleanup my mess hopefully
for (var step = 1; step <= this.totalSplits; step++) {
splitsObject[step][4] = 0;
}
document.getElementById("splits-game-name").textContent = splitsObject.info[0];
document.getElementById("splits-goal-name").textContent = splitsObject.info[1];
document.getElementById("attempt-counter").textContent = splitsObject.info[2];
document.getElementById("splits-table").innerHTML = ""; // Make sure table is empty
// Make sure editor controls are gone (bad place for this)
document.getElementById("editor-controls").innerHTML = "";
// Do split magic
var addtime = 0;
for (step = 1; step <= this.totalSplits; step++) {
splitsObject[this.currentSplit][3] = 0; // Reset current segments
addtime = splitsObject[this.currentSplit][1] + addtime; // Add each segment together to generate split times
/* variables should be used properly here. (confusing) */
// Generate table (Now formatted DIVs) based on splitsObject
var container = document.createElement('span');
container.id = "row" + this.currentSplit;
container.innerHTML = '<div id="splitname' + this.currentSplit + '"></div>' + '<div id="split' + this.currentSplit + '"></div>' + '<div id="difference' + this.currentSplit + '"></div>';
document.getElementById("splits-table").appendChild(container);
// Insert split names
document.getElementById("splitname" + this.currentSplit).innerHTML = splitsObject[this.currentSplit][0];
// Empty string as placeholder for split times
document.getElementById("split" + this.currentSplit).innerHTML = " ";
// Add total time upto current split
if (splitsObject[this.currentSplit][1] === 0){
document.getElementById("difference" + this.currentSplit).textContent = '-';
} else {
document.getElementById("difference" + this.currentSplit).textContent = t.realTime(addtime);
}
this.currentSplit++;
}
this.currentSplit = 1;
this.resizeSplits();
this.setState("stop");
this.disableControls = false;
};
this.setState = function (state) {
this.currently = state;
this.setStyle(state);
};
this.startSplits = function () {
//Mom's spaghetti
// // ID (starts at 0), [0] Game Name, [1] Goal, [2?] Total Time [3?] Primary Splits
// splitsList = {
// "0": ["Super Mario 64", "16 Star"],
// "1": ["Super Mario 64", "70 Star"],
// };
if (Object.keys(splitsList).length === 0) {
if (localStorage.PersonalBest) {
splitsObject = JSON.parse(localStorage.PersonalBest); // Migrate from previous saved splits
} else {
splitsObject = defaultSplitsObject; // Splits Skeleton
}
this.splitID = 0; // Ensure starts at first id 0
splitsList = Object.create(null);
splitsList[0] = [splitsObject.info[0], splitsObject.info[1]];
localStorage.splitsListTracker = JSON.stringify(splitsList);
localStorage["PB" + this.splitID] = JSON.stringify(splitsObject);
this.makeDefaultSplits(0);
} else {
if (localStorage.splitsDefault && localStorage.splitsDefault in splitsList) {
this.selectPB(localStorage.splitsDefault);
return false;
} else if (localStorage.splitsDefault) { // Remove splits default if it no longer exists
localStorage.removeItem("splitsDefault");
}
this.splitSelector();
}
};
this.splitSelector = function () {
if (this.currently === 'play' || this.currently === 'pause' || this.editorEnabled === true) {return false;}
this.setState("menu");
this.disableControls = true; // Disable hotkeys while on menu, gensplits re-enables
document.getElementById("split-selector").innerHTML = "";
document.getElementById("splits-table").innerHTML = "";
document.getElementById("controls").style.display = "none";
document.getElementById("split-selector").style.visibility = "visible";
document.getElementById("container").style.visibility = "hidden";
document.getElementById("split-selector").innerHTML = "<h1>Select Splits</h1>";
var pbid; // Keep this outside for loop so it stays for the rest of the function
for (pbid in splitsList) { // Gets numbers hopefully
splitsObject = JSON.parse(localStorage["PB" + pbid]);
document.getElementById("split-selector").innerHTML += '<span class="delete"><a href="#" onclick="t.deleteSplitFile(' + pbid + ')">X</a><div class="slide"><p>Delete File</p></div></span><span class="defaultSplit"><a href="#" onclick="t.makeDefaultSplits(' + pbid + ')">✓</a><div class="slide"><p>Make Default</p></div></span><ul onclick="t.selectPB(' + pbid + ')"><li>' + splitsObject.info[0] + '</li><li>' + splitsObject.info[1] + '</li></ul>';
}
// Now that the loop has run, pbid should be the last object in the element supposibly.
var nextpbid = parseInt(pbid, 10) + 1;
document.getElementById("split-selector").innerHTML += '<input type="button" value="New Splits Entry" onclick="t.newSplitFile(' + nextpbid + ')"></input>';
};
this.newSplitFile = function (lastfile) {
splitsObject = defaultSplitsObject; // Splits Skeleton
this.splitID = lastfile; // Start after last id
splitsList[this.splitID] = [splitsObject.info[0], splitsObject.info[1]];
localStorage.splitsListTracker = JSON.stringify(splitsList);
localStorage["PB" + this.splitID] = JSON.stringify(splitsObject);
document.getElementById("container").style.visibility = "visible";
document.getElementById("split-selector").innerHTML = "";
document.getElementById("split-selector").style.visibility = "hidden";
t.genSplits();
t.disableControls = true;
t.editorEnabled = true;
editor.genEditorSplits();
};
this.wsplitExport = function () {
var splitInfo = "Title=" + splitsObject.info[0] + " :: " + splitsObject.info[1] + "\r\n" + "Attempts="+ splitsObject.info[2] + "\r\n" + "Offset=0\r\nSize=152,25" + "\r\n";
var splitSplits = "",
splitIcons = "",
addtime = 0;
for (var step = 1; step <= this.totalSplits; step++) {
addtime = splitsObject[step][1] + addtime;
splitSplits += splitsObject[step][0] + ",0," + (addtime / 1000) + "," + (splitsObject[step][1] / 1000) + "\r\n";
splitIcons += '"",';
}
var wspltFile = splitInfo + splitSplits + "Icons=" + splitIcons.slice(0, - 1);
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'application/octet-stream'});
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
saveAs(data, splitsObject.info[0] + " - " + splitsObject.info[1] + ".wsplit");
};
makeTextFile(wspltFile); // How does any of this crap work?
};
this.selectPB = function (pbid) {
this.splitID = pbid;
splitsObject = JSON.parse(localStorage["PB" + pbid]);
document.getElementById("container").style.visibility = "visible";
document.getElementById("split-selector").innerHTML = "";
document.getElementById("split-selector").style.visibility = "hidden";
this.genSplits();
this.timerReset();
};
this.makeDefaultSplits = function (pbid) {
localStorage.splitsDefault = pbid;
this.selectPB(pbid);
};
this.deleteSplitFile = function (id) {
if (confirm("WARNING!:This will irrevocably delete the selected splits, Are you SURE you want to continue?")) {/* Cancel */
} else {
/* OK */
return false;
}
localStorage.removeItem("PB" + id);
delete splitsList[id];
localStorage.splitsListTracker = JSON.stringify(splitsList);
this.startSplits();
};
this.updateAttemptCounter = function () {
splitsObject.info[2]++;
document.getElementById("attempt-counter").textContent = splitsObject.info[2];
localStorage["PB" + this.splitID] = JSON.stringify(splitsObject);
};
this.saveGoldSplit = function () {
for (var step = 1; step <= this.totalSplits; step++) {
if (splitsObject[step][2] > splitsObject[step][3] || splitsObject[step][2] === 0) {
if(splitsObject[step][3] !== 0) {splitsObject[step][2] = splitsObject[step][3];} // Should find a better way
}
}
localStorage["PB" + this.splitID] = JSON.stringify(splitsObject); // Don't break everything, please. Thanks.
};
this.saveSplits = function () {
if (this.disableControls === true || this.currently === 'play') {return false;}
this.disableControls = true;
if (confirm("Would you like to save?")) {/* Cancel */
} else {
/* OK */
this.disableControls = "false";
return false;
}
for (var step = 1; step <= this.totalSplits; step++) {
splitsObject[step][1] = splitsObject[step][3];
if (splitsObject[step][2] === 0) {
splitsObject[step][2] = splitsObject[step][3];
}
if (splitsObject[step][1] === 0) {
splitsObject[step][1] = splitsObject[step][3];
}
}
localStorage["PB" + this.splitID] = JSON.stringify(splitsObject);
this.disableControls = "false";
};
this.loadSplits = function () {
if (this.disableControls === true || this.currently === 'play') {return false;}
splitsObject = JSON.parse(localStorage["PB" + this.splitID]);
this.currentSplit = 1;
this.genSplits();
this.timerReset();
};
// Useful after stopping timer, makes sure things reset completely
this.timerReset = function () {
this.timer = { start: 0, now: 0, realtime: 0 };
this.updateElements(); /* Updates the now 0 timer values. */
};
// Styling Functions
this.cssChange = function (selector, property, value) { // http://stackoverflow.com/a/11081100
for (var i=0; i<document.styleSheets.length;i++) { // Loop through all styles
try { document.styleSheets[i].insertRule(selector+ ' {'+property+':'+value+'}', document.styleSheets[i].cssRules.length);
} catch(err) {try { document.styleSheets[i].addRule(selector, property+':'+value);} catch(err) {}} // IE
}
};
this.setStyle = function (currentState) {
var timer = document.getElementById("timer_realtime");
if (currentState === 'stop') {
for (var step = 1; step <= this.totalSplits; step++) {
var difference = document.getElementById("difference" + step),
row = document.getElementById("row" + step);
difference.style.color = "white";
difference.style.fontWeight = "Normal";
}
document.getElementById("prevsplit").style.color = "White";
document.getElementById("prevsplit").textContent = "Ready";
document.getElementById("prevtext").textContent = "";
this.cssChange('#timer .stop1', 'stop-color', 'white');
this.cssChange('#timer .stop2', 'stop-color', 'gray');
} else if (currentState === 'play') {
if (this.currentSplit === 1) {
document.getElementById("row1").className += " active-split";
document.getElementById("prevsplit").textContent = "...";
}
this.cssChange('#timer .stop1', 'stop-color', option.playColor1);
this.cssChange('#timer .stop2', 'stop-color', option.playColor2);
} else if (currentState === 'pause') {
this.cssChange('#timer .stop1', 'stop-color', option.pauseColor1);
this.cssChange('#timer .stop2', 'stop-color', option.pauseColor2);
} else if (currentState === 'ahead') {
this.cssChange('#timer .stop1', 'stop-color', option.ahaedColor1);
this.cssChange('#timer .stop2', 'stop-color', option.ahaedColor2);
} else if (currentState === 'behind') {
this.cssChange('#timer .stop1', 'stop-color', option.behindColor1);
this.cssChange('#timer .stop2', 'stop-color', option.behindColor2);
}
};
this.setSegmentColor = function (currentSegment) {
var timerText = document.getElementById("split" + this.currentSplit),
prevSplit = document.getElementById("prevsplit"),
pbSegment = splitsObject[this.currentSplit][1],
bestSegment = splitsObject[this.currentSplit][2];
if (pbSegment === 0 && bestSegment !== 0) {
return false;
}
if (currentSegment < bestSegment || bestSegment === 0) {
prevSplit.style.color = option.goldSplitColor;
timerText.style.color = option.goldSplitColor;
if (this.getTotalTime() < this.getSegmentTime() && pbSegment !== 0) {
timerText.textContent = '+' + timerText.textContent;
}
return false; // Exit without checking anything else, gold is gold everywhere!
} else if (currentSegment <= pbSegment && this.getSegmentTime() <= this.getTotalTime()) {
// Ahead Split + Ahead Total Time
prevSplit.style.color = option.aheadSplitColor;
timerText.style.color = option.aheadSplitColor;
} else if (currentSegment <= pbSegment && this.getSegmentTime() >= this.getTotalTime()) {
// Ahead Split, But not ahead total time
prevSplit.style.color = option.aheadSplitColor;
timerText.style.color = option.aheadSplitBehindTotalSegment;
timerText.textContent = '+' + timerText.textContent;
} else if (currentSegment >= pbSegment && this.getSegmentTime() >= this.getTotalTime()) {
// Behind Split, and behind total time.
prevSplit.style.color = option.behindSplitColor;
timerText.style.color = option.behindSplitColor;
timerText.textContent = '+' + timerText.textContent;
prevSplit.textContent = '+' + prevSplit.textContent;
} else if (currentSegment >= pbSegment && this.getSegmentTime() <= this.getTotalTime()) {
// Behind Split, but ahead total time
prevSplit.style.color = option.behindSplitColor;
timerText.style.color = option.behindSplitAheadTotalSegmentColor;
prevSplit.textContent = '+' + prevSplit.textContent;
} else { // Hopefully, all our bases are covered....
// Make obvious something went wrong somehow.
prevSplit.style.color = "yellow";
timerText.style.color = "yellow";
prevSplit.textContent = '?' + prevSplit.textContent;
timerText.textContent = '?' + timerText.textContent;
}
};
// Timing stuff
this.realTime = function (time, isEditor) {
var h = Math.floor(time / 3600000),
m = Math.abs(Math.floor((time / 60000) % 60)),
s = Math.abs(Math.floor((time / 1000) % 60)),
msd = this.ms[(h > 0) ? 1 : 0],
ms = Math.abs(Math.floor((time % 1000) / (Math.pow(10, (3 - msd)))));
if (time < 0) {
ms -= 1;
s -= 1;
m -= 1;
// Adding += might be a HUGE mistake here,
// but it seems to solve an issue with seemingly random -1 values...
h += 1;
}
// I think msd was supposed to avoid this mess somehow (a value to set to show how much to truicate?)
if (isEditor === true) {
humanTime = ((h !== 0) ? h + ':' : '') + this.pad(m, 2) + ':' + this.pad(s, 2) + ((msd) ? '.' + this.pad(ms, msd) : '');
return humanTime;
}
var humanTime;
if (h === 0 && m === 0) {
humanTime = this.pad(s, 1) + ((msd) ? '.' + this.pad(ms, msd) : '').slice(0, -1);
} else if (h === 0 && m < 10) {
humanTime = ((h !== 0) ? h + ':' : '') + this.pad(m, 1) + ':' + this.pad(s, 2); // + ((msd) ? '.' + this.pad(ms, msd) : '');
} else {
humanTime = ((h !== 0) ? h + ':' : '') + this.pad(m, 2) + ':' + this.pad(s, 2); // + ((msd) ? '.' + this.pad(ms, msd) : '');
}
if (time >= 0) { // I hate everything about this if statement.
return humanTime;
} else if ( time < 0 && h === 0){
return '-' + humanTime;
} else if (h !== 0) { // Hour adds the negative sign itself apparently...
return humanTime;
} // If this fails I'm pretty screwed.
};
this.timeConvert = function (hours, minutes, seconds) {
// Convert from syntax like "00:00:00.00" to ms for use internally
var h, min, s, ms, time;
h = Math.floor(hours * 3600000);
min = Math.abs(Math.floor((minutes * 60000)));
s = Math.abs(Math.floor((seconds * 1000)));
time = (h + min + s);
return time;
};
this.parseTime = function (input) {
// Often fails silently (== 0) if invalid input
var output, count;
output = input.split(":");
count = 0;
for (var k in output) {if (output.hasOwnProperty(k)) {++count;}}
if (count == 3) {
return this.timeConvert(output[0], output[1], output[2]);
} else if (count == 2) {
return this.timeConvert(0, output[0], output[1]);
} else if (count == 1) {
return this.timeConvert(0,0,output[0]);
} else {