import type { components } from "../api/schema"; export type WinReason = components["schemas"]["MatchFinish"]["win_reason"]; export const LAST_STANDING: WinReason = "last_standing"; // Причины для ручного выбора, фиксированный порядок (сверху вниз). «Последний выживший» // сюда не входит: она ставится сама, когда невыбывший игрок остался один. export const WIN_REASONS: { code: WinReason; label: string }[] = [ { code: "objectives", label: "По целям" }, { code: "worlds", label: "По мирам" }, { code: "plastic", label: "По пластику" }, { code: "resources", label: "По ресурсам" }, ]; const LABELS: Record = { ...Object.fromEntries(WIN_REASONS.map((w) => [w.code, w.label])), [LAST_STANDING]: "Последний выживший", }; export function winReasonLabel(code: string | null | undefined): string { return code ? LABELS[code] ?? code : "—"; } /** * Причина победы после изменения состава выбывших. Правило сервера: невыбывший ровно * один ⇔ «последний выживший». Поэтому при одном выжившем причина ставится сама, а когда * выживших снова двое — сбрасывается (null): её нужно выбрать заново, а не угадывать. */ export function reasonForSurvivors(current: WinReason | null, survivors: number): WinReason | null { if (survivors === 1) return LAST_STANDING; return current === LAST_STANDING ? null : current; }