-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
380 lines (347 loc) · 11.9 KB
/
script.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
if (localStorage.rubiks == undefined) {
var startDown = false;
var startUp = false;
var inspectionStarted = false;
var solveStarted = false;
var currentScreen = 1;
var prevScreen;
var screenBeforeStats;
var backgroundNum = 0;
var backgroundRGBs = ["229, 57, 53", "244, 81, 30", "57, 73, 171", "67, 160, 71", "255, 179, 0", "117, 117, 117"];
var backgroundNames = ["Red", "Orange", "Blue", "Green", "Yellow", "Grey"];
var backgroundRGB = "229, 57, 53";
var backgroundName = "Red";
var solves = [];
var inspectionStartOn = 15;
var inspection;
var inspectionInterval;
var inspectionTimer;
var solve;
var solveMinutes;
var solveSeconds;
var solveHundredths;
var solveSecondsRaw;
var solveHundredthsRaw;
var solveInterval;
var solveTimer;
var solvesListLoop;
var chartLoop;
var chartArray;
var startDate;
updateStorage();
localStorage.rubiks = true;
}
else {
//Normal init with localStorage vars missing
var startDown = false;
var startUp = false;
var inspectionStarted = false;
var solveStarted = false;
var currentScreen = 1;
var prevScreen;
var screenBeforeStats;
var backgroundRGBs = ["229, 57, 53", "244, 81, 30", "57, 73, 171", "67, 160, 71", "255, 179, 0", "117, 117, 117"];
var backgroundNames = ["Red", "Orange", "Blue", "Green", "Yellow", "Grey"];
var backgroundRGB;
var backgroundName;
var inspection;
var inspectionInterval;
var inspectionTimer;
var solve;
var solveMinutes;
var solveSeconds;
var solveHundredths;
var solveSecondsRaw;
var solveHundredthsRaw;
var solveInterval;
var solveTimer;
var solvesListLoop;
var chartLoop;
var chartArray;
var startDate;
//localStorage vars
if (typeof(Storage) !== "undefined") {
var inspectionStartOn = Number(localStorage.rubiksInspectionStartOn);
document.getElementById("inspection").innerHTML = inspectionStartOn.toString();
var backgroundNum = Number(localStorage.rubiksBackgroundNum);
backgroundChange();
var solves = localStorage.rubiksSolves.split(',');
}
else {
var cookiesList = document.cookie.split("|");
var inspectionStartOn = Number(cookiesList[1]);
document.getElementById("inspection").innerHTML = inspectionStartOn.toString();
var backgroundNum = Number(cookiesList[2]);
backgroundChange();
var solves = cookiesList[3].split(',');
}
}
document.getElementById("scramble").innerHTML = scramblers["333"].getRandomScramble().scramble_string;
Array.min = function( array ){
return Math.min.apply( Math, array );
};
function showScreen(num) {
document.getElementById("screen1").style.display = "none";
document.getElementById("screen2").style.display = "none";
document.getElementById("screen3").style.display = "none";
document.getElementById("screen4").style.display = "none";
document.getElementById("screen5").style.display = "none";
document.getElementById("screen6").style.display = "none";
document.getElementById("screen" + num).style.display = "block";
if (num === 1 || num === 2) {
document.getElementById("scramble").style = "-webkit-filter: opacity(100); filter: opacity(100);";
}
else {
document.getElementById("scramble").style = "-webkit-filter: opacity(0); filter: opacity(0);";
}
prevScreen = currentScreen;
currentScreen = num;
}
function startInspection() {
inspectionInterval = 1; // 1 Second
inspection = inspection - 1;
document.getElementById("numbers").innerHTML = inspection.toString();
if (inspection === 0) {
solve = 0;
solveStarted = true;
startSolve();
clearTimeout(inspectionTimer);
}
inspectionTimer = setTimeout(startInspection, inspectionInterval*1000);
}
function stopInspection() {
clearTimeout(inspectionTimer);
}
function startSolve() {
clearTimeout(inspectionTimer);
solveInterval = 10; // 10 thousandths of a second (1 hundredth)
startDate = new Date().getTime();
updateClockMidSolve();
}
function updateClockMidSolve() {
solve = ((new Date().getTime()) - startDate)/solveInterval;
document.getElementById("numbers").innerHTML = clockify(solve);
solveTimer = setTimeout(updateClockMidSolve, solveInterval);
}
function stopSolve() {
clearTimeout(solveTimer);
solve = ((new Date().getTime()) - startDate)/solveInterval;
solves.push(solve);
updateStorage();
document.getElementById("screen2sub").style.display = "block";
document.getElementById("screen2sub").innerHTML = "Ready";
document.getElementById("numbers").innerHTML = clockify(solves[solves.length - 1]);
newSolve();
}
function clockify(num) {
var minRaw = num / 6000;
var min = Math.floor(minRaw);
var secRaw = (minRaw - min) * 60;
var sec = Math.floor(secRaw);
var hundRaw = (secRaw - sec) * 100;
var hund = Math.floor(hundRaw);
if (min.toString().length < 2) {
min = "0" + min;
}
if (sec.toString().length < 2) {
sec = "0" + sec;
}
if (hund.toString().length < 2) {
hund = "0" + hund;
}
return(min + ":" + sec + "." + hund);
}
function spaceDown() {
if (currentScreen === 1 || currentScreen === 2) {
if (event.keyCode === 32) {
if (startDown === false) {
showScreen(2);
document.getElementById("screen2sub").style.display = "none";
document.getElementById("numbers").innerHTML = "Ready";
startDown = true;
}
if (solveStarted === true) {
stopSolve();
}
if (inspectionStarted === true && solveStarted === false) {
solve = 0;
solveStarted = true;
startSolve();
clearTimeout(inspectionTimer);
}
}
}
}
function spaceUp() {
if (currentScreen === 1 || currentScreen === 2) {
if (event.keyCode === 32) {
if (startDown === true && inspectionStarted === false) {
inspection = inspectionStartOn + 1;
inspectionStarted = true;
startInspection();
}
}
}
}
function gearClicked() {
if (document.getElementById("screen4").style.display === "none" && document.getElementById("screen5").style.display === "none" && document.getElementById("screen6").style.display === "none") {
if (document.getElementById("screen3").style.display === "none") {
document.getElementById("gear").src = "https://booligoosh.github.io/rubiks-time/left.svg";
showScreen(3);
}
else {
document.getElementById("gear").src = "https://booligoosh.github.io/rubiks-time/gear.svg";
showScreen(prevScreen);
}
}
}
function statsClicked() {
if (document.getElementById("screen3").style.display === "none") {
if (document.getElementById("screen4").style.display === "none" && document.getElementById("screen5").style.display === "none" && document.getElementById("screen6").style.display === "none") {
document.getElementById("stats").src = "https://booligoosh.github.io/rubiks-time/left.svg";
screenBeforeStats = currentScreen;
showScreen(4);
}
else if (document.getElementById("screen4").style.display === "none") {
showScreen(4);
}
else {
document.getElementById("stats").src = "https://booligoosh.github.io/rubiks-time/stats.svg";
showScreen(screenBeforeStats);
}
}
}
function solvesList() {
document.getElementById("solveslist").innerHTML = "";
for (var i = solves.length - 1; i >= 0 ; i--) {
addToSolvesDisplay(clockify(solves[i]));
}
showScreen(5);
}
function moreStats() {
chartArray = [['Solve number', 'Time (seconds)']];
chartLoop = 0;
while (chartLoop < solves.length) {
chartArray.push(["Solve " + (chartLoop + 1) + " - " + clockify(Number(solves[chartLoop])), Number(solves[chartLoop]) / 100]);
//OLD CHART FORMAT: chartArray.push(["Solve " + (chartLoop + 1), Number(solves[chartLoop]) / 100]);
chartLoop = chartLoop + 1;
}
showScreen(6);
document.getElementById("average").innerHTML = "Average - " + clockify(averageOfArray(solves));
if (solves.length >= 5) {
var averagesOfFive = averageOfFivesOfArray(solves);
document.getElementById("currentaverageof5").innerHTML = "Current average of 5 - " + clockify(averagesOfFive[averagesOfFive.length - 1]);
document.getElementById("bestaverageof5").innerHTML = "Best average of 5 - " + clockify(Array.min(averagesOfFive));
}
if (solves.length >= 12) {
var averagesOfTwelve = averageOfTwelvesOfArray(solves);
document.getElementById("currentaverageof12").innerHTML = "Current average of 12 - " + clockify(averagesOfTwelve[averagesOfTwelve.length - 1]);
document.getElementById("bestaverageof12").innerHTML = "Best average of 12 - " + clockify(Array.min(averagesOfTwelve));
}
drawChart();
}
function averageOfArray(array) {
var total = 0;
var averageLoop = 0;
while (averageLoop < array.length) {
total = total + Number(array[averageLoop]);
averageLoop = averageLoop + 1;
}
return(total / array.length);
}
function averageOfFivesOfArray(array) {
var newArray = [];
var fiveLoop = 0;
var averagesOfFive = [];
for (var i = 0; i < array.length - 4; i++) {
for (var l = 0; l < 5; l++) {
newArray.push(Number(array[i + l]));
}
averagesOfFive.push(averageOfArray(i));
}
return(averagesOfFive);
}
function addToSolvesDisplay(data) {
var itemNum = solves.length - document.getElementsByTagName("h6").length - 1;
var h6 = document.createElement("h6");
var att1 = document.createAttribute("id");
att1.value = "solve" + itemNum;
h6.setAttributeNode(att1);
var att2 = document.createAttribute("onclick");
att2.value = "deleteSolve(" + itemNum + ")";
h6.setAttributeNode(att2);
var t = document.createTextNode(data);
h6.appendChild(t);
document.getElementById("solveslist").appendChild(h6);
document.getElementById("solvescount").innerHTML = solves.length + " solves so far.";
}
function iPlus() {
inspectionStartOn = inspectionStartOn + 1;
document.getElementById("inspection").innerHTML = inspectionStartOn.toString();
updateStorage();
}
function iMinus() {
if (inspectionStartOn != 0) {
inspectionStartOn = inspectionStartOn - 1;
document.getElementById("inspection").innerHTML = inspectionStartOn.toString();
updateStorage();
}
}
function cLeft() {
backgroundNum = backgroundNum - 1;
if (backgroundNum === -1) {
backgroundNum = 5;
}
backgroundChange();
updateStorage();
}
function cRight() {
backgroundNum = backgroundNum + 1;
if (backgroundNum === 6) {
backgroundNum = 0;
}
backgroundChange();
updateStorage();
}
function backgroundChange() {
backgroundRGB = backgroundRGBs[backgroundNum];
backgroundName = backgroundNames[backgroundNum];
document.getElementById("body").style = "background-image: linear-gradient( rgba(" + backgroundRGB + ", 0.75), rgba(" + backgroundRGB + ", 0.75) ), url('https://booligoosh.github.io/rubiks-time/beach.jpg');";
document.getElementById("color").innerHTML = backgroundName;
}
function deleteSolve(index) {
var r = confirm("You have chosen to delete solve " + Number(index + 1) + " - " + clockify(solves[index]) + "\nAre you sure you want to delete it?");
if (r == true) {
solves.splice(index, 1);
updateStorage();
solvesList();
} else {
}
}
function updateStorage() {
if (typeof(Storage) !== "undefined") {
localStorage.rubiksInspectionStartOn = inspectionStartOn;
localStorage.rubiksBackgroundNum = backgroundNum;
localStorage.rubiksSolves = solves.join();
} else {
document.cookie = "cookies= |" + inspectionStartOn + "|" + backgroundNum + "|" + solves.join();
}
}
function newSolve() {
startDown = false;
startUp = false;
inspectionStarted = false;
solveStarted = false;
document.getElementById("scramble").innerHTML = scramblers["333"].getRandomScramble().scramble_string;
}
//GOOGLE CHARTS SCRIPTS
function drawChart() {
var data = google.visualization.arrayToDataTable(chartArray);
var options = {
title: 'Solves over time',
//curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('graph'));
chart.draw(data, options);
}