-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathui.js
More file actions
709 lines (669 loc) · 34.1 KB
/
Copy pathui.js
File metadata and controls
709 lines (669 loc) · 34.1 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
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
var Ui = (function () {
var timer;
function checkProgress() {
if (timer) window.clearTimeout(timer);
var timeout = Settings.values.timeout * 1000;
if (timeout <= 10000) {
timer = window.setTimeout(function () { if (lastStatus == "progress") setStatus("incorrect"); }, timeout);
}
}
var lastStatus = "init";
var completed = false;
var partial = null;
var initiallyPartial = false;
var scramble = null;
var algIndex = 0;
var incorrect = new Audio("incorrect.wav");
incorrect.load();
var correct = new Audio("correct.wav");
correct.load();
var recognitionStart = null;
var executionStart = null;
var executionStop = null;
function getAlgStats() {
var stats = Settings.values.algStats[algId];
if (!stats) stats = Settings.values.algStats[algId] = { reco: [], exec: [], solves: {total: 0, correct: 0} };
return stats;
}
function updateStats() {
function renderSpan(span) {
span = Math.trunc(span);
var ms = span % 1000;
span = (span - ms) / 1000;
var s = span % 60;
span = (span - s) / 60;
var m = span % 60;
span = (span - m) / 60;
var h = span % 60;
return (h > 0 ? h + ':' : '') +
(m > 0 ? (h > 0 && m < 10 ? '0' : '') + m + ':' : '') +
(m > 0 && s < 10 ? '0' : '') + s + '.' +
(ms < 100 ? '0' : '') + (ms < 10 ? '0' : '') + ms;
}
function addStatAndAverage(stat, val) {
stat.push(val);
while (stat.length > 5) stat.shift();
var sum = 0;
var count = 0;
for (var i = 0; i < stat.length; i++) {
var s = stat[i];
if (s < 10000) { // ignore 10sec+
sum += s;
count++;
}
}
if (count <= 1) return undefined;
return sum / count;
}
function renderAvg(show, span) {
return show ? " (" + Localization.getString("meanTime") + " " + renderSpan(span) + ")" : "";
}
var now = new Date();
var reco = recognitionStart ? (executionStart || now) - recognitionStart : 0;
var exec = executionStart ? (executionStop || now) - executionStart : 0;
var stats = getAlgStats();
var avgReco = addStatAndAverage(stats.reco, reco);
var avgExec = addStatAndAverage(stats.exec, exec);
Settings.save();
var showAvg = avgReco && avgExec;
var successRate = (stats.solves.correct / stats.solves.total * 100).toFixed(2);
var htm = '<table style="margin-left:auto;margin-right:auto">';
htm += '<tr><td align="right">' + Localization.getString("recognitionTime") + ':</td><td>' + renderSpan(reco) + renderAvg(showAvg, avgReco) + '</td></tr>';
htm += '<tr><td align="right">' + Localization.getString("executionTime") + ':</td><td>' + renderSpan(exec) + renderAvg(showAvg, avgExec) + '</td></tr>';
htm += '<tr><td align="right"></td><td style="font-weight: bold; border-top: 1px solid white">' + renderSpan(reco + exec) + renderAvg(showAvg, avgReco + avgExec) + '</td></tr>';
htm += '<tr><td align="right">' + Localization.getString("successRate") + ':</td><td align="left">' + successRate + '% (' + stats.solves.correct + '/' + stats.solves.total + ')' + '</td></tr>';
htm += '</table>';
document.getElementById("message").innerHTML = htm;
}
function updateSolveCount(isCorrect) {
var stats = getAlgStats();
var solves = stats.solves;
if (!solves) solves = stats.solves = {total: 0, correct: 0};
solves.total += 1;
if (isCorrect) solves.correct += 1;
Settings.save();
}
function startRecognition() {
recognitionStart = new Date();
executionStart = null;
executionStop = null;
}
function startOrContinueExecution() {
if (!executionStart) executionStart = new Date();
}
function stopExecution() {
executionStop = new Date();
}
function setStatus(status) {
lastStatus = status;
switch (status) {
case "correct":
stopExecution();
updateSolveCount(true);
updateStats();
correct.play();
document.getElementById("diagram").style.backgroundColor = "green";
document.getElementById("retry").disabled = false;
document.getElementById("next").disabled = false;
completed = true;
partial = null;
initiallyPartial = false;
$("#popup").popup("close");
break;
case "partial":
document.getElementById("status").innerHTML = setName;
document.getElementById("diagram").style.backgroundColor = "goldenrod";
document.getElementById("retry").disabled = false;
document.getElementById("next").disabled = false;
completed = false;
break;
case "incorrect":
stopExecution();
updateSolveCount(false);
incorrect.play();
document.getElementById("diagram").style.backgroundColor = "darkred";
document.getElementById("retry").disabled = false;
document.getElementById("next").disabled = false;
completed = true;
partial = null;
initiallyPartial = false;
break;
case "skip":
stopExecution();
incorrect.play();
document.getElementById("diagram").style.backgroundColor = "gray";
document.getElementById("retry").disabled = false;
document.getElementById("next").disabled = false;
completed = true;
partial = null;
initiallyPartial = false;
break;
case "progress":
startOrContinueExecution();
document.getElementById("diagram").style.backgroundColor = (partial ? "goldenrod" : "#444");
document.getElementById("retry").disabled = false;
document.getElementById("next").disabled = false;
completed = false;
checkProgress();
break;
case "init":
startRecognition();
document.getElementById("status").innerHTML = setName;
document.getElementById("diagram").style.backgroundColor = "transparent";
document.getElementById("retry").disabled = true;
document.getElementById("next").disabled = false;
completed = false;
partial = null;
initiallyPartial = verifyPartial(instance);
document.getElementById("message").innerHTML = '<br /><br /><a href="#popup" data-rel="popup" data-transition="pop" style="font-size: small; margin-left: 0.5em; padding: 1em">' + Localization.getString("hint") + '</a>';
break;
case "error":
stopExecution();
document.getElementById("status").innerHTML = setName;
document.getElementById("diagram").style.backgroundColor = "transparent";
document.getElementById("retry").disabled = true;
document.getElementById("next").disabled = true;
completed = false;
document.getElementById("message").innerText = ""; // remove "Hint" link
break;
}
}
function verifyPartial(result) {
// check partial match (corners oriented, but not permuted)
var pat = Algs.kindToParams(kind).verify.partial;
return pat == undefined ? false : Cube.matchPattern(pat, result);
}
function verifyComplete(result) {
function checkEO() {
function opposite(c) {
switch (c) {
case 'U': return 'D';
case 'D': return 'U';
case 'L': return 'R';
case 'R': return 'L';
case 'F': return 'B';
case 'B': return 'F';
default: throw "Unknown face: " + c;
}
}
var state = Cube.toString(result);
var u = state[0]; // Ubl face
var d = opposite(u);
for (var i = 0; i < 9; i++) { // U face
var s = state[i];
if (s != u && s != d) return false;
}
for (var i = 36; i < 45; i++) { // D face
var s = state[i];
if (s != u && s != d) return false;
}
return true;
}
function matchWithAdjustments(pat, allowRandomM, allowRandomM2, allowRandomU) {
if (Cube.matchPattern(pat, result)) return true;
if (allowRandomU) {
if (Cube.matchPattern(pat, Cube.alg("U", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("U'", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("U2", result))) return true;
}
if (allowRandomM2 || allowRandomM) {
// try flipping up/down centers (maintaining edge orientation)
if (Cube.matchPattern(pat, Cube.alg("L2 R2", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L2 R2 U", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L2 R2 U'", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L2 R2 U2", result))) return true;
}
if (allowRandomM) {
// try flipping M-slice too because some algs (with wide moves) flip this
if (Cube.matchPattern(pat, Cube.alg("L' R", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L' R U", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L' R U'", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L' R U2", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L R'", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L R' U", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L R' U'", result))) return true;
if (Cube.matchPattern(pat, Cube.alg("L R' U2", result))) return true;
}
}
var verify = Algs.kindToParams(kind).verify;
if (verify.eo && !checkEO()) return false;
return (verify.solved != undefined && matchWithAdjustments(verify.solved, verify.allowRandomM, verify.allowRandomM2, verify.allowRandomU));
}
function verify(result, includePartial) {
if (verifyComplete(result)) {
setStatus("correct");
return true;
}
if (includePartial && !partial && !initiallyPartial && verifyPartial(result)) {
partial = instance; // record for retry
instance = result;
update(instance);
alg = "";
setStatus("partial");
}
return false;
}
var queued = 0; // count of queued check() calls
var lastTwist = undefined;
function twist(t) {
var now = new Date();
if (completed) {
if (lastTwist && (now - lastTwist) > 600) {
// retry/next with X/X'
if (t.endsWith("'")) retry(); else next();
}
return;
}
lastTwist = now;
function check() {
if (--queued > 0) return; // skip checking - let future queued calls get to it
if (completed) return; // skip checking if already completed
var rotations = ["", "x", "x y", "x y'", "x y2", "x z", "x z'", "x z2", "x'", "x' y", "x' y'", "x' z", "x' z'", "x2", "x2 y", "x2 y'", "x2 z", "x2 z'", "y", "y'", "y2", "z", "z'", "z2"];
for (var i = 0; i < rotations.length; i++) {
var rot = rotations[i];
// apply rotation, auf, alg, inverse rotation
var result = Cube.alg(rot, Cube.alg(alg, Cube.alg(rot, instance)), true);
if (verify(result, false)) return true;
}
// again, looking for partial matches
for (var i = 0; i < rotations.length; i++) {
var rot = rotations[i];
var result = Cube.alg(rot, Cube.alg(alg, Cube.alg(rot, instance)), true);
if (verify(result, true)) return true;
}
}
if (t == "") return;
alg += t + ' ';
var twists = alg.split(' ');
var len = twists.length;
if (len > 4) {
var a = twists[len - 2];
var b = twists[len - 3];
var c = twists[len - 4];
var d = twists[len - 5];
if (a == b && b == c && c == d) {
setStatus("skip");
window.setTimeout(function() { if (t.endsWith("'")) retry(); else next(); }, 300);
return;
}
}
var progress = "";
for (var i = 1; i < len; i++) {
progress += "• ";
}
document.getElementById("status").innerHTML = progress;
setStatus("progress");
queued++;
window.setTimeout(check, 50);
}
function showConnectButton() {
var btn = document.getElementById("btCubeConnect");
btn.disabled = false;
btn.innerText = Localization.getString("btCubeConnect");
document.getElementById("btCube").style.display = "";
document.getElementById("btCubeDisconnectSection").style.display = "none";
document.getElementById("cube").style.marginTop = "-80px";
document.getElementById("status").style.marginBottom = "80px";
document.getElementById("message").style.marginTop = "-3em";
}
function hideConnectButton() {
document.getElementById("btCube").style.display = "none";
document.getElementById("btCubeDisconnectSection").style.display = "";
document.getElementById("cube").style.marginTop = "0";
document.getElementById("status").style.marginBottom = "0";
document.getElementById("message").style.marginTop = "-1em";
}
function connected() {
hideConnectButton();
next();
}
function error(ex) {
showConnectButton();
if (!ex.message.startsWith("User cancelled")) {
document.getElementById("btError").innerText = Localization.getString("btError");
document.getElementById("btSupport").innerText = Localization.getString("btSupport");
document.getElementById("btAndroid").innerText = Localization.getString("btAndroid");
document.getElementById("btIOS").innerText = Localization.getString("btIOS");
document.getElementById("btMacOS").innerText = Localization.getString("btMacOS");
document.getElementById("btLinux").innerText = Localization.getString("btLinux");
document.getElementById("btWindows").innerText = Localization.getString("btWindows");
document.getElementById("btInfo").innerText = Localization.getString("moreInfo");
$("#bluetooth-help").popup("open");
}
}
function btCubeConnect() {
var btn = document.getElementById("btCubeConnect");
btn.disabled = true;
btn.innerText = Localization.getString("btCubeConnecting");
BtCube.connect(connected, twist, error);
}
function btCubeDisconnect() {
showConnectButton();
BtCube.disconnect();
}
var instance = Cube.solved;
var alg = "";
var algId = "";
var auf = "";
var solution = "";
var id = "";
var kind = "";
var setName = "";
var history = [];
var historyIndex = -1;
function pushHistory(status, popupHtml) {
if (historyIndex < history.length - 1) {
history = history.slice(0, historyIndex + 1);
}
history.push({
instance: instance,
scramble: scramble,
algId: algId,
auf: auf,
solution: solution,
id: id,
kind: kind,
setName: setName,
status: status,
popupHtml: popupHtml
});
historyIndex = history.length - 1;
}
function restoreHistory(entry) {
if (!entry) return;
instance = entry.instance;
scramble = entry.scramble;
algId = entry.algId;
auf = entry.auf;
solution = entry.solution;
id = entry.id;
kind = entry.kind;
setName = entry.setName;
alg = "";
partial = null;
initiallyPartial = false;
recognitionStart = null;
executionStart = null;
executionStop = null;
document.getElementById("popup").innerHTML = entry.popupHtml || "";
setStatus(entry.status || "init");
updateThumbButtons();
update(instance);
$("#popup").popup("close");
}
function currentRating() {
if (!algId) return "";
return Settings.values.algRatings[algId] || "";
}
function updateThumbButtons() {
var up = document.getElementById("thumbUp");
var down = document.getElementById("thumbDown");
if (!up || !down) return;
var rating = currentRating();
up.className = up.className.replace(/\s*is-selected\b/g, "");
down.className = down.className.replace(/\s*is-selected\b/g, "");
if (rating == "up") up.className += " is-selected";
if (rating == "down") down.className += " is-selected";
}
function persistRating(rating) {
if (!algId) return;
var curr = currentRating();
if (curr == rating) {
delete Settings.values.algRatings[algId];
} else {
Settings.values.algRatings[algId] = rating;
}
Settings.save();
updateThumbButtons();
if (typeof updateAlgRatingIndicators == "function") updateAlgRatingIndicators();
}
function update(cube) {
var simple = Settings.values.simpleDiagram;
var hide = Settings.values.llHide;
var diag = Algs.kindToParams(kind).diagram;
document.getElementById("cube").innerHTML = Display.diagram(cube, diag, id, simple, hide);
}
function lookupAlg(name) {
for (var s in Algs.sets) {
var set = Algs.sets[s];
for (var a in set.algs) {
var alg = set.algs[a];
if (name == (s + '_' + alg.id)) {
id = set.algs[a].id;
kind = set.algs[a].kind;
setName = set.name;
return { set: set, alg: set.algs[a] };
}
}
}
return undefined;
}
function simplifyAuf(alg) {
// (U) combinations
if (alg.startsWith("(U) U' ")) return alg.substr(7);
if (alg.startsWith("(U) U2 ")) return "U' " + alg.substr(7);
if (alg.startsWith("(U) U2' ")) return "U' " + alg.substr(8);
if (alg.startsWith("(U) U ")) return "U2 " + alg.substr(6);
// (U') combinations
if (alg.startsWith("(U') U ")) return alg.substr(7);
if (alg.startsWith("(U') U2 ")) return "U " + alg.substr(8);
if (alg.startsWith("(U') U2' ")) return "U " + alg.substr(9);
if (alg.startsWith("(U') U' ")) return "U2 " + alg.substr(8);
// (U2) combinations
if (alg.startsWith("(U2) U2 ")) return alg.substr(8);
if (alg.startsWith("(U2) U2' ")) return alg.substr(9);
if (alg.startsWith("(U2) U' ")) return "U " + alg.substr(8);
if (alg.startsWith("(U2) U ")) return "U' " + alg.substr(7);
return alg;
}
function next() {
function prependAuf(alg) {
var sansAuf = alg;
if (Algs.kindToParams(kind).diagram.stripAuf) {
if (alg.startsWith("U ")) sansAuf = alg.substr(2);
else if (alg.startsWith("U' ")) sansAuf = alg.substr(3);
else if (alg.startsWith("U2 ")) sansAuf = alg.substr(3);
}
var testInstance = Cube.alg(solution, Cube.solved, true); // apply random AUF + alg to solved
var sansAufSansParens = sansAuf.replace(/[\(\)]/g, '');
if (verifyComplete(Cube.alg(sansAufSansParens, testInstance))) return sansAuf;
if (verifyComplete(Cube.alg("U " + sansAufSansParens, testInstance))) return simplifyAuf("(U) " + sansAuf);
if (verifyComplete(Cube.alg("U' " + sansAufSansParens, testInstance))) return simplifyAuf("(U') " + sansAuf);
if (verifyComplete(Cube.alg("U2 " + sansAufSansParens, testInstance))) return simplifyAuf("(U2) " + sansAuf);
throw "No possible solution!";
}
function randomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function weightedRandomElement(arr, weights) {
if (arr.length !== weights.length) {
return randomElement(arr);
}
var len = arr.length;
var totalWeights = 0;
for (var i = 0; i < len; i++) {
totalWeights += weights[i];
}
// Pick a random value akin to random index.
var threshold = Math.random() * totalWeights;
var total = 0;
for (var i = 0; i < len; i++) {
total += weights[i];
if (total >= threshold) {
return arr[i];
}
}
return arr[len - 1];
}
function challenge(cas) {
var params = Algs.kindToParams(kind);
var scramble = params.scramble;
if (!cas) cas = { id: "unknown", name: "", alg: "", kind: "coll" }; // solved (default)
auf = "";
var hasSavedAuf = Object.prototype.hasOwnProperty.call(Settings.values.algAufPrefs, algId);
if (scramble.allowAuf) {
if (hasSavedAuf) {
auf = Settings.values.algAufPrefs[algId] || "";
} else if (Settings.values.randomAuf) {
auf = randomElement(["", "U ", "U' ", "U2 "]);
}
}
if (scramble.randomSingleU) {
// For L4E-style drills, prefer the user's saved AUF if they clicked one.
auf = hasSavedAuf ? (Settings.values.algAufPrefs[algId] || "") : randomElement(["U ", "U' "]);
}
solution = auf + cas.alg;
instance = Cube.solved;
// up color
var rot = [];
var upcols = Settings.values.upColors;
if (upcols.yellow) rot.push("");
if (upcols.white) rot.push("x2");
if (upcols.red) rot.push("x");
if (upcols.orange) rot.push("x'");
if (upcols.green) rot.push("z'");
if (upcols.blue) rot.push("z");
instance = Cube.random(rot, 1, instance);
if (scramble.randomOrientationAroundY) {
instance = Cube.random(["", "y", "y'", "y2"], 1, instance); // random orientation around y-axis
}
if (Settings.values.simpleDiagram) {
instance = Display.maskPieces(params.diagram.simplified, instance)
}
var upColor = Cube.faceColor("U", Cube.faces(instance));
if (scramble.randomMU) {
if (scramble.allowEOFlips) {
// scramble M-slice with U-layer
instance = Cube.random(["U", "U'", "U2", "M", "M'", "M2"], 100, instance);
} else {
// scramble M-slice with U-layer (without flips)
instance = Cube.random(["U", "U'", "U2", "M2", "R2 U R U R' U' R' U' R' U R'", "R U' R U R U R U' R' U' R2", "M2' U M2' U M' U2 M2' U2 M'", "M2' U M2' U2 M2' U M2'"], 100, instance);
}
}
// apply solution
instance = Cube.alg(solution, instance, true);
if (params.diagram.simplified.hideUCenter) {
var numColors = (upcols.yellow ? 1 : 0) + (upcols.white ? 1 : 0) + (upcols.red ? 1 : 0) + (upcols.orange ? 1 : 0) + (upcols.green ? 1 : 0) + (upcols.blue ? 1 : 0);
if (numColors > 1 || params.diagram.simplified.hideInsignificantCornerFaces) {
// adjust M-slice so center top indicates color (too confusing otherwise!)
while (Cube.faceColor("U", Cube.faces(instance)) != upColor) {
instance = Cube.alg("M", instance);
}
}
}
}
alg = "";
id = "";
kind = "pll"; // default
var status = "error";
var popupHtml = "";
while (Settings.values.algs.length > 0) {
var nextAlg;
var selectedAlgs = Settings.values.algs;
switch (Settings.values.randomOrder) {
case "random_balanced":
nextAlg = randomElement(selectedAlgs);
break;
case "random_weighted_incorrect":
// Calculate the weights based on the incorrect rate.
var algWeights = [];
for (var i = 0; i < selectedAlgs.length; i++) {
var algStat = Settings.values.algStats[selectedAlgs[i]];
var algWeight = 1;
if (algStat && algStat.solves) {
var successRate = algStat.solves.correct / (algStat.solves.total + 1); // + 1 to avoid weight being 0.
algWeight = 1 - successRate;
}
algWeights.push(algWeight);
}
nextAlg = weightedRandomElement(selectedAlgs, algWeights);
break;
case "random_off":
default:
if (algIndex >= Settings.values.algs.length) algIndex = 0;
nextAlg = Settings.values.algs[algIndex++];
}
var lookup = lookupAlg(nextAlg);
if (!lookup) {
Settings.values.algs.splice(Settings.values.algs.indexOf(nextAlg), 1); // remove
Settings.save();
continue;
}
algId = lookup.alg.kind + "_" + lookup.alg.id;
challenge(lookup.alg);
scramble = instance;
popupHtml = '<h4>' + prependAuf(lookup.alg.display) + '</h4>';
document.getElementById("popup").innerHTML = popupHtml;
status = "init";
setStatus(status);
updateThumbButtons();
break;
}
if (Settings.values.algs.length == 0) {
challenge(undefined); // solved (default)
algId = "";
scramble = instance;
popupHtml = "";
document.getElementById("popup").innerText = popupHtml;
status = "error";
setStatus(status);
updateThumbButtons();
}
pushHistory(status, popupHtml);
update(instance);
$("#popup").popup("close");
}
function previous() {
if (historyIndex <= 0) return;
historyIndex--;
restoreHistory(history[historyIndex]);
}
function toggleThumb(rating) {
if (rating != "up" && rating != "down") return;
persistRating(rating);
}
function adjustAUF(e) {
function next() {
switch (auf) {
case "": return "U' ";
case "U' ": return "U2 ";
case "U2 ": return "U ";
case "U ": return "";
}
}
var x = e.offsetX / e.currentTarget.clientWidth;
var y = e.offsetY / e.currentTarget.clientHeight;
if (x > 0.15 && x < 0.85 && y > 0.15 && y < 0.85) { // near center to avoid intercepting taps on other UI elements
auf = next();
if (Settings.values.randomAuf) {
Settings.values.randomAuf = false;
}
Settings.values.algAufPrefs[algId] = auf;
Settings.save();
instance = Cube.alg("U", instance);
scramble = instance; // retry from the adjusted AUF, not the original challenge
update(instance);
var aufDisplay = auf == "" ? "" : "(" + auf.substr(0, auf.length - 1) + ") ";
var lookup = lookupAlg(algId);
var displayAlg = simplifyAuf(aufDisplay + lookup.alg.display);
document.getElementById("popup").innerHTML = '<h4>' + displayAlg + '</h4>';
}
}
function retry() {
if (scramble) instance = scramble;
alg = "";
update(instance);
setStatus("init");
}
return {
twist: twist,
btCubeConnect: btCubeConnect,
btCubeDisconnect: btCubeDisconnect,
next: next,
previous: previous,
retry: retry,
adjustAUF: adjustAUF,
toggleThumb: toggleThumb,
showConnectButton: showConnectButton
};
}());