test: define competition series history
This commit is contained in:
1 parent
075fb52848
commit
9f698a8fec
4 files changed
+900
No files matched your search
@@ -0,0 +1,170 @@
|
||||
import type {
|
||||
CompetitionGameView,
|
||||
CompetitionProjection,
|
||||
IsoDateTime,
|
||||
SeriesProjection,
|
||||
} from "@lolympus/contracts";
|
||||
|
||||
export type CompetitionSeriesHistorySeries = Extract<
|
||||
SeriesProjection,
|
||||
{ status: "in-progress" | "completed" }
|
||||
>;
|
||||
|
||||
export interface CompetitionSeriesHistoryRow {
|
||||
series: CompetitionSeriesHistorySeries;
|
||||
games: CompetitionGameView[];
|
||||
occurredAt: IsoDateTime;
|
||||
timestampSource: "game-start" | "decision";
|
||||
}
|
||||
|
||||
export interface CompetitionSeriesHistoryDay {
|
||||
dateKey: string;
|
||||
rows: CompetitionSeriesHistoryRow[];
|
||||
}
|
||||
|
||||
type HistoryProjection = Pick<CompetitionProjection, "series" | "games">;
|
||||
|
||||
export function createCompetitionSeriesHistory(
|
||||
projection: HistoryProjection,
|
||||
timeZone = "Asia/Seoul",
|
||||
): CompetitionSeriesHistoryDay[] {
|
||||
const gamesBySeriesId = new Map<string, CompetitionGameView[]>();
|
||||
for (const game of projection.games) {
|
||||
const seriesGames = gamesBySeriesId.get(game.seriesId) ?? [];
|
||||
seriesGames.push(game);
|
||||
gamesBySeriesId.set(game.seriesId, seriesGames);
|
||||
}
|
||||
|
||||
const rows: CompetitionSeriesHistoryRow[] = [];
|
||||
for (const series of projection.series) {
|
||||
const games = [...(gamesBySeriesId.get(series.seriesId) ?? [])].sort(
|
||||
compareGames,
|
||||
);
|
||||
if (series.status === "in-progress") {
|
||||
const occurredAt = earliestStartedAt(games);
|
||||
if (!occurredAt) continue;
|
||||
rows.push({
|
||||
series,
|
||||
games,
|
||||
occurredAt,
|
||||
timestampSource: "game-start",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (series.status !== "completed") continue;
|
||||
|
||||
const gameStartedAt = earliestStartedAt(games);
|
||||
rows.push({
|
||||
series,
|
||||
games,
|
||||
occurredAt: gameStartedAt ?? series.outcome.completedAt,
|
||||
timestampSource: gameStartedAt ? "game-start" : "decision",
|
||||
});
|
||||
}
|
||||
|
||||
rows.sort(compareRows);
|
||||
|
||||
const days: CompetitionSeriesHistoryDay[] = [];
|
||||
for (const row of rows) {
|
||||
const dateKey = dateKeyInTimeZone(row.occurredAt, timeZone);
|
||||
const currentDay = days.at(-1);
|
||||
if (currentDay?.dateKey === dateKey) {
|
||||
currentDay.rows.push(row);
|
||||
} else {
|
||||
days.push({ dateKey, rows: [row] });
|
||||
}
|
||||
}
|
||||
return days;
|
||||
}
|
||||
|
||||
export function getSeriesHistoryStatusLabel(
|
||||
series: CompetitionSeriesHistorySeries,
|
||||
): string {
|
||||
if (series.status === "in-progress") return "진행 중";
|
||||
switch (series.outcome.type) {
|
||||
case "played":
|
||||
return "종료";
|
||||
case "draw":
|
||||
return "무승부";
|
||||
case "walkover":
|
||||
return "부전승";
|
||||
case "forfeit":
|
||||
return "기권승";
|
||||
case "administrative":
|
||||
return "판정";
|
||||
}
|
||||
}
|
||||
|
||||
export function formatSeriesGameDuration(
|
||||
durationSeconds: number,
|
||||
): string | null {
|
||||
if (!Number.isFinite(durationSeconds) || durationSeconds <= 0) return null;
|
||||
const totalSeconds = Math.floor(durationSeconds);
|
||||
const hours = Math.floor(totalSeconds / 3_600);
|
||||
const minutes = Math.floor((totalSeconds % 3_600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
if (hours > 0) {
|
||||
return `${hours}:${padNumber(minutes)}:${padNumber(seconds)}`;
|
||||
}
|
||||
return `${minutes}:${padNumber(seconds)}`;
|
||||
}
|
||||
|
||||
function compareGames(
|
||||
left: CompetitionGameView,
|
||||
right: CompetitionGameView,
|
||||
): number {
|
||||
return (
|
||||
left.ordinal - right.ordinal ||
|
||||
timestampValue(left.startedAt) - timestampValue(right.startedAt) ||
|
||||
left.id.localeCompare(right.id)
|
||||
);
|
||||
}
|
||||
|
||||
function compareRows(
|
||||
left: CompetitionSeriesHistoryRow,
|
||||
right: CompetitionSeriesHistoryRow,
|
||||
): number {
|
||||
return (
|
||||
timestampValue(left.occurredAt) - timestampValue(right.occurredAt) ||
|
||||
left.series.stage.displayOrder - right.series.stage.displayOrder ||
|
||||
left.series.seriesId.localeCompare(right.series.seriesId)
|
||||
);
|
||||
}
|
||||
|
||||
function earliestStartedAt(games: CompetitionGameView[]): IsoDateTime | null {
|
||||
let earliest: { value: number; startedAt: IsoDateTime } | null = null;
|
||||
for (const game of games) {
|
||||
const value = timestampValue(game.startedAt);
|
||||
if (!Number.isFinite(value)) continue;
|
||||
if (!earliest || value < earliest.value) {
|
||||
earliest = { value, startedAt: game.startedAt };
|
||||
}
|
||||
}
|
||||
return earliest?.startedAt ?? null;
|
||||
}
|
||||
|
||||
function dateKeyInTimeZone(value: IsoDateTime, timeZone: string): string {
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return "unknown";
|
||||
const parts = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
}).formatToParts(date);
|
||||
const part = (type: Intl.DateTimeFormatPartTypes) =>
|
||||
parts.find((entry) => entry.type === type)?.value;
|
||||
const year = part("year");
|
||||
const month = part("month");
|
||||
const day = part("day");
|
||||
return year && month && day ? `${year}-${month}-${day}` : "unknown";
|
||||
}
|
||||
|
||||
function timestampValue(value: IsoDateTime): number {
|
||||
const timestamp = Date.parse(value);
|
||||
return Number.isNaN(timestamp) ? Number.POSITIVE_INFINITY : timestamp;
|
||||
}
|
||||
|
||||
function padNumber(value: number): string {
|
||||
return value.toString().padStart(2, "0");
|
||||
}
|
||||
Reference in new issue
Block a user