diff --git a/frontend/src/ts/test/funbox/funbox-functions.ts b/frontend/src/ts/test/funbox/funbox-functions.ts index 564881d3644d..f9b55ddb9bb5 100644 --- a/frontend/src/ts/test/funbox/funbox-functions.ts +++ b/frontend/src/ts/test/funbox/funbox-functions.ts @@ -146,19 +146,58 @@ class PseudolangWordGenerator extends Wordset { } export class PolyglotWordset extends Wordset { - public wordsWithLanguage: Map; - public languageProperties: Map; + readonly wordsMap: Map; + readonly languageProperties: Map; + private currLang: Language; + readonly langs: Language[]; constructor( - wordsWithLanguage: Map, + wordsMap: Map, languageProperties: Map, ) { - // build and shuffle the word array - const wordArray = Array.from(wordsWithLanguage.keys()); - Arrays.shuffle(wordArray); - super(wordArray); - this.wordsWithLanguage = wordsWithLanguage; + super([]); this.languageProperties = languageProperties; + this.langs = Array.from(languageProperties.keys()); + this.wordsMap = wordsMap; + this.resetIndexes(); + this.length = Array.from(this.wordsMap.values()).reduce( + (sum, ws) => sum + ws.words.length, + 0, + ); + this.currLang = this.langs[0] as Language; + } + + get currentLanguage(): Language { + return this.currLang; + } + + override resetIndexes(): void { + this.wordsMap.forEach((ws, _) => { + ws.resetIndexes(); + }); + } + + private uniformLang(): Language { + const index = Math.floor(Math.random() * this.langs.length); + this.currLang = this.langs[index] as Language; + return this.currLang; + } + + private getWordset(): Wordset { + const lang = this.uniformLang(); + return this.wordsMap.get(lang) as Wordset; + } + + override randomWord(mode: FunboxWordsFrequency): string { + return this.getWordset().randomWord(mode); + } + + override shuffledWord(): string { + return this.getWordset().shuffledWord(); + } + + override nextWord(): string { + return this.getWordset().nextWord(); } } @@ -764,13 +803,10 @@ const list: Partial> = { ]), ); - const wordsWithLanguage = new Map( - languages.flatMap((lang) => - lang.words.map((word) => [word, lang.name]), - ), + const wordsMap: Map = new Map( + languages.map((lang) => [lang.name, new Wordset(lang.words)]), ); - - return new PolyglotWordset(wordsWithLanguage, languageProperties); + return new PolyglotWordset(wordsMap, languageProperties); }, }, }; diff --git a/frontend/src/ts/test/words-generator.ts b/frontend/src/ts/test/words-generator.ts index e36dfdf0206e..627da63fbcab 100644 --- a/frontend/src/ts/test/words-generator.ts +++ b/frontend/src/ts/test/words-generator.ts @@ -388,7 +388,7 @@ async function applyBritishEnglishToWord( function applyLazyModeToWord(word: string, language: LanguageObject): string { // polyglot mode, use the word's actual language if (currentWordset && currentWordset instanceof PolyglotWordset) { - const langName = currentWordset.wordsWithLanguage.get(word); + const langName = currentWordset.currentLanguage; const langProps = langName ? currentWordset.languageProperties.get(langName) : undefined; @@ -508,9 +508,8 @@ async function getQuoteWordList( // because it will be reversed again in the generateWords function if (wordOrder === "reverse") { return currentWordset.words.reverse(); - } else { - return currentWordset.words; } + return currentWordset.words; } const languageToGet = language.name.startsWith("swiss_german") ? "german" @@ -655,17 +654,13 @@ export async function generateWords( const funbox = findSingleActiveFunboxWithFunction("withWords"); if (funbox) { - const result = await funbox.functions.withWords(wordList); + currentWordset = await funbox.functions.withWords(wordList); // PolyglotWordset if polyglot otherwise Wordset - if (result instanceof PolyglotWordset) { - const polyglotResult = result; - currentWordset = polyglotResult; + if (currentWordset instanceof PolyglotWordset) { // set allLigatures if any language in languageProperties has ligatures true ret.allLigatures = Array.from( - polyglotResult.languageProperties.values(), + currentWordset.languageProperties.values(), ).some((props) => !!props.ligatures); - } else { - currentWordset = result; } } else { currentWordset = await withWords(wordList); @@ -901,9 +896,9 @@ export async function getNextWord( const usingFunboxWithGetWord = isFunboxActiveWithFunction("getWord"); const randomWordLanguage = - (currentWordset instanceof PolyglotWordset - ? currentWordset.wordsWithLanguage.get(randomWord) - : Config.language) ?? Config.language; // Fall back to Config language if per-word language is unavailable + currentWordset instanceof PolyglotWordset + ? currentWordset.currentLanguage + : Config.language; // Fall back to Config language if per-word language is unavailable if ( Config.mode !== "custom" &&