Skip to content

Commit

Permalink
impr(word generation): allow tests with plus n funboxes to be repeated
Browse files Browse the repository at this point in the history
also simplifies the test repeat system a little bit

closes #5282
  • Loading branch information
Miodec committed Apr 8, 2024
1 parent 2dd752f commit 9af105b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
49 changes: 4 additions & 45 deletions frontend/src/ts/test/test-logic.ts
Expand Up @@ -180,23 +180,6 @@ export function restart(options = {} as RestartOptions): void {
}
}

if (options.withSameWordset) {
const funboxToPush =
FunboxList.get(Config.funbox)
.find((f) => f.properties?.find((fp) => fp.startsWith("toPush")))
?.properties?.find((fp) => fp.startsWith("toPush:")) ?? "";
if (funboxToPush) {
Notifications.add(
"You can't repeat a test with currently active funboxes",
0,
{
important: true,
}
);
options.withSameWordset = false;
}
}

if (TestState.isActive) {
if (TestState.isRepeated) {
options.withSameWordset = true;
Expand Down Expand Up @@ -333,34 +316,10 @@ export function restart(options = {} as RestartOptions): void {
repeatWithPace = true;
}

if (!options.withSameWordset) {
TestState.setRepeated(false);
TestState.setPaceRepeat(repeatWithPace);
await init();
await PaceCaret.init();
} else {
await Funbox.activate();
TestState.setRepeated(true);
TestState.setPaceRepeat(repeatWithPace);
Replay.stopReplayRecording();
TestWords.words.resetCurrentIndex();
TestInput.input.reset();
TestUI.showWords();
if (Config.keymapMode === "next" && Config.mode !== "zen") {
void KeymapEvent.highlight(
Arrays.nthElementFromArray(
[...TestWords.words.getCurrent()],
0
) as string
);
}
Funbox.toggleScript(TestWords.words.getCurrent());
await PaceCaret.init();
}

if (Config.mode === "quote") {
TestState.setRepeated(false);
}
TestState.setRepeated(options.withSameWordset ?? false);
TestState.setPaceRepeat(repeatWithPace);
await init();
await PaceCaret.init();

for (const f of FunboxList.get(Config.funbox)) {
if (f.functions?.restart) f.functions.restart();
Expand Down
48 changes: 43 additions & 5 deletions frontend/src/ts/test/words-generator.ts
Expand Up @@ -477,6 +477,9 @@ export async function generateWords(
words: string[];
sectionIndexes: number[];
}> {
if (!TestState.isRepeated) {
previousGetNextWordReturns = [];
}
currentQuote = [];
currentSection = [];
sectionIndex = 0;
Expand Down Expand Up @@ -579,6 +582,20 @@ async function generateQuoteWords(
words: [],
sectionIndexes: [],
};
if (TestState.isRepeated) {
for (let i = 0; i < limit; i++) {
const repeated = previousGetNextWordReturns[i];

if (repeated === undefined) {
throw new WordGenError("Repeated word is undefined");
}

ret.words.push(repeated.word);
ret.sectionIndexes.push(repeated.sectionIndex);
}
return ret;
}

const languageToGet = language.name.startsWith("swiss_german")
? "german"
: language.name;
Expand Down Expand Up @@ -674,6 +691,13 @@ export let sectionIndex = 0;
export let currentSection: string[] = [];
let sectionHistory: string[] = [];

let previousGetNextWordReturns: GetNextWordReturn[] = [];

type GetNextWordReturn = {
word: string;
sectionIndex: number;
};

//generate next word
export async function getNextWord(
wordset: Wordset.Wordset,
Expand All @@ -682,18 +706,28 @@ export async function getNextWord(
wordsBound: number,
previousWord: string,
previousWord2: string
): Promise<{
word: string;
sectionIndex: number;
}> {
): Promise<GetNextWordReturn> {
console.debug("Getting next word", {
isRepeated: TestState.isRepeated,
wordset,
wordIndex,
language,
wordsBound,
previousWord,
previousWord2,
});

if (TestState.isRepeated) {
const repeated = previousGetNextWordReturns[wordIndex];

if (repeated === undefined) {
throw new WordGenError("Repeated word is undefined");
}

console.debug("Repeated word: ", repeated);
return repeated;
}

const funboxFrequency = getFunboxWordsFrequency() ?? "normal";
let randomWord = wordset.randomWord(funboxFrequency);
const previousWordRaw = previousWord.replace(/[.?!":\-,]/g, "").toLowerCase();
Expand Down Expand Up @@ -838,8 +872,12 @@ export async function getNextWord(

console.debug("Word:", randomWord);

return {
const ret = {
word: randomWord,
sectionIndex: sectionIndex,
};

previousGetNextWordReturns.push(ret);

return ret;
}

0 comments on commit 9af105b

Please sign in to comment.