From 280481b26253cb9aa4e1332afa212a21e9d94d20 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 28 Mar 2021 00:38:04 +0000 Subject: [PATCH] added challenge contrlller module, added custom mode2 popup module #495 --- gulpfile.js | 2 + src/js/account.js | 2 +- src/js/challenge-controller.js | 91 ++++++++++++ src/js/custom-mode2-popup.js | 99 +++++++++++++ src/js/global-dependencies.js | 2 + src/js/script.js | 183 +---------------------- src/js/test/test-logic.js | 258 ++++++++++++++++----------------- 7 files changed, 326 insertions(+), 311 deletions(-) create mode 100644 src/js/challenge-controller.js create mode 100644 src/js/custom-mode2-popup.js diff --git a/gulpfile.js b/gulpfile.js index fe048a49d..1ae1f0875 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -130,6 +130,8 @@ const refactoredSrc = [ "./src/js/commandline.js", "./src/js/commandline-lists.js", "./src/js/commandline.js", + "./src/js/challenge-controller.js", + "./src/js/custom-mode2-popup.js", ]; //legacy files diff --git a/src/js/account.js b/src/js/account.js index 46c6962a7..706a43c91 100644 --- a/src/js/account.js +++ b/src/js/account.js @@ -312,7 +312,7 @@ firebase.auth().onAuthStateChanged(function (user) { Notifications.add("Loading challenge", 0); let challengeName = window.location.pathname.split("_")[1]; setTimeout(() => { - setupChallenge(challengeName); + ChallengeController.setup(challengeName); }, 1000); } }); diff --git a/src/js/challenge-controller.js b/src/js/challenge-controller.js new file mode 100644 index 000000000..8c11629be --- /dev/null +++ b/src/js/challenge-controller.js @@ -0,0 +1,91 @@ +import * as Misc from "./misc"; +import * as Notifications from "./notification-center"; +import * as UpdateConfig from "./config"; +import * as ManualRestart from "./manual-restart-tracker"; +import * as CustomText from "./custom-text"; +import * as TestLogic from "./test-logic"; +import * as Funbox from "./funbox"; + +export async function setup(challengeName) { + let list = await Misc.getChallengeList(); + let challenge = list.filter((c) => c.name === challengeName)[0]; + let notitext; + try { + if (challenge === undefined) { + Notifications.add("Challenge not found", 0); + ManualRestart.set(); + TestLogic.restart(false, true); + setTimeout(() => { + $("#top .config").removeClass("hidden"); + $(".page.pageTest").removeClass("hidden"); + }, 250); + return; + } + if (challenge.type === "customTime") { + UpdateConfig.setTimeConfig(challenge.parameters[0], true); + UpdateConfig.setMode("time", true); + UpdateConfig.setDifficulty("normal", true); + if (challenge.name === "englishMaster") { + UpdateConfig.setLanguage("english_10k", true); + UpdateConfig.setNumbers(true, true); + UpdateConfig.setPunctuation(true, true); + } + } + if (challenge.type === "customWords") { + UpdateConfig.setWordCount(challenge.parameters[0], true); + UpdateConfig.setMode("words", true); + UpdateConfig.setDifficulty("normal", true); + } else if (challenge.type === "customText") { + CustomText.setText(challenge.parameters[0].split(" ")); + CustomText.setIsWordRandom(challenge.parameters[1]); + CustomText.setWord(parseInt(challenge.parameters[2])); + UpdateConfig.setMode("custom", true); + UpdateConfig.setDifficulty("normal", true); + } else if (challenge.type === "script") { + let scriptdata = await fetch("/challenges/" + challenge.parameters[0]); + scriptdata = await scriptdata.text(); + let text = scriptdata.trim(); + text = text.replace(/[\n\r\t ]/gm, " "); + text = text.replace(/ +/gm, " "); + CustomText.setText(text.split(" ")); + CustomText.setIsWordRandom(false); + UpdateConfig.setMode("custom", true); + UpdateConfig.setDifficulty("normal", true); + if (challenge.parameters[1] != null) { + UpdateConfig.setTheme(challenge.parameters[1]); + } + if (challenge.parameters[2] != null) { + Funbox.activate(challenge.parameters[2]); + } + } else if (challenge.type === "accuracy") { + UpdateConfig.setTimeConfig(0, true); + UpdateConfig.setMode("time", true); + UpdateConfig.setDifficulty("master", true); + } else if (challenge.type === "funbox") { + Funbox.activate(challenge.parameters[0]); + UpdateConfig.setDifficulty("normal", true); + if (challenge.parameters[1] === "words") { + UpdateConfig.setWordCount(challenge.parameters[2], true); + } else if (challenge.parameters[1] === "time") { + UpdateConfig.setTimeConfig(challenge.parameters[2], true); + } + UpdateConfig.setMode(challenge.parameters[1], true); + if (challenge.parameters[3] !== undefined) { + UpdateConfig.setDifficulty(challenge.parameters[3], true); + } + } + ManualRestart.set(); + TestLogic.restart(false, true); + notitext = challenge.message; + $("#top .config").removeClass("hidden"); + $(".page.pageTest").removeClass("hidden"); + + if (notitext === undefined) { + Notifications.add(`Challenge '${challengeName}' loaded.`, 0); + } else { + Notifications.add("Challenge loaded. " + notitext, 0); + } + } catch (e) { + Notifications.add("Something went wrong: " + e, -1); + } +} diff --git a/src/js/custom-mode2-popup.js b/src/js/custom-mode2-popup.js new file mode 100644 index 000000000..9ea54aabb --- /dev/null +++ b/src/js/custom-mode2-popup.js @@ -0,0 +1,99 @@ +import * as UpdateConfig from "./config"; +import * as ManualRestart from "./manual-restart-tracker"; +import * as Notifications from "./notification-center"; +import * as TestLogic from "./test-logic"; + +export function show(mode) { + if ($("#customMode2PopupWrapper").hasClass("hidden")) { + if (mode == "time") { + $("#customMode2Popup .title").text("Test length"); + $("#customMode2Popup").attr("mode", "time"); + } else if (mode == "words") { + $("#customMode2Popup .title").text("Word amount"); + $("#customMode2Popup").attr("mode", "words"); + } + $("#customMode2PopupWrapper") + .stop(true, true) + .css("opacity", 0) + .removeClass("hidden") + .animate({ opacity: 1 }, 100, (e) => { + $("#customMode2Popup input").focus().select(); + }); + } +} + +function hide() { + if (!$("#customMode2PopupWrapper").hasClass("hidden")) { + $("#customMode2PopupWrapper") + .stop(true, true) + .css("opacity", 1) + .animate( + { + opacity: 0, + }, + 100, + (e) => { + $("#customMode2PopupWrapper").addClass("hidden"); + } + ); + } +} + +function apply() { + let mode = $("#customMode2Popup").attr("mode"); + let val = parseInt($("#customMode2Popup input").val()); + + if (mode == "time") { + if (val !== null && !isNaN(val) && val >= 0) { + UpdateConfig.setTimeConfig(val); + ManualRestart.set(); + TestLogic.restart(); + if (val >= 1800) { + Notifications.add("Stay safe and take breaks!", 0); + } else if (val == 0) { + Notifications.add( + "Infinite time! Make sure to use Bail Out from the command line to save your result.", + 0, + 7 + ); + } + } else { + Notifications.add("Custom time must be at least 1", 0); + } + } else if (mode == "words") { + if (val !== null && !isNaN(val) && val >= 0) { + UpdateConfig.setWordCount(val); + ManualRestart.set(); + TestLogic.restart(); + if (val > 2000) { + Notifications.add("Stay safe and take breaks!", 0); + } else if (val == 0) { + Notifications.add( + "Infinite words! Make sure to use Bail Out from the command line to save your result.", + 0, + 7 + ); + } + } else { + Notifications.add("Custom word amount must be at least 1", 0); + } + } + + hide(); +} + +$("#customMode2PopupWrapper").click((e) => { + if ($(e.target).attr("id") === "customMode2PopupWrapper") { + hide(); + } +}); + +$("#customMode2Popup input").keypress((e) => { + if (e.keyCode == 13) { + apply(); + } +}); + +$("#customMode2Popup .button").click(() => { + apply(); +}); diff --git a/src/js/global-dependencies.js b/src/js/global-dependencies.js index f0074f490..1974251c8 100644 --- a/src/js/global-dependencies.js +++ b/src/js/global-dependencies.js @@ -53,3 +53,5 @@ import * as LanguagePicker from "./language-picker"; import * as UI from "./ui"; import * as Commandline from "./commandline"; import * as CommandlineLists from "./commandline-lists"; +import * as ChallengeController from "./challenge-controller"; +import * as CustomMode2Popup from "./custom-mode2-popup"; diff --git a/src/js/script.js b/src/js/script.js index cae5fa375..1f6747e8b 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -401,101 +401,6 @@ function tagsEdit() { } } -function showCustomMode2Popup(mode) { - if ($("#customMode2PopupWrapper").hasClass("hidden")) { - if (mode == "time") { - $("#customMode2Popup .title").text("Test length"); - $("#customMode2Popup").attr("mode", "time"); - } else if (mode == "words") { - $("#customMode2Popup .title").text("Word amount"); - $("#customMode2Popup").attr("mode", "words"); - } - $("#customMode2PopupWrapper") - .stop(true, true) - .css("opacity", 0) - .removeClass("hidden") - .animate({ opacity: 1 }, 100, (e) => { - $("#customMode2Popup input").focus().select(); - }); - } -} - -function hideCustomMode2Popup() { - if (!$("#customMode2PopupWrapper").hasClass("hidden")) { - $("#customMode2PopupWrapper") - .stop(true, true) - .css("opacity", 1) - .animate( - { - opacity: 0, - }, - 100, - (e) => { - $("#customMode2PopupWrapper").addClass("hidden"); - } - ); - } -} - -$("#customMode2PopupWrapper").click((e) => { - if ($(e.target).attr("id") === "customMode2PopupWrapper") { - hideCustomMode2Popup(); - } -}); - -$("#customMode2Popup input").keypress((e) => { - if (e.keyCode == 13) { - applyMode2Popup(); - } -}); - -$("#customMode2Popup .button").click(() => { - applyMode2Popup(); -}); - -function applyMode2Popup() { - let mode = $("#customMode2Popup").attr("mode"); - let val = parseInt($("#customMode2Popup input").val()); - - if (mode == "time") { - if (val !== null && !isNaN(val) && val >= 0) { - UpdateConfig.setTimeConfig(val); - ManualRestart.set(); - TestLogic.restart(); - if (val >= 1800) { - Notifications.add("Stay safe and take breaks!", 0); - } else if (val == 0) { - Notifications.add( - "Infinite time! Make sure to use Bail Out from the command line to save your result.", - 0, - 7 - ); - } - } else { - Notifications.add("Custom time must be at least 1", 0); - } - } else if (mode == "words") { - if (val !== null && !isNaN(val) && val >= 0) { - UpdateConfig.setWordCount(val); - ManualRestart.set(); - TestLogic.restart(); - if (val > 2000) { - Notifications.add("Stay safe and take breaks!", 0); - } else if (val == 0) { - Notifications.add( - "Infinite words! Make sure to use Bail Out from the command line to save your result.", - 0, - 7 - ); - } - } else { - Notifications.add("Custom word amount must be at least 1", 0); - } - } - - hideCustomMode2Popup(); -} - $(document).on("click", "#top .logo", (e) => { changePage("test"); }); @@ -503,7 +408,7 @@ $(document).on("click", "#top .logo", (e) => { $(document).on("click", "#top .config .wordCount .text-button", (e) => { const wrd = $(e.currentTarget).attr("wordCount"); if (wrd == "custom") { - showCustomMode2Popup("words"); + CustomMode2Popup.show("words"); } else { UpdateConfig.setWordCount(wrd); ManualRestart.set(); @@ -514,7 +419,7 @@ $(document).on("click", "#top .config .wordCount .text-button", (e) => { $(document).on("click", "#top .config .time .text-button", (e) => { let mode = $(e.currentTarget).attr("timeConfig"); if (mode == "custom") { - showCustomMode2Popup("time"); + CustomMode2Popup.show("time"); } else { UpdateConfig.setTimeConfig(mode); ManualRestart.set(); @@ -1811,87 +1716,3 @@ window.addEventListener("keydown", function (e) { e.preventDefault(); } }); - -async function setupChallenge(challengeName) { - let list = await Misc.getChallengeList(); - let challenge = list.filter((c) => c.name === challengeName)[0]; - let notitext; - try { - if (challenge === undefined) { - Notifications.add("Challenge not found", 0); - ManualRestart.set(); - TestLogic.restart(false, true); - setTimeout(() => { - $("#top .config").removeClass("hidden"); - $(".page.pageTest").removeClass("hidden"); - }, 250); - return; - } - if (challenge.type === "customTime") { - UpdateConfig.setTimeConfig(challenge.parameters[0], true); - UpdateConfig.setMode("time", true); - UpdateConfig.setDifficulty("normal", true); - if (challenge.name === "englishMaster") { - UpdateConfig.setLanguage("english_10k", true); - UpdateConfig.setNumbers(true, true); - UpdateConfig.setPunctuation(true, true); - } - } - if (challenge.type === "customWords") { - UpdateConfig.setWordCount(challenge.parameters[0], true); - UpdateConfig.setMode("words", true); - UpdateConfig.setDifficulty("normal", true); - } else if (challenge.type === "customText") { - CustomText.setText(challenge.parameters[0].split(" ")); - CustomText.setIsWordRandom(challenge.parameters[1]); - CustomText.setWord(parseInt(challenge.parameters[2])); - UpdateConfig.setMode("custom", true); - UpdateConfig.setDifficulty("normal", true); - } else if (challenge.type === "script") { - let scriptdata = await fetch("/challenges/" + challenge.parameters[0]); - scriptdata = await scriptdata.text(); - let text = scriptdata.trim(); - text = text.replace(/[\n\r\t ]/gm, " "); - text = text.replace(/ +/gm, " "); - CustomText.setText(text.split(" ")); - CustomText.setIsWordRandom(false); - UpdateConfig.setMode("custom", true); - UpdateConfig.setDifficulty("normal", true); - if (challenge.parameters[1] != null) { - UpdateConfig.setTheme(challenge.parameters[1]); - } - if (challenge.parameters[2] != null) { - Funbox.activate(challenge.parameters[2]); - } - } else if (challenge.type === "accuracy") { - UpdateConfig.setTimeConfig(0, true); - UpdateConfig.setMode("time", true); - UpdateConfig.setDifficulty("master", true); - } else if (challenge.type === "funbox") { - Funbox.activate(challenge.parameters[0]); - UpdateConfig.setDifficulty("normal", true); - if (challenge.parameters[1] === "words") { - UpdateConfig.setWordCount(challenge.parameters[2], true); - } else if (challenge.parameters[1] === "time") { - UpdateConfig.setTimeConfig(challenge.parameters[2], true); - } - UpdateConfig.setMode(challenge.parameters[1], true); - if (challenge.parameters[3] !== undefined) { - UpdateConfig.setDifficulty(challenge.parameters[3], true); - } - } - ManualRestart.set(); - TestLogic.restart(false, true); - notitext = challenge.message; - $("#top .config").removeClass("hidden"); - $(".page.pageTest").removeClass("hidden"); - - if (notitext === undefined) { - Notifications.add(`Challenge '${challengeName}' loaded.`, 0); - } else { - Notifications.add("Challenge loaded. " + notitext, 0); - } - } catch (e) { - Notifications.add("Something went wrong: " + e, -1); - } -} diff --git a/src/js/test/test-logic.js b/src/js/test/test-logic.js index 127099da6..4981080f7 100644 --- a/src/js/test/test-logic.js +++ b/src/js/test/test-logic.js @@ -539,135 +539,6 @@ export async function init() { // } } -export function calculateWpmAndRaw() { - let chars = 0; - let correctWordChars = 0; - let spaces = 0; - for (let i = 0; i < input.history.length; i++) { - let word = Config.mode == "zen" ? input.getHistory(i) : words.get(i); - if (input.getHistory(i) == word) { - //the word is correct - //+1 for space - correctWordChars += word.length; - if ( - i < input.history.length - 1 && - Misc.getLastChar(input.getHistory(i)) !== "\n" - ) { - spaces++; - } - } - chars += input.getHistory(i).length; - } - if (words.getCurrent() == input.current) { - correctWordChars += input.current.length; - } - if (Funbox.active === "nospace") { - spaces = 0; - } - chars += input.current.length; - let testSeconds = TestStats.calculateTestSeconds(performance.now()); - let wpm = Math.round(((correctWordChars + spaces) * (60 / testSeconds)) / 5); - let raw = Math.round(((chars + spaces) * (60 / testSeconds)) / 5); - return { - wpm: wpm, - raw: raw, - }; -} - -export function addWord() { - let bound = 100; - if (Funbox.active === "plus_one") bound = 1; - if ( - words.length - input.history.length > bound || - (Config.mode === "words" && - words.length >= Config.words && - Config.words > 0) || - (Config.mode === "custom" && - CustomText.isWordRandom && - words.length >= CustomText.word && - CustomText.word != 0) || - (Config.mode === "custom" && - !CustomText.isWordRandom && - words.length >= CustomText.text.length) - ) - return; - const language = - Config.mode !== "custom" - ? Misc.getCurrentLanguage() - : { - //borrow the direction of the current language - leftToRight: Misc.getCurrentLanguage().leftToRight, - words: CustomText.text, - }; - const wordset = language.words; - let randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - const previousWord = words.getLast(); - const previousWordStripped = previousWord - .replace(/[.?!":\-,]/g, "") - .toLowerCase(); - const previousWord2Stripped = words - .get(words.length - 2) - .replace(/[.?!":\-,]/g, "") - .toLowerCase(); - - if ( - Config.mode === "custom" && - CustomText.isWordRandom && - wordset.length < 3 - ) { - randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - } else if (Config.mode == "custom" && !CustomText.isWordRandom) { - randomWord = CustomText.text[words.length]; - } else { - while ( - previousWordStripped == randomWord || - previousWord2Stripped == randomWord || - randomWord.indexOf(" ") > -1 || - (!Config.punctuation && randomWord == "I") - ) { - randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - } - } - - if (Funbox.active === "rAnDoMcAsE") { - let randomcaseword = ""; - for (let i = 0; i < randomWord.length; i++) { - if (i % 2 != 0) { - randomcaseword += randomWord[i].toUpperCase(); - } else { - randomcaseword += randomWord[i]; - } - } - randomWord = randomcaseword; - } else if (Funbox.active === "gibberish") { - randomWord = Misc.getGibberish(); - } else if (Funbox.active === "58008") { - randomWord = Misc.getNumbers(7); - } else if (Funbox.active === "specials") { - randomWord = Misc.getSpecials(); - } else if (Funbox.active === "ascii") { - randomWord = Misc.getASCII(); - } - - if (Config.punctuation && Config.mode != "custom") { - randomWord = punctuateWord(previousWord, randomWord, words.length, 0); - } - if (Config.numbers && Config.mode != "custom") { - if (Math.random() < 0.1) { - randomWord = Misc.getNumbers(4); - } - } - - words.push(randomWord); - - let w = "
"; - for (let c = 0; c < randomWord.length; c++) { - w += "" + randomWord.charAt(c) + ""; - } - w += "
"; - $("#words").append(w); -} - export function restart(withSameWordset = false, nosave = false, event) { if (TestUI.testRestarting || TestUI.resultCalculating) { try { @@ -870,6 +741,135 @@ export function restart(withSameWordset = false, nosave = false, event) { ); } +export function calculateWpmAndRaw() { + let chars = 0; + let correctWordChars = 0; + let spaces = 0; + for (let i = 0; i < input.history.length; i++) { + let word = Config.mode == "zen" ? input.getHistory(i) : words.get(i); + if (input.getHistory(i) == word) { + //the word is correct + //+1 for space + correctWordChars += word.length; + if ( + i < input.history.length - 1 && + Misc.getLastChar(input.getHistory(i)) !== "\n" + ) { + spaces++; + } + } + chars += input.getHistory(i).length; + } + if (words.getCurrent() == input.current) { + correctWordChars += input.current.length; + } + if (Funbox.active === "nospace") { + spaces = 0; + } + chars += input.current.length; + let testSeconds = TestStats.calculateTestSeconds(performance.now()); + let wpm = Math.round(((correctWordChars + spaces) * (60 / testSeconds)) / 5); + let raw = Math.round(((chars + spaces) * (60 / testSeconds)) / 5); + return { + wpm: wpm, + raw: raw, + }; +} + +export function addWord() { + let bound = 100; + if (Funbox.active === "plus_one") bound = 1; + if ( + words.length - input.history.length > bound || + (Config.mode === "words" && + words.length >= Config.words && + Config.words > 0) || + (Config.mode === "custom" && + CustomText.isWordRandom && + words.length >= CustomText.word && + CustomText.word != 0) || + (Config.mode === "custom" && + !CustomText.isWordRandom && + words.length >= CustomText.text.length) + ) + return; + const language = + Config.mode !== "custom" + ? Misc.getCurrentLanguage() + : { + //borrow the direction of the current language + leftToRight: Misc.getCurrentLanguage().leftToRight, + words: CustomText.text, + }; + const wordset = language.words; + let randomWord = wordset[Math.floor(Math.random() * wordset.length)]; + const previousWord = words.getLast(); + const previousWordStripped = previousWord + .replace(/[.?!":\-,]/g, "") + .toLowerCase(); + const previousWord2Stripped = words + .get(words.length - 2) + .replace(/[.?!":\-,]/g, "") + .toLowerCase(); + + if ( + Config.mode === "custom" && + CustomText.isWordRandom && + wordset.length < 3 + ) { + randomWord = wordset[Math.floor(Math.random() * wordset.length)]; + } else if (Config.mode == "custom" && !CustomText.isWordRandom) { + randomWord = CustomText.text[words.length]; + } else { + while ( + previousWordStripped == randomWord || + previousWord2Stripped == randomWord || + randomWord.indexOf(" ") > -1 || + (!Config.punctuation && randomWord == "I") + ) { + randomWord = wordset[Math.floor(Math.random() * wordset.length)]; + } + } + + if (Funbox.active === "rAnDoMcAsE") { + let randomcaseword = ""; + for (let i = 0; i < randomWord.length; i++) { + if (i % 2 != 0) { + randomcaseword += randomWord[i].toUpperCase(); + } else { + randomcaseword += randomWord[i]; + } + } + randomWord = randomcaseword; + } else if (Funbox.active === "gibberish") { + randomWord = Misc.getGibberish(); + } else if (Funbox.active === "58008") { + randomWord = Misc.getNumbers(7); + } else if (Funbox.active === "specials") { + randomWord = Misc.getSpecials(); + } else if (Funbox.active === "ascii") { + randomWord = Misc.getASCII(); + } + + if (Config.punctuation && Config.mode != "custom") { + randomWord = punctuateWord(previousWord, randomWord, words.length, 0); + } + if (Config.numbers && Config.mode != "custom") { + if (Math.random() < 0.1) { + randomWord = Misc.getNumbers(4); + } + } + + words.push(randomWord); + + let w = "
"; + for (let c = 0; c < randomWord.length; c++) { + w += "" + randomWord.charAt(c) + ""; + } + w += "
"; + $("#words").append(w); +} + export function fail() { input.pushHistory(); corrected.pushHistory();