Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 40x 40x 40x 19x 19x 81x 81x 19x 19x 19x 35x 22x 3x 19x 19x 90x 33x 30x 14x 29x 1x 29x 16x 15x 15x 24x 24x 24x 10x 1x | import {
type CourseDefinition,
type CourseStageRef,
createCourseStageRef,
isClassCourseConstraints,
type RandomCourseDefinition,
} from './definition.ts';
import { type CourseChartRecord, parseCourseChartFilter, selectCourseCharts } from './filter.ts';
/** A drawn random course: the fixed course to play and the candidate drawn for each stage. */
export interface RandomCourseDrawSuccess<T> {
ok: true;
course: CourseDefinition;
/** One per stage, in stage order. */
charts: T[];
}
/** Why a random course could not be drawn: the first stage whose filter is invalid or selects no chart. */
export interface RandomCourseDrawFailure {
ok: false;
/** Zero-based stage index. */
stage: number;
reason: 'invalid-filter' | 'no-candidates';
/** The parse error, for `invalid-filter`. */
error?: string;
}
export type RandomCourseDrawResult<T> = RandomCourseDrawSuccess<T> | RandomCourseDrawFailure;
/** An index in `[0, length)` from a `[0, 1)` draw, as `java.util.Random.nextInt(length)` gives. */
function pick(random: () => number, length: number): number {
const value = Math.floor(random() * length);
return Number.isFinite(value) ? Math.min(length - 1, Math.max(0, value)) : 0;
}
/** The identity beatoraja's `distinct` compares: the chart's SHA-256 (MD5 for a record without one). */
function chartKey(record: CourseChartRecord): string {
return record.sha256 !== undefined && record.sha256.length > 0 ? record.sha256.toLowerCase() : `md5:${record.md5}`;
}
/**
* Draws one chart per stage of a random course, as beatoraja's `RandomCourseData.lotterySongDatas` does, and returns
* the fixed course to play.
*
* - Each stage draws uniformly from the candidates its filter selects, after the filter's own `ORDER BY` / `LIMIT`
* ({@link selectCourseCharts}; `ORDER BY RANDOM()` draws from `random` first). A stage without a filter draws from
* every candidate when it is the first stage, and from the previous filter's selection otherwise.
* - With `distinct`, a chart already drawn for an earlier stage is redrawn; when every candidate of the stage is taken,
* the stage falls back to any of them, so a chart can repeat (beatoraja does the same).
* - A candidate without a valid `md5` / `sha256` cannot be a course stage and is never drawn.
*
* The course is a dan course (`grade`) when the definition has a lane-option constraint, else `expert`, and it is
* never `release`d (beatoraja's `RandomCourseData.createCourseData`). Deterministic given `random`, which returns
* values in `[0, 1)` like `Math.random`; candidates are taken in the order given.
*/
export function drawRandomCourse<T extends CourseChartRecord>(
definition: RandomCourseDefinition,
candidates: readonly T[],
random: () => number,
): RandomCourseDrawResult<T> {
const usable: Array<{ chart: T; ref: CourseStageRef }> = [];
for (const chart of candidates) {
const ref = createCourseStageRef(chart);
if (ref !== undefined) usable.push({ chart, ref });
}
const drawn: Array<{ chart: T; ref: CourseStageRef }> = [];
let lots = usable;
for (const [index, stage] of definition.stages.entries()) {
if (stage.filter !== undefined || index === 0) {
if (stage.filter === undefined) {
lots = usable;
} else {
const parsed = parseCourseChartFilter(stage.filter);
if (!parsed.ok) return { ok: false, stage: index, reason: 'invalid-filter', error: parsed.error };
lots = selectCourseCharts(parsed.query, usable, ({ chart }) => chart, random);
}
}
if (lots.length === 0) return { ok: false, stage: index, reason: 'no-candidates' };
drawn.push(definition.distinct ? drawDistinct(lots, drawn, random) : lots[pick(random, lots.length)]!);
}
return {
ok: true,
course: {
name: definition.name,
kind: isClassCourseConstraints(definition.constraints) ? 'grade' : 'expert',
stages: drawn.map(({ ref }) => ref),
constraints: [...definition.constraints],
trophies: definition.trophies.map((trophy) => ({ ...trophy })),
release: false,
source: { format: 'beatoraja-random' },
},
charts: drawn.map(({ chart }) => chart),
};
}
/** `RandomCourseData.lotterySongData` with `distinct`: redraw a repeat, and allow one once every candidate is taken. */
function drawDistinct<E extends { chart: CourseChartRecord }>(
lots: readonly E[],
drawn: readonly E[],
random: () => number,
): E {
const taken = new Set(drawn.map(({ chart }) => chartKey(chart)));
const remaining = [...lots];
while (remaining.length > 0) {
const index = pick(random, remaining.length);
const candidate = remaining[index]!;
if (!taken.has(chartKey(candidate.chart))) return candidate;
remaining.splice(index, 1);
}
return lots[pick(random, lots.length)]!;
}
|