Skip to content

Commit

Permalink
impr(custom text): add shuffle mode
Browse files Browse the repository at this point in the history
with this, you can ensure you will encounter each word at least once
  • Loading branch information
Miodec committed Apr 27, 2024
1 parent f541f7c commit 25fd805
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions frontend/src/html/popups.html
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@
<div class="buttonGroup">
<button value="simple">simple</button>
<button value="repeat">repeat</button>
<button value="shuffle">shuffle</button>
<button value="random">random</button>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/ts/test/words-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ export async function getNextWord(
(wordset.length < 4 || PractiseWords.before.mode !== null)
) {
randomWord = wordset.randomWord(funboxFrequency);
} else if (Config.mode === "custom" && CustomText.getMode() === "shuffle") {
randomWord = wordset.shuffledWord();
} else if (
Config.mode === "custom" &&
CustomText.getLimitMode() === "section"
Expand Down
33 changes: 26 additions & 7 deletions frontend/src/ts/test/wordset.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
import * as FunboxList from "./funbox/funbox-list";
import { dreymarIndex } from "../utils/misc";
import { randomElementFromArray } from "../utils/arrays";
import { randomElementFromArray, shuffle } from "../utils/arrays";
import Config from "../config";

let currentWordset: Wordset | null = null;
let currentWordset: MonkeyTypes.Wordset | null = null;

export class Wordset implements MonkeyTypes.Wordset {
words: string[];
length: number;

shuffledIndexes: number[];

export class Wordset {
public words: string[];
public length: number;
constructor(words: string[]) {
this.words = words;
this.length = this.words.length;
this.shuffledIndexes = [];
}

public randomWord(mode: MonkeyTypes.FunboxWordsFrequency): string {
randomWord(mode: MonkeyTypes.FunboxWordsFrequency): string {
if (mode === "zipf") {
return this.words[dreymarIndex(this.words.length)] as string;
} else {
return randomElementFromArray(this.words);
}
}

shuffledWord(): string {
if (this.shuffledIndexes.length === 0) {
this.generateShuffledIndexes();
}
return this.words[this.shuffledIndexes.pop() as number] as string;
}

generateShuffledIndexes(): void {
this.shuffledIndexes = [];
for (let i = 0; i < this.length; i++) {
this.shuffledIndexes.push(i);
}
shuffle(this.shuffledIndexes);
}
}

export async function withWords(words: string[]): Promise<Wordset> {
export async function withWords(words: string[]): Promise<MonkeyTypes.Wordset> {
const wordFunbox = FunboxList.get(Config.funbox).find(
(f) => f.functions?.withWords
);
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/ts/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ declare namespace MonkeyTypes {
| `wordOrder:${FunboxWordOrder}`;

class Wordset {
public words: string[];
public length: number;
words: string[];
length: number;
shuffledIndexes: number[];
constructor(words: string[]);
randomWord(mode: MonkeyTypes.FunboxWordsFrequency): string;
shuffledWord(): string;
generateShuffledIndexes(): void;
}

class Section {
Expand Down
2 changes: 1 addition & 1 deletion shared-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ declare namespace SharedTypes {
hash?: string;
}

type CustomTextMode = "repeat" | "random";
type CustomTextMode = "repeat" | "random" | "shuffle";
type CustomTextLimitMode = "word" | "time" | "section";
type CustomTextLimit = {
value: number;
Expand Down

0 comments on commit 25fd805

Please sign in to comment.