227 lines
6.4 KiB
TypeScript
227 lines
6.4 KiB
TypeScript
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">;
|
|
|
|
const KOREAN_WEEKDAYS = [
|
|
"일",
|
|
"월",
|
|
"화",
|
|
"수",
|
|
"목",
|
|
"금",
|
|
"토",
|
|
] as const;
|
|
|
|
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 shouldDisplaySeriesHistoryScore(
|
|
series: CompetitionSeriesHistorySeries,
|
|
): boolean {
|
|
if (series.status === "in-progress") return true;
|
|
if (series.outcome.type === "played") return true;
|
|
return (
|
|
series.outcome.type === "draw" && series.outcome.basis.type === "played"
|
|
);
|
|
}
|
|
|
|
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)}`;
|
|
}
|
|
|
|
export function formatCompetitionHistoryDate(dateKey: string): string {
|
|
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateKey);
|
|
if (!match) return "일시 정보 없음";
|
|
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));
|
|
if (
|
|
date.getUTCFullYear() !== year ||
|
|
date.getUTCMonth() !== month - 1 ||
|
|
date.getUTCDate() !== day
|
|
) {
|
|
return "일시 정보 없음";
|
|
}
|
|
return `${padNumber(month)}월 ${padNumber(day)}일 (${KOREAN_WEEKDAYS[date.getUTCDay()]})`;
|
|
}
|
|
|
|
export function formatCompetitionHistoryTime(
|
|
value: IsoDateTime,
|
|
timeZone = "Asia/Seoul",
|
|
): string | null {
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return null;
|
|
const parts = new Intl.DateTimeFormat("en-US", {
|
|
timeZone,
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
hourCycle: "h23",
|
|
}).formatToParts(date);
|
|
const part = (type: Intl.DateTimeFormatPartTypes) =>
|
|
parts.find((entry) => entry.type === type)?.value;
|
|
const hour = part("hour");
|
|
const minute = part("minute");
|
|
return hour && minute ? `${hour}:${minute}` : null;
|
|
}
|
|
|
|
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");
|
|
}
|