From 0c3b6e4d8cc30c32e3760a7417fcafb01bb9add6 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 9 Oct 2023 18:04:54 +0100 Subject: [PATCH] impr(funbox): rework backwards funbox closes #4711 --- backend/src/constants/funbox-list.ts | 6 ++++- backend/src/utils/validation.ts | 7 +++++- frontend/src/ts/test/funbox/funbox-list.ts | 7 ++++-- frontend/src/ts/test/funbox/funbox.ts | 6 +++++ frontend/src/ts/test/words-generator.ts | 28 ++++++++++++++++++++-- frontend/src/ts/types/types.d.ts | 5 +++- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/backend/src/constants/funbox-list.ts b/backend/src/constants/funbox-list.ts index 3cf80a1e2..ae3f74346 100644 --- a/backend/src/constants/funbox-list.ts +++ b/backend/src/constants/funbox-list.ts @@ -322,7 +322,11 @@ const FunboxList: MonkeyTypes.FunboxMetadata[] = [ }, { name: "backwards", - properties: ["noLigatures", "conflictsWithSymmetricChars"], + properties: [ + "noLigatures", + "conflictsWithSymmetricChars", + "wordOrder:reverse", + ], frontendFunctions: ["applyCSS"], canGetPb: true, difficultyLevel: 3, diff --git a/backend/src/utils/validation.ts b/backend/src/utils/validation.ts index 572b191b8..85c2c9454 100644 --- a/backend/src/utils/validation.ts +++ b/backend/src/utils/validation.ts @@ -131,6 +131,10 @@ export function areFunboxesCompatible(funboxesString: string): boolean { funboxesToCheck.filter((f) => f.properties?.find((fp) => fp === "nospace" || fp.startsWith("toPush")) ).length <= 1; + const oneWordOrderMax = + funboxesToCheck.filter((f) => + f.properties?.find((fp) => fp.startsWith("wordOrder")) + ).length <= 1; const oneChangesWordsVisibilityMax = funboxesToCheck.filter((f) => f.properties?.find((fp) => fp === "changesWordsVisibility") @@ -229,6 +233,7 @@ export function areFunboxesCompatible(funboxesString: string): boolean { onePunctuateWordMax && oneCharCheckerMax && oneCharReplacerMax && - noConfigConflicts + noConfigConflicts && + oneWordOrderMax ); } diff --git a/frontend/src/ts/test/funbox/funbox-list.ts b/frontend/src/ts/test/funbox/funbox-list.ts index af949cbbd..f16153cbe 100644 --- a/frontend/src/ts/test/funbox/funbox-list.ts +++ b/frontend/src/ts/test/funbox/funbox-list.ts @@ -249,8 +249,11 @@ const list: MonkeyTypes.FunboxMetadata[] = [ { name: "backwards", info: "...sdrawkcab epyt ot yrt woN", - properties: ["noLigatures", "conflictsWithSymmetricChars"], - hasCSS: true, + properties: [ + "noLigatures", + "conflictsWithSymmetricChars", + "wordOrder:reverse", + ], }, ]; diff --git a/frontend/src/ts/test/funbox/funbox.ts b/frontend/src/ts/test/funbox/funbox.ts index 971b48b59..94f95dc96 100644 --- a/frontend/src/ts/test/funbox/funbox.ts +++ b/frontend/src/ts/test/funbox/funbox.ts @@ -218,6 +218,12 @@ FunboxList.setFunboxFunctions("rAnDoMcAsE", { }, }); +FunboxList.setFunboxFunctions("backwards", { + alterText(word: string): string { + return word.split("").reverse().join(""); + }, +}); + FunboxList.setFunboxFunctions("capitals", { alterText(word: string): string { return Misc.capitalizeFirstLetterOfEachWord(word); diff --git a/frontend/src/ts/test/words-generator.ts b/frontend/src/ts/test/words-generator.ts index c126ae499..4752173c2 100644 --- a/frontend/src/ts/test/words-generator.ts +++ b/frontend/src/ts/test/words-generator.ts @@ -327,6 +327,18 @@ function applyLazyModeToWord( return word; } +export function getQuoteOrCustomModeWordOrder(): MonkeyTypes.FunboxWordOrder { + const wordOrder = FunboxList.get(Config.funbox) + .find((f) => f.properties?.find((fp) => fp.startsWith("wordOrder"))) + ?.properties?.find((fp) => fp.startsWith("wordOrder")); + + if (!wordOrder) { + return "normal"; + } else { + return wordOrder.split(":")[1] as MonkeyTypes.FunboxWordOrder; + } +} + export function getWordsLimit(): number { let limit = 100; @@ -432,14 +444,26 @@ export async function generateWords( }; const limit = getWordsLimit(); + const wordOrder = getQuoteOrCustomModeWordOrder(); + console.debug("Word order", wordOrder); + let wordList = language.words; if (Config.mode === "custom") { - wordList = CustomText.text; + if (wordOrder === "reverse") { + wordList = CustomText.text.reverse(); + } else { + wordList = CustomText.text; + } } const wordset = await Wordset.withWords(wordList); if (Config.mode === "quote") { - return await generateQuoteWords(language, wordset, limit); + const quoteWords = await generateQuoteWords(language, wordset, limit); + if (wordOrder === "reverse") { + quoteWords.words.reverse(); + quoteWords.sectionIndexes.reverse(); + } + return quoteWords; } if ( diff --git a/frontend/src/ts/types/types.d.ts b/frontend/src/ts/types/types.d.ts index 5a81fe778..aa895b8ea 100644 --- a/frontend/src/ts/types/types.d.ts +++ b/frontend/src/ts/types/types.d.ts @@ -228,6 +228,8 @@ declare namespace MonkeyTypes { type FunboxWordsFrequency = "normal" | "zipf"; + type FunboxWordOrder = "normal" | "reverse"; + type FunboxProperty = | "symmetricChars" | "conflictsWithSymmetricChars" @@ -244,7 +246,8 @@ declare namespace MonkeyTypes { | "nospace" | `toPush:${number}` | "noInfiniteDuration" - | "changesWordsFrequency"; + | "changesWordsFrequency" + | `wordOrder:${FunboxWordOrder}`; interface FunboxFunctions { getWord?: (wordset?: Misc.Wordset, wordIndex?: number) => string;