moved test logic to a module

This commit is contained in:
Miodec 2021-03-24 17:37:55 +00:00
parent bfc83574d1
commit 5f67f3e5b5
15 changed files with 526 additions and 338 deletions

View file

@ -118,6 +118,7 @@ const refactoredSrc = [
"./src/js/test/live-acc.js",
"./src/js/test/test-leaderboards.js",
"./src/js/test/timer-progress.js",
"./src/js/test/test-logic.js",
];
//legacy files

View file

@ -416,7 +416,7 @@ function getAccountDataAndInit() {
AccountIcon.loading(false);
}
if (Config.paceCaret === "pb" || Config.paceCaret === "average") {
if (!testActive) {
if (!TestLogic.active) {
initPaceCaret(true);
}
}

View file

@ -566,7 +566,7 @@ let commands = {
id: "bailOutForSure",
display: "Yes, I am sure",
exec: () => {
bailout = true;
TestLogic.setBailout(true);
showResult();
},
available: () => {
@ -1544,12 +1544,7 @@ function updateCommandsTagsList() {
DB.getSnapshot().tags.forEach((tag) => {
tag.active = false;
});
TestUI.updateModesNotice(
sameWordset,
textHasTab,
paceCaret,
activeFunbox
);
TestUI.updateModesNotice(paceCaret, activeFunbox);
saveActiveTagsToCookie();
},
});
@ -1569,12 +1564,7 @@ function updateCommandsTagsList() {
sticky: true,
exec: () => {
toggleTag(tag.id);
TestUI.updateModesNotice(
sameWordset,
textHasTab,
paceCaret,
activeFunbox
);
TestUI.updateModesNotice(paceCaret, activeFunbox);
let txt = tag.name;
if (tag.active === true) {

View file

@ -47,3 +47,4 @@ import * as CapsWarning from "./caps-warning";
import * as LiveAcc from "./live-acc";
import * as TestLeaderboards from "./test-leaderboards";
import * as TimerProgress from "./timer-progress";
import * as TestLogic from "./test-logic";

File diff suppressed because it is too large Load diff

View file

@ -757,7 +757,7 @@ function toggleTag(tagid, nosave = false) {
}
}
});
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveActiveTagsToCookie();
}

View file

