feat: unify competition schedule and results

This commit is contained in:
dahyeon committed 2026-07-15 01:01:49 -07:00
1 parent 4e10998794
commit c9140b8622
12 files changed
+910 -245

No files matched your search

@@ -13,6 +13,12 @@ export type SeriesRule =
drawPolicy: 'allow' | 'tiebreak-required';
};
export interface SeriesSchedule {
date: string;
time: string | null;
timeZone: 'Asia/Seoul';
}
export interface SeriesDefinitionLike {
id: string;
code?: string;
@@ -26,6 +32,7 @@ export interface SeriesDefinitionLike {
source: Readonly<Record<string, unknown>>;
}>;
rule: SeriesRule;
schedule?: SeriesSchedule;
gameSlots: ReadonlyArray<{
id: string;
seriesId: string;
@@ -75,6 +82,7 @@ interface SeriesProjectionBase {
code?: string;
stage: SeriesDefinitionLike['stage'];
rule: SeriesRule;
schedule?: SeriesSchedule;
entries: [SeriesProjectionEntry, SeriesProjectionEntry];
}
@@ -311,6 +319,15 @@ function validateDefinition(definition: SeriesDefinitionLike): void {
`Series ${definition.id} must define exactly the local A and B slots`,
);
const schedule: unknown = definition.schedule;
if (schedule !== undefined) {
assertCompetitionInvariant(
isSeriesSchedule(schedule),
'SERIES_DEFINITION_INVALID',
`Series ${definition.id} has an invalid schedule`,
);
}
const requiredSlots =
definition.rule.type === 'fixed-games'
? definition.rule.gameCount
@@ -753,6 +770,7 @@ function projectionBase(
...(definition.code ? { code: definition.code } : {}),
stage: definition.stage,
rule: definition.rule,
...(definition.schedule ? { schedule: { ...definition.schedule } } : {}),
entries: [
{ slot: 'A', teamId: participants[0], gameWins: 0 },
{ slot: 'B', teamId: participants[1], gameWins: 0 },
@@ -760,6 +778,35 @@ function projectionBase(
};
}
function isValidLocalDate(value: string): boolean {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (!match) return false;
const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
const date = new Date(Date.UTC(year, month - 1, day));
return (
date.getUTCFullYear() === year &&
date.getUTCMonth() === month - 1 &&
date.getUTCDate() === day
);
}
function isSeriesSchedule(value: unknown): value is SeriesSchedule {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false;
}
const schedule = value as Record<string, unknown>;
return (
typeof schedule.date === 'string' &&
isValidLocalDate(schedule.date) &&
(schedule.time === null ||
(typeof schedule.time === 'string' &&
/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(schedule.time))) &&
schedule.timeZone === 'Asia/Seoul'
);
}
function entriesFor(
participants: [string, string],
winsByTeam: Map<string, number>,