From da5bfe362ccf212259088ee55846b3c21a4184b7 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 4 Aug 2020 01:42:42 +0100 Subject: [PATCH] refactored script further --- public/js/misc.js | 105 ++++++++++++++++++++++++++++++++++ public/js/script.js | 133 ++------------------------------------------ 2 files changed, 111 insertions(+), 127 deletions(-) diff --git a/public/js/misc.js b/public/js/misc.js index 44213ea22..3e295fd08 100644 --- a/public/js/misc.js +++ b/public/js/misc.js @@ -48,3 +48,108 @@ async function getFontsList() { return fontsList; } } + +function smooth(arr, windowSize, getter = (value) => value, setter) { + const get = getter; + const result = []; + + for (let i = 0; i < arr.length; i += 1) { + const leftOffeset = i - windowSize; + const from = leftOffeset >= 0 ? leftOffeset : 0; + const to = i + windowSize + 1; + + let count = 0; + let sum = 0; + for (let j = from; j < to && j < arr.length; j += 1) { + sum += get(arr[j]); + count += 1; + } + + result[i] = setter ? setter(arr[i], sum / count) : sum / count; + } + + return result; +} + +function stdDev(array) { + try { + const n = array.length; + const mean = array.reduce((a, b) => a + b) / n; + return Math.sqrt( + array.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n + ); + } catch (e) { + return 0; + } +} + +function mean(array) { + try { + return ( + array.reduce((previous, current) => (current += previous)) / array.length + ); + } catch (e) { + return 0; + } +} + +function showNotification(text, time) { + let noti = $(".notification"); + noti.text(text); + noti.css("top", `-${noti.outerHeight()}px`); + noti.stop(true, true).animate( + { + top: "1rem", + }, + 250, + "swing", + () => { + noti.stop(true, true).animate( + { + opacity: 1, + }, + time, + () => { + noti.stop(true, true).animate( + { + top: `-${noti.outerHeight()}px`, + }, + 250, + "swing" + ); + } + ); + } + ); +} + +function getReleasesFromGitHub() { + $.getJSON( + "https://api.github.com/repos/Miodec/monkey-type/releases", + (data) => { + $("#bottom .version").text(data[0].name).css("opacity", 1); + $("#versionHistory .releases").empty(); + data.forEach((release) => { + if (!release.draft && !release.prerelease) { + $("#versionHistory .releases").append(` +
+
${release.name}
+
${moment(release.published_at).format( + "DD MMM YYYY" + )}
+
${release.body.replace(/\r\n/g, "
")}
+
+ `); + } + }); + } + ); +} + +function getLastChar(word) { + return word.charAt(word.length - 1); +} + +function capitalizeFirstLetter(str) { + return str.charAt(0).toUpperCase() + str.slice(1); +} diff --git a/public/js/script.js b/public/js/script.js index a7c60bed2..83df98945 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -70,50 +70,6 @@ const generatePairingCode = firebase .functions() .httpsCallable("generatePairingCode"); -function smooth(arr, windowSize, getter = (value) => value, setter) { - const get = getter; - const result = []; - - for (let i = 0; i < arr.length; i += 1) { - const leftOffeset = i - windowSize; - const from = leftOffeset >= 0 ? leftOffeset : 0; - const to = i + windowSize + 1; - - let count = 0; - let sum = 0; - for (let j = from; j < to && j < arr.length; j += 1) { - sum += get(arr[j]); - count += 1; - } - - result[i] = setter ? setter(arr[i], sum / count) : sum / count; - } - - return result; -} - -function stdDev(array) { - try { - const n = array.length; - const mean = array.reduce((a, b) => a + b) / n; - return Math.sqrt( - array.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n - ); - } catch (e) { - return 0; - } -} - -function mean(array) { - try { - return ( - array.reduce((previous, current) => (current += previous)) / array.length - ); - } catch (e) { - return 0; - } -} - function refreshThemeColorObject() { let st = getComputedStyle(document.body); @@ -134,36 +90,6 @@ function refreshThemeColorObject() { .replace(" ", ""); } -function showNotification(text, time) { - let noti = $(".notification"); - noti.text(text); - noti.css("top", `-${noti.outerHeight()}px`); - noti.stop(true, true).animate( - { - top: "1rem", - }, - 250, - "swing", - () => { - noti.stop(true, true).animate( - { - opacity: 1, - }, - time, - () => { - noti.stop(true, true).animate( - { - top: `-${noti.outerHeight()}px`, - }, - 250, - "swing" - ); - } - ); - } - ); -} - function copyResultToClipboard() { if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) { showNotification("Sorry, this feature is not supported in Firefox", 4000); @@ -268,29 +194,6 @@ function toggleScriptFunbox(...params) { } } -function getReleasesFromGitHub() { - $.getJSON( - "https://api.github.com/repos/Miodec/monkey-type/releases", - (data) => { - $("#bottom .version").text(data[0].name).css("opacity", 1); - $("#versionHistory .releases").empty(); - data.forEach((release) => { - if (!release.draft && !release.prerelease) { - $("#versionHistory .releases").append(` -
-
${release.name}
-
${moment(release.published_at).format( - "DD MMM YYYY" - )}
-
${release.body.replace(/\r\n/g, "
")}
-
- `); - } - }); - } - ); -} - function verifyUsername() { // test = firebase.functions().httpsCallable('moveResults') // test2 = firebase.functions().httpsCallable('getNames') @@ -341,10 +244,6 @@ function getuid() { console.error("Only share this uid with Miodec and nobody else!"); } -function getLastChar(word) { - return word.charAt(word.length - 1); -} - function setFocus(foc) { if (foc && !focusState) { focusState = true; @@ -363,15 +262,6 @@ function setFocus(foc) { } } -function capitalizeFirstLetter(str) { - return str.charAt(0).toUpperCase() + str.slice(1); -} - -function roundedToFixed(float, digits) { - let rounded = Math.pow(10, digits); - return (Math.round(float * rounded) / rounded).toFixed(digits); -} - function initWords() { testActive = false; wordsList = []; @@ -703,28 +593,17 @@ function showWords() { .css("overflow", "hidden"); } - // if ($(".active-key") != undefined) { - // $(".active-key").removeClass("active-key"); - // } - var currentKey = wordsList[currentWordIndex] .substring(currentInput.length, currentInput.length + 1) .toString() .toUpperCase(); - // var highlightKey = `#Key${currentKey}`; - - // $(highlightKey).addClass("active-key"); - if (config.keymapMode === "next") { updateHighlightedKeymapKey(); } updateActiveElement(); updateCaretPosition(); - // if (config.keymap !== "off") { - // changeKeymapLayout(config.keymapLayout); - // } } function updateActiveElement() { @@ -1415,7 +1294,7 @@ function showResult(difficultyFailed = false) { }; } clearIntervals(); - let testtime = roundedToFixed(stats.time, 1); + let testtime = stats.time; $("#result .stats .wpm .bottom").text(Math.round(stats.wpm)); $("#result .stats .wpm .bottom").attr("aria-label", stats.wpm); $("#result .stats .raw .bottom").text(Math.round(stats.wpmRaw)); @@ -1437,8 +1316,11 @@ function showResult(difficultyFailed = false) { let afkseconds = keypressPerSecond.filter((x) => x == 0).length; - $("#result .stats .time .bottom").text(testtime + "s"); - $("#result .stats .time .bottom").attr("aria-label", `${afkseconds}s afk`); + $("#result .stats .time .bottom").text(Math.round(testtime) + "s"); + $("#result .stats .time .bottom").attr( + "aria-label", + `${roundTo2(testtime)}s (${afkseconds}s afk)` + ); setTimeout(function () { $("#resultExtraButtons").removeClass("hidden").css("opacity", 0).animate( @@ -1457,11 +1339,8 @@ function showResult(difficultyFailed = false) { let mode2 = ""; if (config.mode === "time") { mode2 = config.time; - // $("#result .stats .time").addClass('hidden'); } else if (config.mode === "words") { mode2 = config.words; - // $("#result .stats .time").removeClass('hidden'); - // $("#result .stats .time .bottom").text(roundedToFixed(stats.time,1)+'s'); } else if (config.mode === "custom") { mode2 = "custom"; } else if (config.mode === "quote") {