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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | 46x 46x 46x 46x 3x 43x 43x 107x 43x 46x 43x 43x 43x 1x 42x 2x 40x 40x 67x 67x 66x 66x 66x 39x 39x 39x 39x 46x 46x 29x 29x 29x 116x 116x 116x 29x 29x 29x 29x 26x 26x 26x 26x 26x 16x 26x 104x 104x 104x 104x 104x 104x 104x 104x 26x 46x 51x 63x 51x 104x 96x 96x 46x 26x 104x 8x 96x 96x 96x 26x 8x 32x 75x 11x 16x 26x 26x 51x 16x 16x 2x 2x 8x 8x 8x 8x 2x | import { eq } from 'drizzle-orm';
import type { BatchItem } from 'drizzle-orm/batch';
import {
checkCourseReplayEligibility,
REPLAY_ENGINE_VERSION,
rejudgeCourseReplay,
resolveCoursePlayedRuleset,
type BeMusicCourseReplay,
type BeMusicReplay,
type CourseReplayUnrankedReason,
type ReplayCourseResult,
type RulesetId,
} from '@be-music/player-engine/replay';
import { normalizeCourseConstraints } from '@be-music/player-engine/course';
import type { Database } from '../db/client.ts';
import { course, courseBestScore, courseScore, courseScoreResult } from '../db/schema.ts';
import { sha256HexAsync as sha256Hex } from '@be-music/shared-utils/hash';
import { byRulesetOrder, RULESET_IDS } from '../rulesets.ts';
import {
checkPlayedChart,
gzipText,
playedChartStatements,
putChartDocument,
type PlayedChart,
} from '../scores/played-chart.ts';
import { checkChartPlayOptions } from '../scores/chart-play-options.ts';
import { identifyPlay, type NotSimulatedOutcome, type SubmissionDeps } from '../scores/submit.ts';
import { mergeBestScores } from '../scoring/best.ts';
import { clearLampRank } from '@be-music/player-engine/ruleset';
import { loadCourseBestScores, toCourseBestScoreRow } from './bests.ts';
import { courseTrophyRank, mergeCourseTrophy, toCourseStageFigures } from './figures.ts';
import { countCoursePlayers, rankOfCourseExScore } from './ranking.ts';
import { checkNonstopStageCharts, checkUnreachedStageCharts, type StageChartInput } from './stage-charts.ts';
import { checkCourseRun } from './verify.ts';
/** The fingerprint documents of one played stage, in the order the stages were played. */
export interface CourseStageDocuments {
playDocument: unknown;
chartDocument?: unknown;
}
export interface CourseSubmissionInput {
userId: string;
courseLog: BeMusicCourseReplay;
/** The course replay as stored (canonical JSON text). */
courseLogText: string;
/** One entry per played stage; none for a NONSTOP run, whose stages are rebuilt from {@link stageCharts}. */
stages: readonly CourseStageDocuments[];
/** The charts of the stages the server rebuilds: every unreached stage, and every stage of a NONSTOP run. */
stageCharts?: readonly StageChartInput[];
submittedBy?: string;
}
export interface CourseRulesetOutcome {
ruleset: RulesetId;
rulesetRevision: string;
exScore: number;
noteCount: number;
/** The course lamp, by its rank (`clearLampRank`). */
clearLamp: number;
missCount: number;
maxCombo: number;
stagesPlayed: number;
trophy: string | null;
rank: number;
players: number;
improved: { exScore: boolean; lamp: boolean; missCount: boolean; trophy: boolean };
/** `truncated` when the run stopped at a failed stage this ruleset's gauge would have survived (ADR 0019). */
outcome?: 'truncated';
}
export type CourseSubmissionOutcome =
| {
status: 'accepted' | 'duplicate';
courseScoreId: string;
courseFingerprint: string;
results: CourseRulesetOutcome[];
/** The rulesets the run cannot be judged under; absent when it is judged under every one. */
notSimulated?: NotSimulatedOutcome[];
/** Why the run was stored without ranking it; its results then carry rank and players 0. */
unranked?: CourseReplayUnrankedReason;
}
| { status: 'rejected'; httpStatus: 400 | 409 | 422; reason: string; stage?: number };
/**
* Accepts one course run (docs/leaderboard.md section 6.1). Each played stage passes the checks a single play does
* (eligibility, fingerprint documents, chart registration), each unreached stage's chart backs up its facts
* (`checkUnreachedStageCharts`), the run is re-simulated under every ruleset with `simulateCourseReplay`, and
* `checkCourseRun` refuses runs whose stages do not follow from each other or that were given up. A NONSTOP run is
* verified on its stitched play instead: the server stitches the stage charts again (`checkNonstopStageCharts`) and the
* play must have been recorded on that chart. A course run creates no chart scores. Rejections never write anything;
* the course replay goes to R2 before the D1 batch, as for single plays. A run that does not rank (a stage at another
* tempo, an unreleased course: `checkCourseReplayEligibility`'s `unranked`) is stored and simulated all the same, but
* moves no course best and takes no place in the course ranking.
*/
export async function submitCourseScore(
deps: SubmissionDeps,
input: CourseSubmissionInput,
): Promise<CourseSubmissionOutcome> {
const { db, blobs } = deps;
const { courseLog } = input;
const eligibility = checkCourseReplayEligibility(courseLog);
if (!eligibility.eligible) {
return { status: 'rejected', httpStatus: 422, reason: eligibility.reason, ...stageOf(eligibility.stage) };
}
const { courseFingerprint, assistFlags, assistClass, unranked } = eligibility;
// A run with an assisted stage is stored unranked (`assist`, #602); `assisted` keeps which board it would be on.
const assisted = assistClass !== 'none';
const playedLogs = courseLog.stages.flatMap((entry) => (entry.played ? [entry.replay] : []));
const nonstop =
courseLog.nonstop !== undefined && eligibility.nonstop !== undefined ? eligibility.nonstop : undefined;
Iif (input.stages.length !== (nonstop !== undefined ? 0 : playedLogs.length)) {
return { status: 'rejected', httpStatus: 400, reason: 'invalid-submission' };
}
const submissionId = await sha256Hex(
nonstop !== undefined
? identifyNonstopRun(courseFingerprint, courseLog.nonstop!.replay, nonstop.playFingerprint)
: identifyCourseRun(courseFingerprint, playedLogs, eligibility.stages),
);
const existing = await db.query.courseScore.findFirst({ where: eq(courseScore.submissionId, submissionId) });
if (existing !== undefined && existing.userId !== input.userId) {
return { status: 'rejected', httpStatus: 409, reason: 'play-already-submitted' };
}
if (existing !== undefined) {
return {
status: 'duplicate',
courseScoreId: existing.id,
courseFingerprint: existing.courseFingerprint,
results: await loadCourseOutcomes(
db,
existing.id,
existing.courseFingerprint,
existing.assisted,
existing.unranked,
),
...(existing.unranked !== null ? { unranked: existing.unranked } : {}),
};
}
// Each played stage's chart, as a single play's (L3, chart registration, L4).
const playedCharts: PlayedChart[] = [];
for (const [index, stage] of eligibility.stages.entries()) {
const check = await checkPlayedChart(db, { ...stage, ...input.stages[index]! });
if (!check.ok) return { status: 'rejected', httpStatus: check.httpStatus, reason: check.reason, stage: index };
const chartOptions = checkChartPlayOptions(playedLogs[index]!, input.stages[index]!.playDocument);
Iif (!chartOptions.ok) return { status: 'rejected', httpStatus: 422, reason: chartOptions.reason, stage: index };
playedCharts.push(check);
}
// A NONSTOP run's stitched play runs no stage's gameplay options.
Iif (nonstop !== undefined && !checkChartPlayOptions(courseLog.nonstop!.replay, undefined).ok) {
return { status: 'rejected', httpStatus: 422, reason: 'chart-play-options-mismatch' };
}
const playedRuleset = resolveCoursePlayedRuleset(courseLog);
Iif (playedRuleset === undefined) {
return { status: 'rejected', httpStatus: 422, reason: 'course-ruleset-mismatch' };
}
const stageCharts = input.stageCharts ?? [];
const charts =
nonstop !== undefined
? checkNonstopStageCharts(courseLog, stageCharts, {
playFingerprint: nonstop.playFingerprint,
ruleset: playedRuleset,
})
: checkUnreachedStageCharts(courseLog, stageCharts);
if (!charts.ok) return { status: 'rejected', httpStatus: 422, reason: charts.reason, ...stageOf(charts.stage) };
// Each ruleset re-judges the run by ADR 0019's rules, as a single play's: a ruleset that cannot read it stores nothing,
// and a truncated run stores its figures unranked.
const simulated = new Map<RulesetId, { result: ReplayCourseResult; truncated: boolean }>();
const notSimulated: NotSimulatedOutcome[] = [];
for (const ruleset of RULESET_IDS) {
const outcome = rejudgeCourseReplay(courseLog, { ruleset });
Iif (outcome.status === 'not-simulated') notSimulated.push({ ruleset, reason: outcome.reason });
else simulated.set(ruleset, { result: outcome.result, truncated: outcome.status === 'truncated' });
}
const playedRun = simulated.get(playedRuleset);
Iif (playedRun === undefined) return { status: 'rejected', httpStatus: 422, reason: 'course-ruleset-mismatch' };
const run = checkCourseRun(courseLog, playedRun.result);
if (!run.ok) return { status: 'rejected', httpStatus: 422, reason: run.reason, ...stageOf(run.stage) };
const now = deps.now();
const courseScoreId = deps.newId();
const replayKey = `course-replays/${courseScoreId}.bmcourse.json.gz`;
const [currentBests] = await Promise.all([
unranked === undefined ? loadCourseBestScores(db, input.userId, courseFingerprint, assisted) : new Map(),
gzipText(input.courseLogText).then((compressed) =>
blobs.put(replayKey, compressed, {
httpMetadata: { contentType: 'application/json', contentEncoding: 'gzip' },
}),
),
...uniqueRegistrations(playedCharts).map((played) => putChartDocument(blobs, played)),
]);
const figures = [...simulated].map(([ruleset, { result, truncated }]) => {
const lamp = clearLampRank(result.lamp);
const current = currentBests.get(ruleset);
const update = mergeBestScores(current, {
scoreId: courseScoreId,
playedAt: now,
exScore: result.exScore,
lamp,
missCount: result.missCount,
});
const trophyRank = courseTrophyRank(courseLog.course.trophies, result.trophy);
const trophy = mergeCourseTrophy(current?.trophy, {
scoreId: courseScoreId,
trophy: result.trophy ?? null,
rank: trophyRank,
});
const playedHere = (current?.playedHere ?? false) || playedRuleset === ruleset;
return { ruleset, result, truncated, lamp, update, trophyRank, trophy, playedHere };
});
// A truncated run moves no course best and takes no place in a ranking.
const ranked = figures.filter(({ truncated }) => !truncated);
// A NONSTOP run's gauge is its stitched play's.
const lastPlayed = courseLog.nonstop?.replay ?? playedLogs.at(-1)!;
const statements: BatchItem<'sqlite'>[] = [
// A NONSTOP run registers no stage charts: its cut-out stages carry the stitched play's judge rank and TOTAL, not
// their charts' own.
...(nonstop !== undefined
? []
: playedLogs.flatMap((replay, index) => playedChartStatements(db, replay, playedCharts[index]!, now))),
db
.insert(course)
.values({
courseFingerprint,
name: courseLog.course.name,
kind: courseLog.course.kind,
constraints: [...new Set(normalizeCourseConstraints(courseLog.course.constraints))].sort(),
stageChartFingerprints: courseLog.stages.map((entry) =>
entry.played ? entry.replay.chart.chartFingerprint! : entry.chartFingerprint!,
),
createdAt: new Date(now),
updatedAt: new Date(now),
})
// The first run names the course; a concurrent first run may have registered it already.
.onConflictDoNothing(),
db.insert(courseScore).values({
id: courseScoreId,
submissionId,
userId: input.userId,
courseFingerprint,
playedRuleset,
gauge: lastPlayed.play.gauge,
assisted,
assistFlags,
// A NONSTOP run's charts were all rebuilt here from the stage charts, which hash to their chart fingerprints.
verification: playedCharts.every((played) => played.verification === 'L4') ? 'L4' : 'L3',
stagesPlayed: playedLogs.length,
recordedBy: courseLog.recorder !== undefined ? { ...courseLog.recorder } : null,
submittedBy: input.submittedBy ?? null,
serverEngine: REPLAY_ENGINE_VERSION,
replayKey,
unranked: unranked ?? null,
createdAt: new Date(now),
updatedAt: new Date(now),
}),
...figures.map(({ ruleset, result, truncated, lamp, trophyRank }) =>
db.insert(courseScoreResult).values({
courseScoreId,
ruleset,
rulesetRevision: result.ruleset,
exScore: result.exScore,
noteCount: result.noteCount,
clearLamp: lamp,
missCount: result.missCount,
maxCombo: result.maxCombo,
stagesPlayed: result.stagesPlayed,
trophy: result.trophy ?? null,
trophyRank,
stages: result.stages.map(toCourseStageFigures),
gaugeType: result.gauge.type,
gaugeFinal: result.gauge.final,
judges: { ...result.judge, fast: result.fast, slow: result.slow },
outcome: truncated ? 'truncated' : null,
}),
),
// An unranked run moves no course best.
...(unranked !== undefined ? [] : ranked).map(({ ruleset, update, trophy, playedHere }) => {
const row = toCourseBestScoreRow(
{ userId: input.userId, courseFingerprint, ruleset, assisted },
update.next,
trophy.next,
playedHere,
now,
);
return db
.insert(courseBestScore)
.values(row)
.onConflictDoUpdate({
target: [
courseBestScore.userId,
courseBestScore.courseFingerprint,
courseBestScore.ruleset,
courseBestScore.assisted,
],
set: row,
});
}),
];
await db.batch(statements as [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]]);
const results = await Promise.all(
figures.map(async ({ ruleset, result, truncated, lamp, update, trophy }): Promise<CourseRulesetOutcome> => {
if (unranked !== undefined || truncated) {
return {
...unrankedCourseOutcome({ ...result, ruleset, rulesetRevision: result.ruleset, clearLamp: lamp }),
...(truncated ? { outcome: 'truncated' as const } : {}),
};
}
const filter = { courseFingerprint, ruleset, assisted, includeAnonymous: true, playedHereOnly: false };
const [rank, players] = await Promise.all([
rankOfCourseExScore(db, filter, update.next.bestEx),
countCoursePlayers(db, filter),
]);
return {
ruleset,
rulesetRevision: result.ruleset,
exScore: result.exScore,
noteCount: result.noteCount,
clearLamp: lamp,
missCount: result.missCount,
maxCombo: result.maxCombo,
stagesPlayed: result.stagesPlayed,
trophy: result.trophy ?? null,
rank,
players,
improved: { ...update.improved, trophy: trophy.improved },
};
}),
);
return {
status: 'accepted',
courseScoreId,
courseFingerprint,
results,
...(notSimulated.length > 0 ? { notSimulated } : {}),
...(unranked !== undefined ? { unranked } : {}),
};
}
/** The outcome of an unranked run under one ruleset: its figures, with no rank, players or best moved. */
function unrankedCourseOutcome(figures: {
ruleset: RulesetId;
rulesetRevision: string;
exScore: number;
noteCount: number;
clearLamp: number;
missCount: number;
maxCombo: number;
stagesPlayed: number;
trophy?: string | null;
}): CourseRulesetOutcome {
return {
ruleset: figures.ruleset,
rulesetRevision: figures.rulesetRevision,
exScore: figures.exScore,
noteCount: figures.noteCount,
clearLamp: figures.clearLamp,
missCount: figures.missCount,
maxCombo: figures.maxCombo,
stagesPlayed: figures.stagesPlayed,
trophy: figures.trophy ?? null,
rank: 0,
players: 0,
improved: { exScore: false, lamp: false, missCount: false, trophy: false },
};
}
/**
* What identifies a course run regardless of who recorded it or when: the course and each played stage as
* `identifyPlay` identifies a single play. Its hash is the course submission id.
*/
export function identifyCourseRun(
courseFingerprint: string,
playedLogs: readonly BeMusicReplay[],
stages: ReadonlyArray<{ playFingerprint: string }>,
): string {
return JSON.stringify({
courseFingerprint,
stages: playedLogs.map((replay, index) => identifyPlay(replay, stages[index]!.playFingerprint)),
});
}
/** {@link identifyCourseRun} for a NONSTOP run: the course and its stitched play, as `identifyPlay` identifies it. */
export function identifyNonstopRun(courseFingerprint: string, replay: BeMusicReplay, playFingerprint: string): string {
return JSON.stringify({ courseFingerprint, nonstop: identifyPlay(replay, playFingerprint) });
}
function stageOf(stage: number | undefined): { stage?: number } {
return stage === undefined ? {} : { stage };
}
/** The new charts among the stages, once each (a course can play one chart twice). */
function uniqueRegistrations(playedCharts: readonly PlayedChart[]): PlayedChart[] {
const seen = new Set<string>();
return playedCharts.filter((played) => {
if (played.registration === undefined || seen.has(played.chartFingerprint)) return false;
seen.add(played.chartFingerprint);
return true;
});
}
/** The outcome of an already stored run, for a resubmission. Ranks reflect the current standings. */
async function loadCourseOutcomes(
db: Database,
courseScoreId: string,
courseFingerprint: string,
assisted: boolean,
unranked: string | null,
): Promise<CourseRulesetOutcome[]> {
const rows = await db.select().from(courseScoreResult).where(eq(courseScoreResult.courseScoreId, courseScoreId));
const outcomes = await Promise.all(
rows.map(async (row): Promise<CourseRulesetOutcome> => {
Iif (unranked !== null || row.outcome !== null) {
return { ...unrankedCourseOutcome(row), ...(row.outcome === 'truncated' ? { outcome: row.outcome } : {}) };
}
const filter = {
courseFingerprint,
ruleset: row.ruleset,
assisted,
includeAnonymous: true,
playedHereOnly: false,
};
const [rank, players] = await Promise.all([
rankOfCourseExScore(db, filter, row.exScore),
countCoursePlayers(db, filter),
]);
return {
ruleset: row.ruleset,
rulesetRevision: row.rulesetRevision,
exScore: row.exScore,
noteCount: row.noteCount,
clearLamp: row.clearLamp,
missCount: row.missCount,
maxCombo: row.maxCombo,
stagesPlayed: row.stagesPlayed,
trophy: row.trophy,
rank,
players,
improved: { exScore: false, lamp: false, missCount: false, trophy: false },
};
}),
);
return outcomes.sort(byRulesetOrder);
}
|