// The video page's "Frame scan" panel, as data (release 30, lane P): a // frame-scan sidecar → the phase strip, one small chart per tournament, the // events with links to the moment, the flags. Pure, so it is unit-tested and // the component only draws. import type { FrameScanRecord } from "yt-dlp-transcript-common/lib/frameScan-server"; import { clock, fmtNum } from "yt-dlp-transcript-common/lib/frames/types"; export type StripPart = { phase: string; leftPct: number; widthPct: number; label: string }; export type Spark = { label: string; points: string; min: number; max: number; last: number }; export type TournamentView = { key: string; name: string; span: string; sparks: Spark[]; facts: string[] }; export type EventView = { t: number; at: string; kind: string; detail: string; href: string | null; source: string }; export type CorrectionView = { t: number; at: string; what: string; note: string | null }; export type FrameScanView = { profile: string; summary: string; // The hero's screen name the scan was told (a re-derive may change it). hero: string | null; // A person's corrections of readings (release 32), by second. corrections: CorrectionView[]; strip: StripPart[]; tournaments: TournamentView[]; events: EventView[]; flags: string[]; stopped: string | null; }; /** The video's page on its platform at second `t`, when the platform takes a time. */ export function atTime(url: string | null | undefined, t: number): string | null { if (!url) return null; let u: URL; try { u = new URL(url); } catch { return null; } const s = Math.max(0, Math.floor(t)); const host = u.hostname.replace(/^www\./, ""); if (host === "youtube.com" || host === "m.youtube.com" || host === "youtu.be") u.searchParams.set("t", `${s}s`); else if (host === "rumble.com") u.searchParams.set("start", String(s)); else if (host === "twitch.tv") u.searchParams.set("t", `${Math.floor(s / 3600)}h${Math.floor((s % 3600) / 60)}m${s % 60}s`); else u.hash = `t=${s}`; return u.toString(); } const W = 240; const H = 40; // A series as an SVG polyline in a W×H box (a step line: a value holds until the next). function spark(label: string, pts: readonly (readonly [number, number, ...unknown[]])[], from: number, to: number): Spark | null { if (pts.length < 1) return null; const vs = pts.map((p) => p[1]); const min = Math.min(...vs); const max = Math.max(...vs); const span = Math.max(1, to - from); const x = (t: number) => (((t - from) / span) * W).toFixed(1); const y = (v: number) => (max === min ? H / 2 : H - ((v - min) / (max - min)) * H).toFixed(1); const out: string[] = []; pts.forEach((p, i) => { if (i > 0) out.push(`${x(p[0])},${y(pts[i - 1][1])}`); out.push(`${x(p[0])},${y(p[1])}`); }); out.push(`${x(to)},${y(pts[pts.length - 1][1])}`); return { label, points: out.join(" "), min, max, last: vs[vs.length - 1] }; } const SERIES_LABELS: Record = { stack: "Stack (chips)", stackBB: "Stack (big blinds)", playersLeft: "Players left", level: "Level" }; const FACT_LABELS: Record string> = { buyIn: (v) => `buy-in $${fmtNum(v)}`, totalPlayers: (v) => `field ${fmtNum(v)}`, prizePool: (v) => `prize pool $${fmtNum(v)}`, firstPrize: (v) => `first prize $${fmtNum(v)}`, payout: (v) => `payout $${fmtNum(v)}`, position: (v) => `placed ${fmtNum(v)}`, averageStack: (v) => `average stack ${fmtNum(v)}`, }; export function frameScanView(r: FrameScanRecord, webpageUrl: string | null): FrameScanView { const end = r.source.durationSeconds ?? r.sampling.to; const total = Math.max(1, end); const strip = r.derived.segments.map((s) => ({ phase: s.phase, leftPct: Math.round((s.from / total) * 10_000) / 100, widthPct: Math.max(0.2, Math.round(((s.to - s.from) / total) * 10_000) / 100), label: `${s.phase} ${clock(s.from)}–${clock(s.to)}`, })); const tournaments = r.derived.tournaments.map((t) => ({ key: t.key, name: t.name ?? t.key, span: `${clock(t.from)}–${clock(t.to)}`, sparks: ["stack", "playersLeft"].map((f) => spark(SERIES_LABELS[f] ?? f, t.series[f] ?? [], t.from, t.to)).filter((s): s is Spark => s !== null), facts: Object.entries(t.facts).flatMap(([k, v]) => (FACT_LABELS[k] ? [FACT_LABELS[k](v)] : [])), })); // Every pot the hero takes is an event; the list shows the big ones and counts the rest. const pots = r.derived.events.filter((e) => e.kind === "pot").length; const events = r.derived.events.filter((e) => e.kind !== "pot").map((e) => ({ t: e.t, at: clock(e.t), kind: e.kind, detail: e.detail, href: atTime(webpageUrl, e.t), source: e.source, })); const flags = r.derived.flags.map((f) => `${clock(f.t)} ${SERIES_LABELS[f.field] ?? f.field}: said ${fmtNum(f.spoken)}, the frame at ${clock(f.frameT)} reads ${fmtNum(f.frame)}`); const last = r.runs[r.runs.length - 1]; const summary = `${fmtNum(r.sampling.frames)} frames every ${r.sampling.everySeconds} s, ${fmtNum(r.readings.length)} read` + ` (${fmtNum(r.sampling.skippedDuplicates)} duplicates skipped) on ${r.model.model} · $${r.costUsd.toFixed(2)}` + (r.derived.misreads.length ? ` · ${r.derived.misreads.length} misread${r.derived.misreads.length === 1 ? "" : "s"} set aside` : "") + (pots ? ` · ${pots} smaller pot${pots === 1 ? "" : "s"} won` : "") + (r.rederivedAt ? ` · re-derived ${r.rederivedAt.slice(0, 16).replace("T", " ")}` : ""); const corrections = (r.corrections ?? []).map((c) => ({ t: c.t, at: clock(c.t), what: c.misread ? "misread, set aside" : Object.entries(c.set ?? {}) .map(([k, v]) => `${k} ${v === null ? "not shown" : Array.isArray(v) ? v.join(" ") : typeof v === "number" ? fmtNum(v) : v}`) .join(", "), note: c.note ?? null, })); return { profile: r.profile, summary, hero: r.hints?.hero ?? null, corrections, strip, tournaments, events, flags, stopped: last?.stopped ?? null, }; }