From 0202725d4f6bc28c2b7bafef0f8fcf636361e262 Mon Sep 17 00:00:00 2001 From: ben potter <39702521+benjaminpotter@users.noreply.github.com> Date: Tue, 8 Jun 2021 11:10:17 -0400 Subject: [PATCH] Add poetry funbox by benjaminpotter --- functions/package-lock.json | 1 - gulpfile.js | 1 + src/js/test/poetry.js | 61 +++++++++++++ src/js/test/test-logic.js | 170 ++++++++++++++++++++---------------- static/funbox/_list.json | 5 ++ 5 files changed, 161 insertions(+), 77 deletions(-) create mode 100644 src/js/test/poetry.js diff --git a/functions/package-lock.json b/functions/package-lock.json index 4381a499b..7b5fe0997 100644 --- a/functions/package-lock.json +++ b/functions/package-lock.json @@ -2611,7 +2611,6 @@ "version": "6.11.2", "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz", "integrity": "sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==", - "hasInstallScript": true, "optional": true, "dependencies": { "@protobufjs/aspromise": "^1.1.2", diff --git a/gulpfile.js b/gulpfile.js index 9b3aa2ddc..d1450107b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -165,6 +165,7 @@ const refactoredSrc = [ "./src/js/test/test-timer.js", "./src/js/test/test-config.js", "./src/js/test/layout-emulator.js", + "./src/js/test/poetry.js", "./src/js/replay.js", ]; diff --git a/src/js/test/poetry.js b/src/js/test/poetry.js new file mode 100644 index 000000000..4fe4640f4 --- /dev/null +++ b/src/js/test/poetry.js @@ -0,0 +1,61 @@ +const bannedChars = ["—", "_", " "]; +const maxWords = 100; +const apiURL = "https://poetrydb.org/random"; + +export class Poem { + constructor(title, author, words) { + this.title = title; + this.author = author; + this.words = words; + + this.cleanUpText(); + } + + cleanUpText() { + var count = 0; + var scrubbedWords = []; + for (var i = 0; i < this.words.length; i++) { + let scrubbed = ""; + for (var j = 0; j < this.words[i].length; j++) { + if (!bannedChars.includes(this.words[i][j])) + scrubbed += this.words[i][j]; + } + + if (scrubbed == "") continue; + + scrubbedWords.push(scrubbed); + count++; + + if (count == maxWords) break; + } + + this.words = scrubbedWords; + } +} + +export async function getPoem() { + return new Promise((res, rej) => { + console.log("Getting poem"); + var poemReq = new XMLHttpRequest(); + poemReq.onload = () => { + if (poemReq.readyState == 4) { + if (poemReq.status == 200) { + let poemObj = JSON.parse(poemReq.responseText)[0]; + let words = []; + poemObj.lines.forEach((line) => { + line.split(" ").forEach((word) => { + words.push(word); + }); + }); + + let poem = new Poem(poemObj.title, poemObj.author, words); + res(poem); + } else { + rej(poemReq.status); + } + } + }; + poemReq.open("GET", apiURL); + poemReq.send(); + }); +} diff --git a/src/js/test/test-logic.js b/src/js/test/test-logic.js index 3e971d4d6..24384d050 100644 --- a/src/js/test/test-logic.js +++ b/src/js/test/test-logic.js @@ -29,13 +29,16 @@ import * as CloudFunctions from "./cloud-functions"; import * as TestLeaderboards from "./test-leaderboards"; import * as Replay from "./replay.js"; import * as MonkeyPower from "./monkey-power"; +import * as Poetry from "./poetry.js"; let glarsesMode = false; -export function toggleGlarses(){ +export function toggleGlarses() { glarsesMode = true; - console.log('Glarses Mode On - test result will be hidden. You can check the stats in the console (here)'); - console.log('To disable Glarses Mode refresh the page.'); + console.log( + "Glarses Mode On - test result will be hidden. You can check the stats in the console (here)" + ); + console.log("To disable Glarses Mode refresh the page."); } export let notSignedInLastResult = null; @@ -450,75 +453,83 @@ export async function init() { if (Config.mode == "custom") { wordset = CustomText.text; } - for (let i = 0; i < wordsBound; i++) { - let randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - const previousWord = words.get(i - 1); - const previousWord2 = words.get(i - 2); - if ( - Config.mode == "custom" && - (CustomText.isWordRandom || CustomText.isTimeRandom) - ) { - randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - } else if (Config.mode == "custom" && !CustomText.isWordRandom) { - randomWord = CustomText.text[i]; - } else { - while ( - randomWord == previousWord || - randomWord == previousWord2 || - (!Config.punctuation && randomWord == "I") || - randomWord.indexOf(" ") > -1 + + if (Config.funbox == "poetry") { + let poem = await Poetry.getPoem(); + poem.words.forEach((word) => { + words.push(word); + }); + } else { + for (let i = 0; i < wordsBound; i++) { + let randomWord = wordset[Math.floor(Math.random() * wordset.length)]; + const previousWord = words.get(i - 1); + const previousWord2 = words.get(i - 2); + if ( + Config.mode == "custom" && + (CustomText.isWordRandom || CustomText.isTimeRandom) ) { randomWord = wordset[Math.floor(Math.random() * wordset.length)]; - } - } - - if (Config.funbox === "rAnDoMcAsE") { - let randomcaseword = ""; - for (let i = 0; i < randomWord.length; i++) { - if (i % 2 != 0) { - randomcaseword += randomWord[i].toUpperCase(); - } else { - randomcaseword += randomWord[i]; + } else if (Config.mode == "custom" && !CustomText.isWordRandom) { + randomWord = CustomText.text[i]; + } else { + while ( + randomWord == previousWord || + randomWord == previousWord2 || + (!Config.punctuation && randomWord == "I") || + randomWord.indexOf(" ") > -1 + ) { + randomWord = wordset[Math.floor(Math.random() * wordset.length)]; } } - randomWord = randomcaseword; - } else if (Config.funbox === "gibberish") { - randomWord = Misc.getGibberish(); - } else if (Config.funbox === "58008") { - // UpdateConfig.setPunctuation(false, true); - UpdateConfig.setNumbers(false, true); - randomWord = Misc.getNumbers(7); - } else if (Config.funbox === "specials") { - UpdateConfig.setPunctuation(false, true); - UpdateConfig.setNumbers(false, true); - randomWord = Misc.getSpecials(); - } else if (Config.funbox === "ascii") { - UpdateConfig.setPunctuation(false, true); - UpdateConfig.setNumbers(false, true); - randomWord = Misc.getASCII(); - } - if (Config.punctuation) { - randomWord = punctuateWord(previousWord, randomWord, i, wordsBound); - } - if (Config.numbers) { - if ( - Math.random() < 0.1 && - i !== 0 && - Misc.getLastChar(previousWord) !== "." - ) { - randomWord = Misc.getNumbers(4); - if (i == wordsBound - 1) { - randomWord += "."; + if (Config.funbox === "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 (Config.funbox === "gibberish") { + randomWord = Misc.getGibberish(); + } else if (Config.funbox === "58008") { + // UpdateConfig.setPunctuation(false, true); + UpdateConfig.setNumbers(false, true); + randomWord = Misc.getNumbers(7); + } else if (Config.funbox === "specials") { + UpdateConfig.setPunctuation(false, true); + UpdateConfig.setNumbers(false, true); + randomWord = Misc.getSpecials(); + } else if (Config.funbox === "ascii") { + UpdateConfig.setPunctuation(false, true); + UpdateConfig.setNumbers(false, true); + randomWord = Misc.getASCII(); + } + + if (Config.punctuation) { + randomWord = punctuateWord(previousWord, randomWord, i, wordsBound); + } + if (Config.numbers) { + if ( + Math.random() < 0.1 && + i !== 0 && + Misc.getLastChar(previousWord) !== "." + ) { + randomWord = Misc.getNumbers(4); + if (i == wordsBound - 1) { + randomWord += "."; + } } } - } - if (/\t/g.test(randomWord)) { - setHasTab(true); - } + if (/\t/g.test(randomWord)) { + setHasTab(true); + } - words.push(randomWord); + words.push(randomWord); + } } } else if (Config.mode == "quote") { // setLanguage(Config.language.replace(/_\d*k$/g, ""), true); @@ -1852,7 +1863,7 @@ export function finish(difficultyFailed = false) { ChartController.result.update({ duration: 0 }); ChartController.result.resize(); - if(glarsesMode){ + if (glarsesMode) { $("#middle #result .glarsesmessage").remove(); $("#middle #result").prepend(` @@ -1870,21 +1881,28 @@ export function finish(difficultyFailed = false) { $("#middle #result #resultReplay").remove(); $("#middle #result .loginTip").remove(); - console.log(`Test Completed: ${stats.wpm} wpm ${stats.acc}% acc ${stats.wpmRaw} raw ${consistency}% consistency`); - + console.log( + `Test Completed: ${stats.wpm} wpm ${stats.acc}% acc ${stats.wpmRaw} raw ${consistency}% consistency` + ); } - UI.swapElements($("#typingTest"), $("#result"), 250, () => { - TestUI.setResultCalculating(false); - $("#words").empty(); - ChartController.result.resize(); - if (Config.alwaysShowWordsHistory) { - TestUI.toggleResultWords(); + UI.swapElements( + $("#typingTest"), + $("#result"), + 250, + () => { + TestUI.setResultCalculating(false); + $("#words").empty(); + ChartController.result.resize(); + if (Config.alwaysShowWordsHistory) { + TestUI.toggleResultWords(); + } + $("#testModesNotice").addClass("hidden"); + }, + () => { + Keymap.hide(); } - $("#testModesNotice").addClass("hidden"); - }, () => { - Keymap.hide(); - }); + ); } export function fail() { diff --git a/static/funbox/_list.json b/static/funbox/_list.json index d30977118..3202a53ea 100644 --- a/static/funbox/_list.json +++ b/static/funbox/_list.json @@ -98,6 +98,11 @@ "name": "nospace", "type": "script", "info": "Whoneedsspacesanyway?" + }, + { + "name": "poetry", + "type": "script", + "info": "Practice typing some beautiful prose." } ]