fix deterministic authoritative RNG fallbacks

This commit is contained in:
2026-07-28 05:46:08 +00:00
parent 53ec5d543e
commit 43966ea1ef
11 changed files with 267 additions and 42 deletions
@@ -57,14 +57,16 @@ const readPattern = (world: InMemoryTurnWorld, config: Record<string, unknown>):
);
};
const shuffledDefaultPattern = (): number[] => {
const pattern = [0, 0, 1, 2, 3];
for (let index = pattern.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(Math.random() * (index + 1));
[pattern[index], pattern[swapIndex]] = [pattern[swapIndex]!, pattern[index]!];
}
return pattern;
};
const shuffledDefaultPattern = (
hiddenSeed: string | number,
previousYear: number,
previousMonth: number
): number[] =>
new RandUtil(
new LiteHashDRBG(
simpleSerialize(hiddenSeed, 'monthly', previousYear, previousMonth, 'tournamentPattern')
)
).shuffle([0, 0, 1, 2, 3]);
export const createTournamentAutoStartHandler = (options: {
profileName: string;
@@ -112,7 +114,10 @@ export const createTournamentAutoStartHandler = (options: {
}
const pattern = readPattern(world, config);
const resolvedPattern = pattern.length > 0 ? pattern : shuffledDefaultPattern();
const resolvedPattern =
pattern.length > 0
? pattern
: shuffledDefaultPattern(hiddenSeed, context.previousYear, context.previousMonth);
const type = resolvedPattern.pop() ?? 0;
world.updateWorldMeta({ tournamentPattern: resolvedPattern });
const now = options.now?.() ?? new Date();