/
index.ts
67 lines (61 loc) · 2.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import BingoGenerator from "./generator";
import { BingoList, Goal } from "./types/goalList";
import { extractGoalList } from "./util";
import { Mode, Profile } from "./types/settings";
import { BingoBoard } from "./bingoBoard";
import { DEFAULT_PROFILES } from "./constants/profiles";
/**
* Main function for generating bingo boards (used in live versions)
* Function name has to be preserved for compatibility with bingosetup.js in the bingo repo
* Returns board in the right (legacy) format for the bingo setup
* @param bingoList Object with the goal list
* @param options Object containing mode and seed
* @returns A bingo board in the legacy format (array with goals starting from index 1, and metadata in the array object)
*/
export function ootBingoGenerator(bingoList: BingoList, options: { mode: Mode; seed: number }) {
const goalList = extractGoalList(bingoList, options.mode);
if (!goalList) {
return;
}
const profile = DEFAULT_PROFILES[options.mode];
const bingoGenerator = new BingoGenerator(goalList, options.mode, profile);
const board = bingoGenerator.generateBoard(options.seed);
if (!board) {
return;
}
// make goals start from position 1 in the list (as expected by bingosetup.js)
const shiftedGoals: Goal[] = [];
board.goals.forEach((goal, index) => (shiftedGoals[index + 1] = goal));
// @ts-ignore Typescript does not like the legacy format where a string key is used to store the meta information in the array object
shiftedGoals["meta"] = { iterations: board.iterations };
return shiftedGoals;
}
/**
* Wrapper for BingoSync
*/
export function bingoGenerator(bingoList: BingoList, options: { mode: Mode; seed: number }) {
return ootBingoGenerator(bingoList, options);
}
/**
* Function for generating boards, for internal use
* @param bingoList Object with the goal list
* @param mode Mode (normal, short, blackout, etc.)
* @param seed Rng seed
* @param profile Optional, the generator uses a standard profile fitting the mode if not provided. Note that in previous generators the profiles were always built in.
* @param maxIterations Optional, the maximum amount of attempts to successfully generate a board. Defaults to 100.
* @returns A bingo board object
*/
export function generateBingoBoard(
bingoList: BingoList,
mode: Mode,
seed: number,
profile?: Profile,
maxIterations?: number
): BingoBoard | undefined {
const goalList = extractGoalList(bingoList, mode);
if (!goalList) {
return;
}
const bingoGenerator = new BingoGenerator(goalList, mode, profile ?? DEFAULT_PROFILES[mode]);
return bingoGenerator.generateBoard(seed, maxIterations);
}