@ -1,5 +1,6 @@
import * as Misc from "./misc";
import Config from "./config";
import * as TestLogic from "./test-logic";
export let caretAnimating = true;
@ -27,9 +28,9 @@ export function hide() {
$("#caret").addClass("hidden");
}
export function show(currentInput) {
export function show() {
if ($("#result").hasClass("hidden")) {
updatePosition(currentInput);
updatePosition();
$("#caret").removeClass("hidden");
startAnimation();
}
@ -37,7 +38,7 @@ export function show(currentInput) {
//TODO remove this after test logic is a module
//TODO remove config when module
export function updatePosition(currentInput) {
export function updatePosition() {
if ($("#wordsWrapper").hasClass("hidden")) return;
if ($("#caret").hasClass("off")) {
return;
@ -45,7 +46,7 @@ export function updatePosition(currentInput) {
let caret = $("#caret");
let inputLen = currentInput.length;
let inputLen = TestLogic.input.current.length;
let currentLetterIndex = inputLen - 1;
if (currentLetterIndex == -1) {
currentLetterIndex = 0;

View file

@ -2,7 +2,6 @@ import * as Caret from "./caret";
let state = false;
//TODO remove testActive once in a module
export function set(foc) {
if (foc && !state) {
state = true;

View file

@ -2,7 +2,6 @@ import Config from "./config";
import * as ThemeColors from "./theme-colors";
import layouts from "./layouts";
//TODO remove after wordslist and currentinput and current word index are in a module
export function highlightKey(currentKey) {
if (Config.mode === "zen") return;
try {
@ -10,11 +9,6 @@ export function highlightKey(currentKey) {
$(".active-key").removeClass("active-key");
}
// var currentKey = wordsList[currentWordIndex]
// .substring(currentInput.length, currentInput.length + 1)
// .toString()
// .toUpperCase();
let highlightKey;
switch (currentKey) {
case "\\":

View file

@ -1,4 +1,4 @@
import Config from './config';
import Config from "./config";
export function update(acc) {
let number = Math.floor(acc);
@ -9,10 +9,9 @@ export function update(acc) {
document.querySelector("#liveAcc").innerHTML = number + "%";
}
export function show() {
if (!Config.showLiveAcc) return;
// if (!testActive) return;
// if (!TestLogic.active) return;
if (Config.timerStyle === "mini") {
// $("#miniTimerAndLiveWpm .wpm").css("opacity", Config.timerOpacity);
if (!$("#miniTimerAndLiveWpm .acc").hasClass("hidden")) return;

View file

@ -1,7 +1,7 @@
import Config from "./config";
export function update(wpm, raw) {
// if (!testActive || !Config.showLiveWpm) {
// if (!TestLogic.active || !Config.showLiveWpm) {
// hideLiveWpm();
// } else {
// showLiveWpm();

151
src/js/test/test-logic.js Normal file
View file

@ -0,0 +1,151 @@
class Words {
constructor() {
this.list = [];
this.length = 0;
this.currentIndex = 0;
}
get(i) {
if (i === undefined) {
return this.list;
} else {
return this.list[i];
}
}
getCurrent() {
return this.list[this.currentIndex];
}
getLast() {
return this.list[this.list.length - 1];
}
push(word) {
this.list.push(word);
this.length = this.list.length;
}
reset() {
this.list = [];
this.currentIndex = 0;
this.length = this.list.length;
}
resetCurrentIndex() {
this.currentIndex = 0;
}
decreaseCurrentIndex() {
this.currentIndex--;
}
increaseCurrentIndex() {
this.currentIndex++;
}
}
class Input {
constructor() {
this.current = "";
this.history = [];
}
reset() {
this.current = "";
this.history = [];
}
resetHistory() {
this.history = [];
}
setCurrent(val) {
this.current = val;
this.length = this.current.length;
}
appendCurrent(val) {
this.current += val;
this.length = this.current.length;
}
resetCurrent() {
this.current = "";
}
pushHistory() {
this.history.push(this.current);
this.historyLength = this.history.length;
this.resetCurrent();
}
popHistory() {
return this.history.pop();
}
getHistory(i) {
return this.history[i];
}
}
class Corrected {
constructor() {
this.current = "";
this.history = [];
}
setCurrent(val) {
this.current = val;
}
appendCurrent(val) {
this.current += val;
}
resetCurrent() {
this.current = "";
}
resetHistory() {
this.history = [];
}
reset() {
this.resetCurrent();
this.resetHistory();
}
getHistory(i) {
return this.history[i];
}
popHistory() {
return this.history.pop();
}
pushHistory() {
this.history.push(this.current);
}
}
export let active = false;
export let words = new Words();
export let input = new Input();
export let corrected = new Corrected();
export let currentWordIndex = 0;
export let isRepeated = false;
export let hasTab = false;
export let randomQuote = null;
export let bailout = false;
export function setActive(tf) {
active = tf;
}
export function setRepeated(tf) {
isRepeated = tf;
}
export function setHasTab(tf) {
hasTab = tf;
}
export function setBailout(tf) {
bailout = tf;
}
export function setRandomQuote(rq) {
randomQuote = rq;
}

View file

@ -2,6 +2,7 @@ import * as Notifications from "./notification-center";
import * as ThemeColors from "./theme-colors";
import Config from "./config";
import * as DB from "./db";
import * as TestLogic from "./test-logic";
export let currentWordElementIndex = 0;
export let resultVisible = false;
@ -213,24 +214,19 @@ export function lineJump(currentTop) {
currentTestLine++;
}
export function updateModesNotice(
sameWordset,
textHasTab,
paceCaret,
activeFunbox
) {
export function updateModesNotice(paceCaret, activeFunbox) {
let anim = false;
if ($(".pageTest #testModesNotice").text() === "") anim = true;
$(".pageTest #testModesNotice").empty();
if (sameWordset) {
if (TestLogic.isRepeated) {
$(".pageTest #testModesNotice").append(
`<div class="text-button" function="restartTest()" style="color:var(--error-color);"><i class="fas fa-sync-alt"></i>repeated</div>`
);
}
if (textHasTab) {
if (TestLogic.hasTab) {
$(".pageTest #testModesNotice").append(
`<div class="text-button"><i class="fas fa-long-arrow-alt-right"></i>shift + tab to restart</div>`
);

View file

@ -1,6 +1,7 @@
import Config from "./config";
import * as CustomText from "./custom-text";
import * as Misc from "./misc";
import * as TestLogic from "./test-logic";
export function show() {
let op = Config.showTimerProgress ? Config.timerOpacity : 0;
@ -84,7 +85,7 @@ export function restart() {
}
//TODO remove the parameters once they are inside a module
export function update(time, wordsList, currentWordIndex, inputHistory) {
export function update(time) {
if (
Config.mode === "time" ||
(Config.mode === "custom" && CustomText.isTimeRandom)
@ -123,7 +124,7 @@ export function update(time, wordsList, currentWordIndex, inputHistory) {
Config.mode === "quote"
) {
if (Config.timerStyle === "bar") {
let outof = wordsList.length;
let outof = TestLogic.words.length;
if (Config.mode === "words") {
outof = Config.words;
}
@ -134,7 +135,9 @@ export function update(time, wordsList, currentWordIndex, inputHistory) {
outof = CustomText.text.length;
}
}
let percent = Math.floor(((currentWordIndex + 1) / outof) * 100);
let percent = Math.floor(
((TestLogic.words.currentIndex + 1) / outof) * 100
);
$("#timer")
.stop(true, true)
.animate(
@ -144,7 +147,7 @@ export function update(time, wordsList, currentWordIndex, inputHistory) {
250
);
} else if (Config.timerStyle === "text") {
let outof = wordsList.length;
let outof = TestLogic.words.length;
if (Config.mode === "words") {
outof = Config.words;
}
@ -156,14 +159,16 @@ export function update(time, wordsList, currentWordIndex, inputHistory) {
}
}
if (outof === 0) {
$("#timerNumber").html("<div>" + `${inputHistory.length}` + "</div>");
$("#timerNumber").html(
"<div>" + `${TestLogic.input.history.length}` + "</div>"
);
} else {
$("#timerNumber").html(
"<div>" + `${inputHistory.length}/${outof}` + "</div>"
"<div>" + `${TestLogic.input.history.length}/${outof}` + "</div>"
);
}
} else if (Config.timerStyle === "mini") {
let outof = wordsList.length;
let outof = TestLogic.words.length;
if (Config.mode === "words") {
outof = Config.words;
}
@ -175,16 +180,22 @@ export function update(time, wordsList, currentWordIndex, inputHistory) {
}
}
if (Config.words === 0) {
$("#miniTimerAndLiveWpm .time").html(`${inputHistory.length}`);
$("#miniTimerAndLiveWpm .time").html(
`${TestLogic.input.history.length}`
);
} else {
$("#miniTimerAndLiveWpm .time").html(`${inputHistory.length}/${outof}`);
$("#miniTimerAndLiveWpm .time").html(
`${TestLogic.input.history.length}/${outof}`
);
}
}
} else if (Config.mode == "zen") {
if (Config.timerStyle === "text") {
$("#timerNumber").html("<div>" + `${inputHistory.length}` + "</div>");
$("#timerNumber").html(
"<div>" + `${TestLogic.input.history.length}` + "</div>"
);
} else {
$("#miniTimerAndLiveWpm .time").html(`${inputHistory.length}`);
$("#miniTimerAndLiveWpm .time").html(`${TestLogic.input.history.length}`);
}
}
}

View file

@ -146,7 +146,7 @@ function setDifficulty(diff, nosave) {
}
ConfigSet.difficulty(diff);
if (!nosave) restartTest(false, nosave);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -166,7 +166,7 @@ function toggleBlindMode() {
blind = false;
}
ConfigSet.blindMode(blind);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
saveConfigToCookie();
}
@ -175,7 +175,7 @@ function setBlindMode(blind, nosave) {
blind = false;
}
ConfigSet.blindMode(blind);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -243,7 +243,7 @@ function setStopOnError(soe, nosave) {
if (Config.stopOnError !== "off") {
ConfigSet.confidenceMode("off");
}
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -320,7 +320,7 @@ function setPaceCaret(val, nosave) {
// val = "off";
// }
ConfigSet.paceCaret(val);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
initPaceCaret(nosave);
if (!nosave) saveConfigToCookie();
}
@ -339,7 +339,7 @@ function setMinWpm(minwpm, nosave) {
minwpm = "off";
}
ConfigSet.minWpm(minwpm);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -357,7 +357,7 @@ function setMinAcc(min, nosave) {
min = "off";
}
ConfigSet.minAcc(min);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -1014,7 +1014,7 @@ function setConfidenceMode(cm, nosave) {
ConfigSet.stopOnError("off");
}
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (!nosave) saveConfigToCookie();
}
@ -1146,7 +1146,7 @@ function setLayout(layout, nosave) {
layout = "qwerty";
}
ConfigSet.layout(layout);
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
if (Config.keymapLayout === "overrideSync") {
Keymap.refreshKeys(Config.keymapLayout, setKeymapLayout);
}
@ -1497,5 +1497,5 @@ function applyConfig(configObj) {
$("#nitropay_ad_about").remove();
}
}
TestUI.updateModesNotice(sameWordset, textHasTab, paceCaret, activeFunbox);
TestUI.updateModesNotice(paceCaret, activeFunbox);
}