diff --git a/.env.example b/.env.example index 6a2d1ae82..521474a34 100644 --- a/.env.example +++ b/.env.example @@ -43,8 +43,6 @@ SQL_LOG=none VITE_SHOW_LUTI_NAV_ITEM=false -// If false the scanner page and its ingest endpoint are admin only -VITE_SCANNER_ENABLED=false // Push notification. Generate values here https://vapidkeys.com/ VITE_VAPID_PUBLIC_KEY= diff --git a/.gitignore b/.gitignore index 0125500da..cd23cf2d9 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ notepad.txt *.mp4 *.mkv *.webm -*.mov \ No newline at end of file +*.mov +!/e2e/fixtures/scanner-vod.webm \ No newline at end of file diff --git a/app/components/GameTimeline.module.css b/app/components/GameTimeline.module.css index 30e626b44..59eb89a89 100644 --- a/app/components/GameTimeline.module.css +++ b/app/components/GameTimeline.module.css @@ -101,11 +101,15 @@ .readoutStatusLabel { &.statusLabelDead { - color: var(--color-error); + color: var(--color-chart-splatted); } &.statusLabelSpecial { - color: var(--color-info); + color: var(--color-chart-special); + } + + &.statusLabelKill { + color: var(--color-chart-kill); } } diff --git a/app/components/GameTimeline.tsx b/app/components/GameTimeline.tsx index 71f2eefcb..5a31799f1 100644 --- a/app/components/GameTimeline.tsx +++ b/app/components/GameTimeline.tsx @@ -1,7 +1,7 @@ /** - * A game's two scanned-timeline charts (player status bands above the objective-counter chart) - * on one shared time axis. Hovering scrubs both: a cursor line spans the charts and a readout - * shows the moment's state, replacing the chart's own tooltip. + * A game's scanned-timeline charts (player status bands above the objective-counter chart, plus a + * TC/RM objective's position along its track) on one shared time axis. Hovering scrubs both: a cursor line spans the charts and a readout + * shows the moment's state and any kills under the cursor, replacing the chart's own tooltip. */ import clsx from "clsx"; import { memo, useRef, useState } from "react"; @@ -11,6 +11,7 @@ import type { MainWeaponId } from "~/modules/in-game-lists/types"; import { abilityImageUrl } from "~/utils/urls"; import styles from "./GameTimeline.module.css"; import { Image, WeaponImage } from "./Image"; +import { ObjectivePositionTimeline } from "./ObjectivePositionTimeline"; import { ObjectiveTimeline, type ObjectiveTimelineEvent, @@ -19,10 +20,13 @@ import { formatElapsed, smoothPenalties, TIMELINE_PLOT_GUTTER_PX, + withUnpushedTrackCounts, } from "./objective-timeline-utils"; import { PLAYER_STATUS_TAIL_SECONDS, PlayerStatusTimeline, + type PlayerStatusTimelineKill, + type PlayerStatusTimelinePov, type PlayerStatusTimelineSample, type PlayerStatusTimelineTeam, statusSpans, @@ -31,11 +35,14 @@ import { /** Cursor position past this fraction of the plot flips the readout to its left side. */ const READOUT_FLIP_RATIO = 0.55; const READOUT_CURSOR_GAP_PX = 12; +/** Kill ticks this close to the cursor show up in the readout. */ +const KILL_READOUT_RADIUS_PX = 4; interface GameTimelineProps { objectiveEvents?: readonly ObjectiveTimelineEvent[]; playerStatusSamples?: readonly PlayerStatusTimelineSample[]; teams: readonly [PlayerStatusTimelineTeam, PlayerStatusTimelineTeam]; + pov?: PlayerStatusTimelinePov; } interface ScrubPosition { @@ -52,13 +59,16 @@ export function GameTimeline({ objectiveEvents, playerStatusSamples, teams, + pov, }: GameTimelineProps) { const [scrub, setScrub] = useState(null); const plotRef = useRef(null); - const objective = (objectiveEvents ?? []).toSorted((a, b) => a.t - b.t); + const objective = withUnpushedTrackCounts( + (objectiveEvents ?? []).toSorted((a, b) => a.t - b.t), + ); const samples = (playerStatusSamples ?? []).toSorted((a, b) => a.t - b.t); - const domain = timelineDomain(objective, samples); + const domain = timelineDomain(objective, samples, pov); if (!domain) return null; const handlePointer = (event: React.PointerEvent) => { @@ -91,6 +101,7 @@ export function GameTimeline({ objectiveEvents={objectiveEvents} playerStatusSamples={playerStatusSamples} teams={teams} + pov={pov} />
{scrub ? ( @@ -100,6 +111,7 @@ export function GameTimeline({ objective={objective} samples={samples} teams={teams} + pov={samples.length > 0 ? pov : undefined} /> ) : null}
@@ -114,16 +126,24 @@ const TimelineCharts = memo(function TimelineCharts({ objectiveEvents, playerStatusSamples, teams, + pov, }: GameTimelineProps) { - const objective = (objectiveEvents ?? []).toSorted((a, b) => a.t - b.t); + const objective = withUnpushedTrackCounts( + (objectiveEvents ?? []).toSorted((a, b) => a.t - b.t), + ); const samples = (playerStatusSamples ?? []).toSorted((a, b) => a.t - b.t); - const domain = timelineDomain(objective, samples); + const domain = timelineDomain(objective, samples, pov); if (!domain) return null; return ( <> {samples.length > 0 ? ( - + ) : null} {objective.length > 0 ? ( ) : null} + {objective.some((event) => event.data.position !== undefined) ? ( + + ) : null} ); }); @@ -143,18 +166,24 @@ function ScrubReadout({ objective, samples, teams, + pov, }: { scrub: ScrubPosition; domain: [number, number]; objective: readonly ObjectiveTimelineEvent[]; samples: readonly PlayerStatusTimelineSample[]; teams: GameTimelineProps["teams"]; + pov?: PlayerStatusTimelinePov; }) { const { t } = useTranslation(["common"]); const [min, max] = domain; const time = min + (scrub.x / scrub.width) * (max - min); const objectiveNow = objectiveStateAt(objective, time); const statusNow = playerStatusAt(samples, time); + const killRadius = (KILL_READOUT_RADIUS_PX / scrub.width) * (max - min); + const killsNow = (pov?.kills ?? []).filter( + (kill) => Math.abs(kill.t - time) <= killRadius, + ); const flipped = scrub.x > scrub.width * READOUT_FLIP_RATIO; return ( @@ -208,11 +237,19 @@ function ScrubReadout({ })} ) : null} - {objectiveNow?.control[side] ? ( + {objectiveNow?.control === side ? ( {t("common:objectiveTimeline.inControl")} ) : null} + {objectiveNow?.position != null && + pushingSide(objectiveNow.position) === side ? ( + + {t("common:objectiveTimeline.pushed", { + value: Math.abs(objectiveNow.position), + })} + + ) : null} {statusNow ? ( <> @@ -230,6 +267,7 @@ function ScrubReadout({ /> ) : null} + {pov?.side === side ? : null} ))} @@ -284,19 +322,39 @@ function StatusWeaponsRow({ ); } +function KillsRow({ kills }: { kills: PlayerStatusTimelineKill[] }) { + const { t } = useTranslation(["common"]); + if (kills.length === 0) return null; + + return ( +
+ + {t("common:playerStatusTimeline.kill")} + + {kills.map((kill) => kill.name ?? "?").join(" · ")} +
+ ); +} + +/** Spans every read and, when the status rows draw them, the POV's kill ticks — the feed can outlive the other reads. */ function timelineDomain( objective: readonly ObjectiveTimelineEvent[], samples: readonly PlayerStatusTimelineSample[], + pov: PlayerStatusTimelinePov | undefined, ): [number, number] | null { + const killTimes = + samples.length > 0 ? (pov?.kills ?? []).map((kill) => kill.t) : []; const start = Math.min( objective[0]?.t ?? Number.POSITIVE_INFINITY, samples[0]?.t ?? Number.POSITIVE_INFINITY, + ...killTimes, ); const end = Math.max( objective[objective.length - 1]?.t ?? Number.NEGATIVE_INFINITY, samples.length > 0 ? samples[samples.length - 1]!.t + PLAYER_STATUS_TAIL_SECONDS : Number.NEGATIVE_INFINITY, + ...killTimes, ); if (!Number.isFinite(start) || !Number.isFinite(end)) return null; return [start, Math.max(end, start + 1)]; @@ -308,7 +366,9 @@ interface ObjectiveStateAtTime { /** last readable count per team at the scrubbed moment */ scores: [number | null, number | null]; penalties: [number | null, number | null]; - control: [boolean, boolean]; + control: 0 | 1 | null; + /** TC/RM: the last read objective position (see ObjectiveTimelineSample); null when none */ + position: number | null; } /** State implied by the last objective read at or before `time`, scores carried across unreadable reads. */ @@ -338,15 +398,26 @@ function objectiveStateAt( )[index] ?? null, ) as [number | null, number | null]; const latest = sorted[index]!; + let position: number | null = null; + for (let i = 0; i <= index; i++) { + position = sorted[i]!.data.position ?? position; + } return { clock: latest.data.time, scores, penalties, - control: [latest.data.control[0], latest.data.control[1]], + control: latest.data.control, + position, }; } +/** The side a track position is progress for: past the middle toward bravo's end is alpha's. */ +function pushingSide(position: number): 0 | 1 | null { + if (position === 0) return null; + return position > 0 ? 0 : 1; +} + interface PlayerStatusAtTime { /** slot indexes inside a splatted band at the scrubbed moment, per side */ dead: [number[], number[]]; diff --git a/app/components/ObjectivePositionTimeline.module.css b/app/components/ObjectivePositionTimeline.module.css new file mode 100644 index 000000000..c32904187 --- /dev/null +++ b/app/components/ObjectivePositionTimeline.module.css @@ -0,0 +1,61 @@ +.container { + display: flex; + height: 140px; + background-color: var(--color-bg-high); + border-radius: var(--radius-box); + padding: var(--s-2-5) var(--s-3); +} + +.plot { + position: relative; + flex: 1; + min-height: 0; +} + +.chart { + position: absolute; + inset: 0; + display: block; + width: 100%; + height: 100%; + overflow: visible; +} + +.gridLine { + stroke: color-mix(in oklch, var(--color-text) 8%, transparent); + stroke-width: 1; + + &.gridLineDashed { + stroke-dasharray: 3 4; + } + + &.gridLineZero { + stroke: color-mix(in oklch, var(--color-text) 25%, transparent); + } +} + +.label { + font-size: 10px; + font-weight: var(--weight-semi); + fill: var(--color-text-high); +} + +.area { + fill: currentColor; + fill-opacity: 0.45; +} + +.alpha { + color: var(--color-chart-alpha); +} + +.bravo { + color: var(--color-chart-bravo); +} + +.line { + fill: none; + stroke: var(--color-text-high); + stroke-width: 1.5; + stroke-linejoin: round; +} diff --git a/app/components/ObjectivePositionTimeline.tsx b/app/components/ObjectivePositionTimeline.tsx new file mode 100644 index 000000000..1522fe85f --- /dev/null +++ b/app/components/ObjectivePositionTimeline.tsx @@ -0,0 +1,148 @@ +/** + * Tug-of-war chart of a Tower Control / Rainmaker objective along its track: the middle line is + * the start, up is toward the end alpha pushes to and fills in alpha's color, down toward bravo's + * goal in bravo's. Shares the objective chart's x-axis and y-label gutter so both line up. + */ + +import clsx from "clsx"; +import { useElementSize } from "~/hooks/useElementSize"; +import styles from "./ObjectivePositionTimeline.module.css"; +import type { ObjectiveTimelineEvent } from "./ObjectiveTimeline"; +import { + TIMELINE_PLOT_GUTTER_PX, + timelineTimeTicks, +} from "./objective-timeline-utils"; + +const TRACK_END = 100; +const POSITION_TICKS = [TRACK_END, 0, -TRACK_END]; +const DEFAULT_WIDTH = 600; +const DEFAULT_HEIGHT = 116; +const PLOT_PADDING_Y_PX = 6; +const Y_LABEL_GAP_PX = 6; + +export function ObjectivePositionTimeline({ + events, + domain, +}: { + /** sorted by `t`; events without a position read are skipped */ + events: readonly ObjectiveTimelineEvent[]; + domain: [number, number]; +}) { + const { ref: measureRef, size } = useElementSize(); + + const reads = events.flatMap((event) => + event.data.position != null + ? [{ t: event.t, position: event.data.position }] + : [], + ); + if (reads.length === 0) return null; + + const { width, height } = size ?? { + width: DEFAULT_WIDTH, + height: DEFAULT_HEIGHT, + }; + const [xMin, xMax] = domain; + const xRange = Math.max(xMax - xMin, 1); + const plotLeft = TIMELINE_PLOT_GUTTER_PX; + const plotRight = width; + const plotTop = PLOT_PADDING_Y_PX; + const plotBottom = height - PLOT_PADDING_Y_PX; + const xAt = (time: number) => + plotLeft + ((time - xMin) / xRange) * (plotRight - plotLeft); + const yAt = (position: number) => + plotTop + + ((TRACK_END - position) / (2 * TRACK_END)) * (plotBottom - plotTop); + + const withCrossings = withZeroCrossings(reads); + const areaPath = (clamp: (position: number) => number) => + [ + `M${xAt(withCrossings[0]!.t).toFixed(1)} ${yAt(0).toFixed(1)}`, + ...withCrossings.map( + (read) => + `L${xAt(read.t).toFixed(1)} ${yAt(clamp(read.position)).toFixed(1)}`, + ), + `L${xAt(withCrossings[withCrossings.length - 1]!.t).toFixed(1)} ${yAt(0).toFixed(1)}`, + "Z", + ].join(" "); + + return ( +
+
+ +
+
+ ); +} + +/** Inserts a read at each point the line crosses the middle, so clamping either side to zero keeps the exact shape. */ +function withZeroCrossings(reads: Array<{ t: number; position: number }>) { + return reads.flatMap((read, i) => { + const previous = reads[i - 1]; + if (!previous || previous.position * read.position >= 0) return [read]; + + const ratio = previous.position / (previous.position - read.position); + return [ + { t: previous.t + (read.t - previous.t) * ratio, position: 0 }, + read, + ]; + }); +} diff --git a/app/components/ObjectiveTimeline.tsx b/app/components/ObjectiveTimeline.tsx index c203aba51..310235dd5 100644 --- a/app/components/ObjectiveTimeline.tsx +++ b/app/components/ObjectiveTimeline.tsx @@ -16,6 +16,7 @@ import { formatElapsed, smoothPenalties, TIMELINE_PLOT_GUTTER_PX, + timelineTimeTicks, } from "./objective-timeline-utils"; /** count-axis units of gutter kept below zero for the control lane */ @@ -28,11 +29,6 @@ const DEFAULT_HEIGHT = 240; const PLOT_TOP_PX = 6; const TIME_LABELS_HEIGHT_PX = 18; const Y_LABEL_GAP_PX = 6; -const MAX_TIME_TICKS = 8; -const PX_PER_TIME_TICK = 56; -const TIME_TICK_STEPS_SECONDS = [ - 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, -]; /** Time labels closer than this to a plot edge are aligned inwards so they stay inside the card */ const TIME_LABEL_EDGE_PX = 14; const TOOLTIP_CURSOR_GAP_PX = 12; @@ -49,8 +45,13 @@ export interface ObjectiveTimelineSample { score: [number | null, number | null]; /** penalty pill value per team; null = no pill (or unreadable) */ penalty: [number | null, number | null]; - /** which team held the objective at the read */ - control: [boolean, boolean]; + /** which team held the objective at the read; null = neither */ + control: 0 | 1 | null; + /** + * TC/RM: the objective along its track, -100..100, positive toward the end + * alpha pushes to; absent on SZ, null = unread + */ + position?: number | null; } export interface ObjectiveTimelineEvent { @@ -126,13 +127,10 @@ export function ObjectiveTimeline({ const countTicks = R.range(0, Math.floor(maxCount / COUNT_TICK_STEP) + 1).map( (multiple) => multiple * COUNT_TICK_STEP, ); - const timeTicks = timeTickValues({ + const timeTicks = timelineTimeTicks({ min: xMin, max: xMax, - maxCount: R.clamp(Math.floor((plotRight - plotLeft) / PX_PER_TIME_TICK), { - min: 2, - max: MAX_TIME_TICKS, - }), + plotWidth: plotRight - plotLeft, }); const handlePointerMove = (event: React.PointerEvent) => { @@ -319,7 +317,7 @@ export function ObjectiveTimeline({ value: penalty[side], }) : null, - control[side] + control === side ? t("common:objectiveTimeline.inControl") : null, ] @@ -446,7 +444,7 @@ function controlRuns( let current: { start: number; end: number } | null = null; for (const [i, event] of sorted.entries()) { const inLane = - event.data.control[side] || sorted[i - 1]?.data.control[side]; + event.data.control === side || sorted[i - 1]?.data.control === side; if (inLane) { current ??= { start: event.t, end: event.t }; current.end = event.t; @@ -460,26 +458,6 @@ function controlRuns( return runs; } -function timeTickValues({ - min, - max, - maxCount, -}: { - min: number; - max: number; - maxCount: number; -}) { - const range = Math.max(max - min, 1); - const step = - TIME_TICK_STEPS_SECONDS.find( - (candidate) => range / candidate <= maxCount - 1, - ) ?? Math.ceil(range / (maxCount - 1) / 3600) * 3600; - - return R.range(Math.ceil(min / step), Math.floor(max / step) + 1).map( - (multiple) => multiple * step, - ); -} - function timeLabelAnchor({ x, plotLeft, diff --git a/app/components/PlayerStatusTimeline.module.css b/app/components/PlayerStatusTimeline.module.css index 789a7ab95..6fac6e9fc 100644 --- a/app/components/PlayerStatusTimeline.module.css +++ b/app/components/PlayerStatusTimeline.module.css @@ -22,18 +22,29 @@ } .legendSwatchDead, -.legendSwatchSpecial { +.legendSwatchSpecial, +.legendSwatchKill, +.legendSwatchSplattedByPov { width: 10px; height: 10px; border-radius: 2px; } .legendSwatchDead { - background-color: var(--color-error); + background-color: var(--color-chart-splatted); } .legendSwatchSpecial { - background-color: var(--color-info); + background-color: var(--color-chart-special); +} + +.legendSwatchKill { + width: 3px; + background-color: var(--color-chart-kill); +} + +.legendSwatchSplattedByPov { + background-color: var(--color-chart-kill); } .team { @@ -61,13 +72,25 @@ padding-right: var(--s-2); } -.track { +.trackArea { position: relative; flex: 1; +} + +.track { + position: relative; height: 14px; overflow: hidden; border-radius: var(--radius-selector); background-color: var(--color-bg-higher); + + &.trackPov { + background-color: color-mix( + in oklch, + var(--color-bg-higher) 75%, + var(--color-accent) 25% + ); + } } .spanDead, @@ -80,13 +103,26 @@ } .spanDead { - background-color: var(--color-error); - opacity: 0.75; + background-color: var(--color-chart-splatted); + + &.spanSplattedByPov { + background-color: var(--color-chart-kill); + } } .spanSpecial { - background-color: var(--color-info); - opacity: 0.75; + background-color: var(--color-chart-special); +} + +.killTick { + position: absolute; + top: -3px; + bottom: -3px; + width: 3px; + transform: translateX(-50%); + border-radius: 2px; + background-color: var(--color-chart-kill); + box-shadow: 0 0 0 1px var(--color-bg-high); } .unknownWeapon { diff --git a/app/components/PlayerStatusTimeline.tsx b/app/components/PlayerStatusTimeline.tsx index bb6c78bc4..1ea4033f4 100644 --- a/app/components/PlayerStatusTimeline.tsx +++ b/app/components/PlayerStatusTimeline.tsx @@ -2,8 +2,10 @@ * Per-player splatted / special-held bands over a game's scanned icon-strip reads, rendered above * the ObjectiveTimeline on the same `t` axis (pass `domain` to share the range). Reads re-confirm * an unchanged state every few seconds; a longer gap means the HUD was not observed, so bands never - * bridge across one. + * bridge across one. A known POV player gets a highlighted row with their kills as ticks, and the + * splatted bands those kills opened stand out on the victims' rows. */ +import clsx from "clsx"; import { useTranslation } from "react-i18next"; import type { MainWeaponId } from "~/modules/in-game-lists/types"; import { abilityImageUrl } from "~/utils/urls"; @@ -20,6 +22,16 @@ const MAX_BRIDGE_SECONDS = 15; /** Trailing open band drawn this long past its last confirming read. */ export const PLAYER_STATUS_TAIL_SECONDS = 1; +/** + * The kill-feed row and the victim's crossed-out icon show together, but either read can land a + * beat late (an ink burst hiding the feed, the strip's ~1s cadence): the victim's splatted band + * opening within this much of the kill is that kill's. + */ +const KILL_SPLAT_MAX_LEAD_SECONDS = 4; +const KILL_SPLAT_MAX_LAG_SECONDS = 3; + +const PLAYER_SLOTS = [0, 1, 2, 3] as const; + type PlayerFlags = readonly [boolean, boolean, boolean, boolean]; /** One icon-strip read, sides in `[alpha, bravo]` order. */ @@ -36,15 +48,33 @@ export interface PlayerStatusTimelineTeam { weapons: (MainWeaponId | null)[]; } +/** The player whose point of view the footage is, with their kills. */ +export interface PlayerStatusTimelinePov { + side: 0 | 1; + slot: number; + kills: readonly PlayerStatusTimelineKill[]; +} + +export interface PlayerStatusTimelineKill { + /** seconds on the same axis as the samples */ + t: number; + /** the splatted player's name; null when unknown */ + name: string | null; + /** the splatted player's row on the other side; null when unknown */ + victimSlot: number | null; +} + export function PlayerStatusTimeline({ samples, teams, domain, + pov, }: { samples: readonly PlayerStatusTimelineSample[]; teams: readonly [PlayerStatusTimelineTeam, PlayerStatusTimelineTeam]; /** x-axis range override, to share the objective chart's axis */ domain?: [number, number]; + pov?: PlayerStatusTimelinePov; }) { const { t } = useTranslation(["common"]); const sorted = samples.toSorted((a, b) => a.t - b.t); @@ -56,11 +86,17 @@ export function PlayerStatusTimeline({ sorted[sorted.length - 1]!.t + PLAYER_STATUS_TAIL_SECONDS, ); const range = Math.max(1, max - min); - const leftOf = (span: StatusSpan) => `${((span.start - min) / range) * 100}%`; + const leftOf = (at: number) => `${((at - min) / range) * 100}%`; const widthOf = (span: StatusSpan) => `${((span.end - span.start) / range) * 100}%`; const titleOf = (label: string, span: StatusSpan) => `${label} · ${formatElapsed(span.start)}–${formatElapsed(span.end)}`; + const isPovRow = (side: 0 | 1, slot: number) => + pov?.side === side && pov.slot === slot; + const killsOfVictim = (side: 0 | 1, slot: number) => + pov && pov.side !== side + ? pov.kills.filter((kill) => kill.victimSlot === slot) + : []; return (
{t("common:playerStatusTimeline.specialReady")} + {pov && pov.kills.length > 0 ? ( + + + {t("common:playerStatusTimeline.kill")} + + ) : null} + {pov?.kills.some((kill) => kill.victimSlot !== null) ? ( + + + {t("common:playerStatusTimeline.splattedByPov")} + + ) : null}
{([0, 1] as const).map((side) => (
{teams[side].label}
- {[0, 1, 2, 3].map((slot) => ( -
-
- -
-
- {statusSpans(sorted, (sample) => sample.dead[side][slot]!).map( - (span, i) => ( -
- ), - )} - {statusSpans( - sorted, - (sample) => sample.special[side][slot]!, - ).map((span, i) => ( + {PLAYER_SLOTS.map((slot) => { + const deadSpans = statusSpans( + sorted, + (sample) => sample.dead[side][slot]!, + ); + const splattedByPov = spansOpenedByKills( + deadSpans, + killsOfVictim(side, slot), + ); + return ( +
+
+ +
+
- ))} + className={clsx(styles.track, { + [styles.trackPov]: isPovRow(side, slot), + })} + > + {deadSpans.map((span, i) => ( +
+ ))} + {statusSpans( + sorted, + (sample) => sample.special[side][slot]!, + ).map((span, i) => ( +
+ ))} +
+ {isPovRow(side, slot) + ? pov!.kills.map((kill, i) => ( +
+ )) + : null} +
-
- ))} + ); + })}
))}
@@ -145,6 +227,23 @@ interface StatusSpan { end: number; } +/** Indexes of the spans each kill opened: the latest one opening inside the kill's window. */ +function spansOpenedByKills( + spans: readonly StatusSpan[], + kills: readonly PlayerStatusTimelineKill[], +): Set { + const opened = new Set(); + for (const kill of kills) { + const index = spans.findLastIndex( + (span) => + span.start >= kill.t - KILL_SPLAT_MAX_LEAD_SECONDS && + span.start <= kill.t + KILL_SPLAT_MAX_LAG_SECONDS, + ); + if (index !== -1) opened.add(index); + } + return opened; +} + /** * Contiguous stretches where the flag held true: opens at the first true read, closes at the read * showing false, or one second past the last confirmation when the next read is too far away. diff --git a/app/components/layout/Footer.tsx b/app/components/layout/Footer.tsx index 6622ed3e3..0bf715163 100644 --- a/app/components/layout/Footer.tsx +++ b/app/components/layout/Footer.tsx @@ -37,6 +37,7 @@ import { PLANNER_URL, PLUS_SUGGESTIONS_PAGE, PLUS_VOTING_RESULTS_PAGE, + SCANNER_PAGE, SENDOU_INK_DISCORD_URL, SENDOU_INK_GITHUB_URL, SENDOU_LOVE_EMOJI_PATH, @@ -110,6 +111,7 @@ const SITEMAP_COLUMNS = [ { name: "maps", url: MAPS_URL }, { name: "tier-list-maker", url: TIER_LIST_MAKER_URL }, { name: "xsearch", url: XSEARCH_PAGE }, + { name: "scanner", url: SCANNER_PAGE }, { name: "settings", url: SETTINGS_PAGE }, ], }, diff --git a/app/components/layout/TopNavMenus.tsx b/app/components/layout/TopNavMenus.tsx index b83f6ff13..772509cfa 100644 --- a/app/components/layout/TopNavMenus.tsx +++ b/app/components/layout/TopNavMenus.tsx @@ -51,6 +51,7 @@ const NAV_CATEGORIES = [ { name: "maps", url: "maps" }, { name: "tier-list-maker", url: "tier-list-maker" }, { name: "xsearch", url: "xsearch" }, + { name: "scanner", url: "scanner" }, { name: "admin", url: "admin", diff --git a/app/components/layout/nav-items.ts b/app/components/layout/nav-items.ts index 3d6b92e9f..8b36d2f66 100644 --- a/app/components/layout/nav-items.ts +++ b/app/components/layout/nav-items.ts @@ -71,6 +71,10 @@ export const navItems = [ name: "vods", url: "vods", }, + { + name: "scanner", + url: "scanner", + }, { name: "art", url: "art", diff --git a/app/components/match-page/ingested-scoreboard.ts b/app/components/match-page/ingested-scoreboard.ts index b18531549..90238e151 100644 --- a/app/components/match-page/ingested-scoreboard.ts +++ b/app/components/match-page/ingested-scoreboard.ts @@ -112,7 +112,16 @@ function toTimelineObjective( time: sample.time, score: alphaFirst(sample.score), penalty: alphaFirst(sample.penalty), - control: alphaFirst(sample.control), + control: + alphaIsWinner || sample.control === null + ? sample.control + : sample.control === 0 + ? 1 + : 0, + position: + !alphaIsWinner && sample.position != null + ? 0 - sample.position + : sample.position, }, })); } diff --git a/app/components/objective-timeline-utils.test.ts b/app/components/objective-timeline-utils.test.ts index cbeb1e7c7..827ed00b9 100644 --- a/app/components/objective-timeline-utils.test.ts +++ b/app/components/objective-timeline-utils.test.ts @@ -4,6 +4,7 @@ import { type ObjectiveScoreRead, type PenaltyRead, smoothPenalties, + withUnpushedTrackCounts, } from "./objective-timeline-utils"; function reads(...pairs: Array<[t: number, penalty: number | null]>) { @@ -103,3 +104,37 @@ describe("matchScoresFromObjective", () => { expect(matchScoresFromObjective([])).toEqual([null, null]); }); }); + +describe("withUnpushedTrackCounts", () => { + const trackEvent = (alpha: number | null, bravo: number | null) => ({ + data: { + score: [alpha, bravo] as [number | null, number | null], + position: 0, + }, + }); + + test("shows a full count until a side's first plate read", () => { + const scores = withUnpushedTrackCounts([ + trackEvent(null, null), + trackEvent(90, null), + trackEvent(null, 70), + trackEvent(null, null), + ]).map((event) => event.data.score); + + expect(scores).toEqual([ + [100, 100], + [90, 100], + [null, 70], + [null, null], + ]); + }); + + test("leaves splat zones counts alone", () => { + const events = [ + { data: { score: [null, null] as [number | null, number | null] } }, + { data: { score: [90, 80] as [number | null, number | null] } }, + ]; + + expect(withUnpushedTrackCounts(events)).toEqual(events); + }); +}); diff --git a/app/components/objective-timeline-utils.ts b/app/components/objective-timeline-utils.ts index 6a682a94f..a8da7aa8f 100644 --- a/app/components/objective-timeline-utils.ts +++ b/app/components/objective-timeline-utils.ts @@ -1,4 +1,11 @@ +import * as R from "remeda"; + const PENALTY_BRIDGE_SECONDS = 6; +const MAX_TIME_TICKS = 8; +const PX_PER_TIME_TICK = 56; +const TIME_TICK_STEPS_SECONDS = [ + 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, +]; /** Label gutter left of the plot, shared by the objective chart's y-axis and the player-status weapon column so both span the same x-range. */ export const TIMELINE_PLOT_GUTTER_PX = 36; @@ -86,6 +93,66 @@ export function matchScoresFromObjective( return [lastCountTaken(0), lastCountTaken(1)]; } +interface TrackCountEvent { + data: { + score: [number | null, number | null]; + position?: number | null; + }; +} + +/** + * TC/RM draw no count plate for a team that never pushed past the middle, so + * a side's reads before its first count showed its full count, not an + * unreadable one. SZ events (no `position`) pass through untouched. + * + * @param sorted events sorted by `t` ascending; result is index-aligned + */ +export function withUnpushedTrackCounts( + sorted: readonly T[], +): T[] { + if (!sorted.some((event) => event.data.position !== undefined)) { + return [...sorted]; + } + const firstRead = ([0, 1] as const).map((side) => + sorted.findIndex((event) => event.data.score[side] !== null), + ); + return sorted.map((event, i) => { + const score = event.data.score.map((value, side) => { + const first = firstRead[side]!; + return value === null && (first === -1 || i < first) ? FULL_COUNT : value; + }) as [number | null, number | null]; + return { ...event, data: { ...event.data, score } }; + }); +} + +/** + * Round-numbered time-axis ticks between `min` and `max` (seconds), spaced to fit `plotWidth`. + * Shared so stacked timelines draw their gridlines at the same times. + */ +export function timelineTimeTicks({ + min, + max, + plotWidth, +}: { + min: number; + max: number; + plotWidth: number; +}) { + const maxCount = R.clamp(Math.floor(plotWidth / PX_PER_TIME_TICK), { + min: 2, + max: MAX_TIME_TICKS, + }); + const range = Math.max(max - min, 1); + const step = + TIME_TICK_STEPS_SECONDS.find( + (candidate) => range / candidate <= maxCount - 1, + ) ?? Math.ceil(range / (maxCount - 1) / 3600) * 3600; + + return R.range(Math.ceil(min / step), Math.floor(max / step) + 1).map( + (multiple) => multiple * step, + ); +} + /** Seconds formatted as m:ss, with an hours part only when needed. */ export function formatElapsed(seconds: number): string { const hours = Math.floor(seconds / 3600); diff --git a/app/config.ts b/app/config.ts index ef9659684..6e9a06940 100644 --- a/app/config.ts +++ b/app/config.ts @@ -32,7 +32,6 @@ const schema = v.object({ VITE_PROD_MODE: v.optional(envBoolean, "false"), VITE_SHOW_LUTI_NAV_ITEM: v.optional(envBoolean, "false"), VITE_FUSE_ENABLED: v.optional(envBoolean, "false"), - VITE_SCANNER_ENABLED: v.optional(envBoolean, "false"), VITE_LEAGUE_GOOGLE_FORM_URL: v.optional(v.string()), VITE_SHOW_BANNER_FOR_SEASON: v.optional(v.string()), @@ -57,8 +56,6 @@ export const Config = { prodMode: values.VITE_PROD_MODE, showLutiNavItem: values.VITE_SHOW_LUTI_NAV_ITEM, fuseEnabled: values.VITE_FUSE_ENABLED, - /** While false only the admin and devs can use the scanner page and its ingest endpoint. */ - scannerEnabled: values.VITE_SCANNER_ENABLED, leagueGoogleFormUrl: values.VITE_LEAGUE_GOOGLE_FORM_URL, /** Season to show the registration banner for. */ showBannerForSeason: values.VITE_SHOW_BANNER_FOR_SEASON, diff --git a/app/db/tables.ts b/app/db/tables.ts index d48add5e6..dd9a8a1ad 100644 --- a/app/db/tables.ts +++ b/app/db/tables.ts @@ -557,6 +557,8 @@ export interface ReportedWeapon { userId: number; weaponSplId: MainWeaponId; createdAt: Generated; + /** the scanner read this weapon was reported from, null when reported by hand */ + ingestedMatchId: number | null; } export interface IngestedMatch { diff --git a/app/features/admin/admin-constants.ts b/app/features/admin/admin-constants.ts index 415684dda..32f8f19ef 100644 --- a/app/features/admin/admin-constants.ts +++ b/app/features/admin/admin-constants.ts @@ -9,9 +9,6 @@ export const STAFF_IDS = [11329, 9719, 9342, 20774, 23094]; export const DEV_IDS = [27883]; // hfcRed Dreamy Cafy Acing export const QA_IDS: number[] = [27883, 38176, 10654, 9342]; -// xi -/** Users who can access the scanner while it is not yet enabled for everyone */ -export const SCANNER_TESTER_IDS: number[] = [30228]; export const STAFF_DISCORD_IDS = [ "138757634500067328", diff --git a/app/features/changelog/changelog-constants.ts b/app/features/changelog/changelog-constants.ts index 5ebfed60a..fad179bad 100644 --- a/app/features/changelog/changelog-constants.ts +++ b/app/features/changelog/changelog-constants.ts @@ -31,6 +31,7 @@ export const DISCORD_EMOJI_NAMES: Record = { associations: "associations", badges: "badges", luti: "luti", + scanner: "scanner", }; /** Stands in for the sendou.ink logo, used by entries without a nav item. */ diff --git a/app/features/components-showcase/routes/components.colors.tsx b/app/features/components-showcase/routes/components.colors.tsx index 282efca09..fecffbc22 100644 --- a/app/features/components-showcase/routes/components.colors.tsx +++ b/app/features/components-showcase/routes/components.colors.tsx @@ -353,6 +353,10 @@ const REFERENCE = [ tokens: ["--color-chart-alpha", "--color-chart-bravo"], use: "The two sides in charts and timelines", }, + { + tokens: ["--color-chart-splatted", "--color-chart-special"], + use: "Player states in timelines, kept apart from the side colors", + }, ] as const; export default function ComponentsColorsPage() { diff --git a/app/features/match-page-test/routes/match-page-test.tsx b/app/features/match-page-test/routes/match-page-test.tsx index ce9f619b5..fc67db012 100644 --- a/app/features/match-page-test/routes/match-page-test.tsx +++ b/app/features/match-page-test/routes/match-page-test.tsx @@ -27,7 +27,7 @@ import type { PlayerStatusTimelineSample } from "~/components/PlayerStatusTimeli import { logger } from "~/utils/logger"; import type { SendouRouteHandle } from "~/utils/remix.server"; -/** Counter reads of a made-up zones game, for previewing the timeline chart. */ +/** Counter reads of a made-up Rainmaker game, for previewing the timeline charts. */ const MOCK_OBJECTIVE_EVENTS = mockObjectiveEvents(); /** Icon-strip reads of the same made-up game, for previewing the status bands. */ @@ -864,41 +864,37 @@ export default function MatchPageTestRoute() { } /** - * Zones game second by second: the controlling side burns its penalty before its count moves, - * and losing the zone after counting hands it a penalty to burn next time. + * Rainmaker game second by second: the carrier's team drags the objective toward the other end, + * a dropped Rainmaker sits neutral where it fell, and each team's count only falls past its best + * push so far. */ function mockObjectiveEvents(): ObjectiveTimelineEvent[] { - const PHASES: Array<{ seconds: number; control: [boolean, boolean] }> = [ - { seconds: 12, control: [false, false] }, - { seconds: 30, control: [true, false] }, - { seconds: 14, control: [false, false] }, - { seconds: 44, control: [false, true] }, - { seconds: 10, control: [false, false] }, - { seconds: 80, control: [true, false] }, + const PHASES: Array<{ + seconds: number; + holder: 0 | 1 | null; + speed: number; + }> = [ + { seconds: 12, holder: null, speed: 0 }, + { seconds: 20, holder: 0, speed: 2 }, + { seconds: 14, holder: null, speed: 0 }, + { seconds: 40, holder: 1, speed: -2 }, + { seconds: 10, holder: null, speed: 0 }, + { seconds: 45, holder: 0, speed: 2 }, + { seconds: 20, holder: null, speed: 0 }, + { seconds: 29, holder: 1, speed: -2 }, ]; - const PENALTY_ON_LOSING_ZONE = 12; const SAMPLE_EVERY_SECONDS = 2; - const score: [number, number] = [100, 100]; - const penalty: [number, number] = [0, 0]; + const best: [number, number] = [0, 0]; const events: ObjectiveTimelineEvent[] = []; - let previousControl: [boolean, boolean] = [false, false]; + let position = 0; let t = 0; for (const phase of PHASES) { - for (const side of [0, 1] as const) { - if (previousControl[side] && !phase.control[side]) { - penalty[side] += PENALTY_ON_LOSING_ZONE; - } - } - previousControl = phase.control; - for (let second = 0; second < phase.seconds; second++) { - for (const side of [0, 1] as const) { - if (!phase.control[side]) continue; - if (penalty[side] > 0) penalty[side] -= 1; - else score[side] = Math.max(0, score[side] - 1); - } + position = Math.max(-100, Math.min(100, position + phase.speed)); + best[0] = Math.max(best[0], position); + best[1] = Math.max(best[1], -position); t += 1; if (t % SAMPLE_EVERY_SECONDS !== 0) continue; @@ -906,9 +902,13 @@ function mockObjectiveEvents(): ObjectiveTimelineEvent[] { t, data: { time: 300 - t, - score: [score[0], score[1]], - penalty: [penalty[0] || null, penalty[1] || null], - control: [phase.control[0], phase.control[1]], + score: [ + best[0] > 0 ? 100 - best[0] : null, + best[1] > 0 ? 100 - best[1] : null, + ], + penalty: [null, null], + control: phase.holder, + position, }, }); } diff --git a/app/features/scanner-ingest/ScannerIngestRepository.server.test.ts b/app/features/scanner-ingest/ScannerIngestRepository.server.test.ts index e437fbd74..3e33947e9 100644 --- a/app/features/scanner-ingest/ScannerIngestRepository.server.test.ts +++ b/app/features/scanner-ingest/ScannerIngestRepository.server.test.ts @@ -186,9 +186,9 @@ describe("addLinks", () => { expect(await fetchLinks()).toHaveLength(2); }); - test("reports the POV player's weapon once", async () => { - const povUser = await UserFactory.create(); - const { match: groupMatch, maps } = await setupSendouqMatch(); + test("reports the POV player's weapon once, tagged with its scan and dated to the game", async () => { + const { match: groupMatch, maps, users } = await setupSendouqMatch(); + const povUser = users[0]!; const { effectiveMatches } = await ScannerIngestRepository.addOrMergeMatches({ @@ -201,7 +201,9 @@ describe("addLinks", () => { { ingestedMatchId: effectiveMatches[0].id, match: effectiveMatches[0].data, - game: sendouqGame(maps[0]), + game: sendouqGame(maps[0], { + winnerUserIds: users.slice(0, FULL_GROUP_SIZE).map((user) => user.id), + }), }, ]; @@ -215,6 +217,75 @@ describe("addLinks", () => { expect(reportedWeapons[0].mapIndex).toBe(maps[0].index); expect(reportedWeapons[0].userId).toBe(povUser.id); expect(reportedWeapons[0].weaponSplId).toBe(WEAPONS[0]); + expect(reportedWeapons[0].ingestedMatchId).toBe(effectiveMatches[0].id); + expect(reportedWeapons[0].createdAt).toBe(Math.floor(PLAYED_AT / 1000)); + }); + + test("reports no weapon for a sender outside the game's rosters", async () => { + const outsider = await UserFactory.create(); + const { maps, users } = await setupSendouqMatch(); + + const { effectiveMatches } = + await ScannerIngestRepository.addOrMergeMatches({ + povUserId: outsider.id, + submitterUserId: outsider.id, + matches: [testMatch({ pov: { team: 0, index: 0 } })], + context: null, + }); + + const linkedCount = await ScannerIngestRepository.addLinks({ + links: [ + { + ingestedMatchId: effectiveMatches[0].id, + match: effectiveMatches[0].data, + game: sendouqGame(maps[0], { + winnerUserIds: users + .slice(0, FULL_GROUP_SIZE) + .map((user) => user.id), + }), + }, + ], + povUserId: outsider.id, + }); + + expect(linkedCount).toBe(1); + expect(await fetchReportedWeapons()).toHaveLength(0); + }); + + test("deleting the ingested match deletes the weapon reported from it", async () => { + const { maps, users } = await setupSendouqMatch(); + const povUser = users[0]!; + + const { effectiveMatches } = + await ScannerIngestRepository.addOrMergeMatches({ + povUserId: povUser.id, + submitterUserId: povUser.id, + matches: [testMatch({ pov: { team: 0, index: 0 } })], + context: null, + }); + await ScannerIngestRepository.addLinks({ + links: [ + { + ingestedMatchId: effectiveMatches[0].id, + match: effectiveMatches[0].data, + game: sendouqGame(maps[0], { + winnerUserIds: users + .slice(0, FULL_GROUP_SIZE) + .map((user) => user.id), + }), + }, + ], + povUserId: povUser.id, + }); + expect(await fetchReportedWeapons()).toHaveLength(1); + + // biome-ignore lint/plugin: no production path deletes ingested matches yet + await db + .deleteFrom("IngestedMatch") + .where("id", "=", effectiveMatches[0].id) + .execute(); + + expect(await fetchReportedWeapons()).toHaveLength(0); }); }); @@ -345,7 +416,7 @@ async function setupSendouqMatch(options: { isConcluded?: boolean } = {}) { .orderBy("index", "asc") .execute(); - return { match, maps }; + return { match, maps, users }; } function fetchIngestedMatches() { @@ -368,13 +439,16 @@ function fetchReportedWeapons() { return db.selectFrom("ReportedWeapon").selectAll().execute(); } -function sendouqGame(map: { - id: number; - matchId: number; - index: number; - mode: IngestableGame["mode"]; - stageId: IngestableGame["stageId"]; -}): IngestableGame { +function sendouqGame( + map: { + id: number; + matchId: number; + index: number; + mode: IngestableGame["mode"]; + stageId: IngestableGame["stageId"]; + }, + rosters: Partial> = {}, +): IngestableGame { return { target: { type: "sendouq", @@ -388,7 +462,9 @@ function sendouqGame(map: { loserUserIds: [], winnerInGameNames: [], loserInGameNames: [], + inGameNameByUserId: new Map(), playedAt: Math.floor(PLAYED_AT / 1000), linkedPlayerNames: null, + ...rosters, }; } diff --git a/app/features/scanner-ingest/ScannerIngestRepository.server.ts b/app/features/scanner-ingest/ScannerIngestRepository.server.ts index dbd6c1250..f70e2dffc 100644 --- a/app/features/scanner-ingest/ScannerIngestRepository.server.ts +++ b/app/features/scanner-ingest/ScannerIngestRepository.server.ts @@ -448,7 +448,8 @@ export async function addOrMergeMatches({ /** * Links ingested matches to their matched game results. A row links to at most one game (re-sends * are no-ops); a game collects links from many rows (each POV's scan). A known POV player's - * weapon is reported as a regular ReportedWeapon unless they already have one for that game. + * weapon (Scoreboards.povWeaponId) is reported as a ReportedWeapon tagged with its ingested match, + * unless they already have one for that game. * * @returns count of newly created links */ @@ -494,6 +495,61 @@ export async function addLinks({ }); } +/** Stored matches hinted to the context and played since `playedSince` that no game links yet, chronological. */ +export function findUnlinkedMatchesByHint({ + context, + playedSince, +}: { + context: IngestContext; + /** database timestamp (seconds) */ + playedSince: number; +}) { + return db + .selectFrom("IngestedMatch") + .select([ + "IngestedMatch.id", + "IngestedMatch.data", + "IngestedMatch.povUserId", + ]) + .where( + context.type === "tournament" + ? "IngestedMatch.tournamentIdHint" + : "IngestedMatch.groupMatchIdHint", + "=", + context.type === "tournament" + ? context.tournamentId + : context.groupMatchId, + ) + .where("IngestedMatch.playedAt", ">=", playedSince) + .where((eb) => + eb.not( + eb.exists( + eb + .selectFrom("IngestedMatchLink") + .select("IngestedMatchLink.id") + .whereRef( + "IngestedMatchLink.ingestedMatchId", + "=", + "IngestedMatch.id", + ), + ), + ), + ) + .orderBy("IngestedMatch.playedAt", "asc") + .execute(); +} + +/** Unlinks every ingested match from a SendouQ map, as when its report is undone. */ +export async function deleteLinksByGroupMatchMapId( + groupMatchMapId: number, + trx?: Transaction, +) { + await (trx ?? db) + .deleteFrom("IngestedMatchLink") + .where("IngestedMatchLink.groupMatchMapId", "=", groupMatchMapId) + .execute(); +} + async function addOrMergeMatch( trx: Transaction, { @@ -782,6 +838,10 @@ async function tournamentGames({ loserUserIds: loserRoster?.userIds ?? [], winnerInGameNames: winnerRoster?.inGameNames ?? [], loserInGameNames: loserRoster?.inGameNames ?? [], + inGameNameByUserId: new Map([ + ...(winnerRoster?.inGameNameByUserId ?? []), + ...(loserRoster?.inGameNameByUserId ?? []), + ]), playedAt: row.playedAt, linkedPlayerNames: linkedNames.get(row.matchGameResultId) ?? null, }; @@ -791,6 +851,7 @@ async function tournamentGames({ interface Roster { userIds: number[]; inGameNames: string[]; + inGameNameByUserId: Map; } async function teamRosters(teamIds: Array) { @@ -814,18 +875,22 @@ async function teamRosters(teamIds: Array) { const result = new Map(); for (const member of members) { - const roster = result.get(member.tournamentTeamId) ?? { - userIds: [], - inGameNames: [], - }; + const roster = result.get(member.tournamentTeamId) ?? emptyRoster(); roster.userIds.push(member.userId); - if (member.inGameName) roster.inGameNames.push(member.inGameName); + if (member.inGameName) { + roster.inGameNames.push(member.inGameName); + roster.inGameNameByUserId.set(member.userId, member.inGameName); + } result.set(member.tournamentTeamId, roster); } return result; } +function emptyRoster(): Roster { + return { userIds: [], inGameNames: [], inGameNameByUserId: new Map() }; +} + async function sendouqGames({ groupMatchId, userId, @@ -918,6 +983,10 @@ async function sendouqGames({ loserUserIds: loserRoster?.userIds ?? [], winnerInGameNames: winnerRoster?.inGameNames ?? [], loserInGameNames: loserRoster?.inGameNames ?? [], + inGameNameByUserId: new Map([ + ...(winnerRoster?.inGameNameByUserId ?? []), + ...(loserRoster?.inGameNameByUserId ?? []), + ]), playedAt: row.playedAt, linkedPlayerNames: linkedNames.get(row.groupMatchMapId) ?? null, }; @@ -937,19 +1006,19 @@ async function groupRosters(groupIds: number[]) { const result = new Map(); for (const member of members) { - const roster = result.get(member.groupId) ?? { - userIds: [], - inGameNames: [], - }; + const roster = result.get(member.groupId) ?? emptyRoster(); roster.userIds.push(member.userId); - if (member.inGameName) roster.inGameNames.push(member.inGameName); + if (member.inGameName) { + roster.inGameNames.push(member.inGameName); + roster.inGameNameByUserId.set(member.userId, member.inGameName); + } result.set(member.groupId, roster); } return result; } -/** Winner-first player names of each game's earliest linked ingest, keyed by the link target column's value. */ +/** Winner-first player names of each game's earliest linked ingest with recognizable names, keyed by the link target column's value. */ async function linkedPlayerNamesByTarget( column: "tournamentMatchGameResultId" | "groupMatchMapId", targetIds: number[], @@ -972,7 +1041,7 @@ async function linkedPlayerNamesByTarget( for (const row of rows) { if (row.targetId === null || result.has(row.targetId)) continue; - const names = Scoreboards.winnerFirstPlayerNames(row.data); + const names = Scoreboards.recognizablePlayerNames(row.data); if (names) result.set(row.targetId, names); } @@ -981,12 +1050,15 @@ async function linkedPlayerNamesByTarget( async function reportPovWeapon( trx: Transaction, - { match, game }: { match: ScannerMatch; game: IngestableGame }, + { + ingestedMatchId, + match, + game, + }: { ingestedMatchId: number; match: ScannerMatch; game: IngestableGame }, povUserId: number | null, ) { - if (povUserId === null || match.pov === null) return; - const weaponSplId = - match.teams[match.pov.team]?.players[match.pov.index]?.weaponId ?? null; + if (povUserId === null) return; + const weaponSplId = Scoreboards.povWeaponId({ match, game, povUserId }); if (weaponSplId === null) return; await trx @@ -1001,6 +1073,9 @@ async function reportPovWeapon( mapIndex: game.mapIndex, userId: povUserId, weaponSplId, + ingestedMatchId, + // season stats bucket by it, and a scan can arrive long after the game + createdAt: game.playedAt, }) .onConflict((oc) => oc diff --git a/app/features/scanner-ingest/actions/scanner-ingest.server.ts b/app/features/scanner-ingest/actions/scanner-ingest.server.ts index 9b98f0d5f..5a109b81b 100644 --- a/app/features/scanner-ingest/actions/scanner-ingest.server.ts +++ b/app/features/scanner-ingest/actions/scanner-ingest.server.ts @@ -1,12 +1,14 @@ import { subDays } from "date-fns"; import type { ActionFunction } from "react-router"; -import { Config } from "~/config"; import { requireUser } from "~/features/auth/core/user.server"; import type { ScannerMatch } from "~/features/scanner/core/scanner-match"; -import { isAdmin, isDev, isScannerTester } from "~/modules/permissions/utils"; +import { + isLinkableLobby, + isUploadedLobby, +} from "~/features/scanner/scanner-types"; import { dateToDatabaseTimestamp } from "~/utils/dates"; import { logger } from "~/utils/logger"; -import { forbidden, parseBody } from "~/utils/remix.server"; +import { parseBody } from "~/utils/remix.server"; import * as Scoreboards from "../core/Scoreboards"; import * as ScannerIngestRepository from "../ScannerIngestRepository.server"; import { @@ -21,27 +23,19 @@ const CONTENT_RESOLUTION_WINDOW_DAYS = 365; export const action: ActionFunction = async ({ request }) => { const user = requireUser(); - if ( - !Config.scannerEnabled && - !isAdmin(user) && - !isDev(user) && - !isScannerTester(user) - ) { - forbidden(); - } - const data = await parseBody({ request, schema: ingestBodySchema }); const povUserId = user.id; - const indexedMatches = data.matches - .map((match, requestIndex) => ({ - match: withoutDisprovenCast(match), - requestIndex, - })) - .filter(({ match }) => match.lobby === null || match.lobby === "PRIVATE"); + const requestMatches = data.matches.map(withoutDisprovenCast); + const indexedMatches = requestMatches + .map((match, requestIndex) => ({ match, requestIndex })) + .filter(({ match }) => isLinkableLobby(match.lobby)); const matches = indexedMatches.map(({ match }) => match); - if (matches.length === 0) { + const storeOnlyMatches = requestMatches.filter( + (match) => isUploadedLobby(match.lobby) && !isLinkableLobby(match.lobby), + ); + if (matches.length === 0 && storeOnlyMatches.length === 0) { return { storedMatchesCount: 0, mergedMatchesCount: 0, @@ -51,19 +45,31 @@ export const action: ActionFunction = async ({ request }) => { } satisfies IngestResponse; } - const resolved = await resolveIngestContext({ - matches, + const storedOnly = await ScannerIngestRepository.addOrMergeMatches({ povUserId, - casterUserId: user.id, + submitterUserId: user.id, + matches: storeOnlyMatches, + context: null, }); - const { insertedCount, mergedCount, effectiveMatches } = - await ScannerIngestRepository.addOrMergeMatches({ - povUserId, - submitterUserId: user.id, - matches, - context: resolved?.context ?? null, - }); + const resolved = + matches.length > 0 + ? await resolveIngestContext({ + matches, + povUserId, + casterUserId: user.id, + }) + : null; + + const linkable = await ScannerIngestRepository.addOrMergeMatches({ + povUserId, + submitterUserId: user.id, + matches, + context: resolved?.context ?? null, + }); + const { effectiveMatches } = linkable; + const insertedCount = storedOnly.insertedCount + linkable.insertedCount; + const mergedCount = storedOnly.mergedCount + linkable.mergedCount; let linkedGamesCount = 0; let linkedMatches: IngestResponse["linkedMatches"] = []; @@ -140,8 +146,8 @@ interface IngestContextCandidate { * activity around play time is the strong signal: their match running then (for cast footage, * the casted sets of tournaments they help run). Candidates are scored by how many matches would * link, but kept even when nothing links yet (a live minimap-only match still gets its hint). - * Without activity the content decides: mode+stage sequence plus roster sides is near-unique in - * a user's history. + * Without activity (a set that outlasted the activity window) the user's history decides: the + * context whose games were reported around the matches' play times. */ async function resolveIngestContext({ matches, @@ -294,7 +300,7 @@ function withoutDisprovenCast(match: ScannerMatch): ScannerMatch { return { ...match, cast: false }; } -/** When the request's matches were probably played: the latest playedAt, else "now". */ +/** When the request's matches were probably played: the latest playedAt, else "now" (live reads without a scoreboard). */ function anchorTime(matches: ScannerMatch[]): number { const playedAts = matches .map((match) => match.playedAt) diff --git a/app/features/scanner-ingest/core/Matches.test.ts b/app/features/scanner-ingest/core/Matches.test.ts index 341f852af..968590d59 100644 --- a/app/features/scanner-ingest/core/Matches.test.ts +++ b/app/features/scanner-ingest/core/Matches.test.ts @@ -21,7 +21,7 @@ describe("canonicalMatch", () => { time: 215, score: [95, 53], penalty: [4, null], - control: [true, false], + control: 0, }, ], }, @@ -92,6 +92,48 @@ describe("isSameMatch", () => { expect(Matches.isSameMatch(a, b)).toBe(true); }); + test("close play times do not identify back-to-back games against different opponents", () => { + const playedAt = 1_700_000_000_000; + const nextSet = scannerMatch({ playedAt: playedAt + 5 * 60 * 1000 }); + nextSet.teams[1] = { + players: ["o1", "o2", "o3", "o4"].map((name, i) => + scannerMatchPlayer(name, (100 + 10 * i) as MainWeaponId), + ), + }; + expect(Matches.isSameMatch(scannerMatch({ playedAt }), nextSet)).toBe( + false, + ); + }); + + test("close play times still identify a match with a couple of misread names per team", () => { + const playedAt = 1_700_000_000_000; + const b = scannerMatch({ playedAt: playedAt + 60 * 1000 }); + b.teams[0].players[0] = scannerMatchPlayer("misread1", WEAPONS[0]!); + b.teams[0].players[1] = scannerMatchPlayer("misread2", WEAPONS[1]!); + b.teams[1].players[2] = scannerMatchPlayer("misread3", WEAPONS[6]!); + expect(Matches.isSameMatch(scannerMatch({ playedAt }), b)).toBe(true); + }); + + test("contradicting weapons rule out close play times when names are unread", () => { + const playedAt = 1_700_000_000_000; + const minimap = scannerMatch({ + playedAt: playedAt + 5 * 60 * 1000, + teams: [ + { + players: WEAPONS.slice(0, 4).map((w) => scannerMatchPlayer(null, w)), + }, + { + players: [100, 110, 120, 130].map((w) => + scannerMatchPlayer(null, w as MainWeaponId), + ), + }, + ], + }); + expect(Matches.isSameMatch(scannerMatch({ playedAt }), minimap)).toBe( + false, + ); + }); + test("far-apart play times contradict identity even with equal rosters", () => { const a = scannerMatch({ playedAt: 1_700_000_000_000 }); const b = scannerMatch({ playedAt: 1_700_000_000_000 + 60 * 60 * 1000 }); @@ -113,6 +155,15 @@ describe("isSameMatch", () => { ).toBe(false); }); + test("differing lobbies contradict identity", () => { + expect( + Matches.isSameMatch( + scannerMatch({ lobby: "PRIVATE" }), + scannerMatch({ lobby: "X" }), + ), + ).toBe(false); + }); + test("a null mode does not contradict a read one", () => { expect( Matches.isSameMatch( @@ -197,6 +248,34 @@ describe("mergeMatches", () => { expect(merged.teams[0].players[0]!.name).toBe("w1"); }); + test("flips a side-swapped track position back with its sides", () => { + const existing = scannerMatch({ objective: null }); + const incoming = sideSwapped( + scannerMatch({ + objective: { + mode: "TC", + samples: [ + { + t: 120, + time: 215, + score: [100, 60], + penalty: [null, null], + control: 1, + position: -30, + }, + ], + }, + }), + ); + + const { merged } = Matches.mergeMatches(existing, incoming); + expect(merged.objective!.samples[0]).toMatchObject({ + score: [100, 60], + control: 1, + position: -30, + }); + }); + test("aligns a side-swapped incoming match before merging", () => { const existing = scannerMatch({ winner: null, matchScores: null }); const incoming = sideSwapped( diff --git a/app/features/scanner-ingest/core/Matches.ts b/app/features/scanner-ingest/core/Matches.ts index 299a2afe7..e67f0ccb2 100644 --- a/app/features/scanner-ingest/core/Matches.ts +++ b/app/features/scanner-ingest/core/Matches.ts @@ -22,6 +22,11 @@ const MIN_NAME_OVERLAP = 6; const MIN_WEAPON_OVERLAP = 7; const MIN_WEAPON_SLOTS_READ = 7; +/** A team pair with at least this many names (else weapons) read on both sides can contradict identity... */ +const MIN_SLOTS_READ_TO_CONTRADICT = 3; +/** ...when at most this many of them align: different players, so a different game. */ +const MAX_CONTRADICTING_OVERLAP = 1; + const PLAYERS_PER_TEAM = 4; /** Rebuilds a match with a fixed key order so `JSON.stringify` is stable — the hashing and change-detection representation. */ @@ -65,10 +70,12 @@ export function canonicalMatch(match: ScannerMatch): ScannerMatch { /** * Whether two (possibly partial) matches describe the same game; callers pre-scope to the same - * tournament + POV user. Contradicting mode/stage/replay-code/play-time rules identity out; then a - * matching replay code, close play times, or an aligning roster (names, else weapons) rules it in. + * tournament + POV user. Contradicting lobby/mode/stage/replay-code/play-time rules identity out; then a + * matching replay code rules it in, contradicting rosters (a team pair of different players) rule it out, + * and close play times or an aligning roster (names, else weapons) rule it in. */ export function isSameMatch(a: ScannerMatch, b: ScannerMatch): boolean { + if (a.lobby !== null && b.lobby !== null && a.lobby !== b.lobby) return false; if (a.mode !== null && b.mode !== null && a.mode !== b.mode) return false; if (a.stage !== null && b.stage !== null && a.stage !== b.stage) return false; @@ -84,14 +91,16 @@ export function isSameMatch(a: ScannerMatch, b: ScannerMatch): boolean { } if (codeDiff !== null) return true; - if (playedDiff !== null && playedDiff <= PLAYED_AT_AFFINITY_MS) return true; const aligned = bestAlignment(a, b); + if (rostersContradict(a, b, aligned.orientation)) return false; + if (playedDiff !== null && playedDiff <= PLAYED_AT_AFFINITY_MS) return true; + if (aligned.nameOverlap >= MIN_NAME_OVERLAP) return true; if ( aligned.weaponOverlap >= MIN_WEAPON_OVERLAP && - weaponSlotsRead(a) >= MIN_WEAPON_SLOTS_READ && - weaponSlotsRead(b) >= MIN_WEAPON_SLOTS_READ + matchWeaponSlotsRead(a) >= MIN_WEAPON_SLOTS_READ && + matchWeaponSlotsRead(b) >= MIN_WEAPON_SLOTS_READ ) { return true; } @@ -161,7 +170,8 @@ function canonicalObjective( time: sample.time, score: [sample.score[0], sample.score[1]], penalty: [sample.penalty[0], sample.penalty[1]], - control: [sample.control[0], sample.control[1]], + control: sample.control, + ...(sample.position !== undefined ? { position: sample.position } : null), })), }; } @@ -266,12 +276,47 @@ function weaponOverlap(a: ScannerMatchTeam, b: ScannerMatchTeam): number { return overlap; } -function weaponSlotsRead(match: ScannerMatch): number { - return match.teams.flatMap((team) => - team.players.filter((player) => player.weaponId !== null), +function rostersContradict( + a: ScannerMatch, + b: ScannerMatch, + orientation: Alignment["orientation"], +): boolean { + const [bFirst, bSecond] = + orientation === "straight" + ? [b.teams[0], b.teams[1]] + : [b.teams[1], b.teams[0]]; + return ( + teamsContradict(a.teams[0], bFirst) || teamsContradict(a.teams[1], bSecond) + ); +} + +function teamsContradict(a: ScannerMatchTeam, b: ScannerMatchTeam): boolean { + if (Math.min(namesRead(a), namesRead(b)) >= MIN_SLOTS_READ_TO_CONTRADICT) { + return nameOverlap(a, b) <= MAX_CONTRADICTING_OVERLAP; + } + if ( + Math.min(weaponSlotsRead(a), weaponSlotsRead(b)) >= + MIN_SLOTS_READ_TO_CONTRADICT + ) { + return weaponOverlap(a, b) <= MAX_CONTRADICTING_OVERLAP; + } + return false; +} + +function namesRead(team: ScannerMatchTeam): number { + return team.players.filter( + (player) => player.name && normalizeInGameName(player.name), ).length; } +function weaponSlotsRead(team: ScannerMatchTeam): number { + return team.players.filter((player) => player.weaponId !== null).length; +} + +function matchWeaponSlotsRead(match: ScannerMatch): number { + return weaponSlotsRead(match.teams[0]) + weaponSlotsRead(match.teams[1]); +} + function swapSides(match: ScannerMatch): ScannerMatch { return { ...match, @@ -294,7 +339,11 @@ function swapSides(match: ScannerMatch): ScannerMatch { ...sample, score: [sample.score[1], sample.score[0]], penalty: [sample.penalty[1], sample.penalty[0]], - control: [sample.control[1], sample.control[0]], + control: + sample.control === null ? null : sample.control === 0 ? 1 : 0, + ...(sample.position != null + ? { position: 0 - sample.position } + : null), })), }, playerStatus: diff --git a/app/features/scanner-ingest/core/Scoreboards.test.ts b/app/features/scanner-ingest/core/Scoreboards.test.ts index ccde711e8..49773006b 100644 --- a/app/features/scanner-ingest/core/Scoreboards.test.ts +++ b/app/features/scanner-ingest/core/Scoreboards.test.ts @@ -34,6 +34,7 @@ function testGame( loserUserIds: [], winnerInGameNames: [], loserInGameNames: [], + inGameNameByUserId: new Map(), playedAt: 1000, linkedPlayerNames: null, ...rest, @@ -53,7 +54,7 @@ function tournamentMatchIdOf(matched: Scoreboards.MatchedGame): number | null { } function testMatch({ - t = 60, + playedAt = 1000, mode = "SZ", stage = 0, lobby = "PRIVATE", @@ -64,7 +65,8 @@ function testMatch({ objective = null, playerStatus = null, }: { - t?: number; + /** database timestamp (seconds) like the games' `playedAt`; null for a read without a wall clock (VoD) */ + playedAt?: number | null; mode?: ModeShort | null; stage?: StageId | null; lobby?: ScannerLobby | null; @@ -87,9 +89,9 @@ function testMatch({ }), ); return { - startsAt: t, - endsAt: t, - playedAt: null, + startsAt: 60, + endsAt: 360, + playedAt: playedAt === null ? null : playedAt * 1000, lobby, mode, stage, @@ -117,14 +119,30 @@ function testObjective(): ScannerMatchObjective { time: 300, score: [100, 100], penalty: [null, null], - control: [false, false], + control: null, }, { t: 630, time: 270, score: [80, 100], penalty: [null, 12], - control: [true, false], + control: 0, + }, + ], + }; +} + +function testTrackObjective(): ScannerMatchObjective { + return { + mode: "RM", + samples: [ + { + t: 600, + time: 300, + score: [75, null], + penalty: [null, null], + control: 0, + position: 25, }, ], }; @@ -163,7 +181,11 @@ function swapSides(match: ScannerMatch): ScannerMatch { ...sample, score: [sample.score[1], sample.score[0]], penalty: [sample.penalty[1], sample.penalty[0]], - control: [sample.control[1], sample.control[0]], + control: + sample.control === null ? null : sample.control === 0 ? 1 : 0, + ...(sample.position != null + ? { position: 0 - sample.position } + : null), })), }, playerStatus: @@ -256,7 +278,7 @@ describe("matchedGames", () => { games: [ testGame({ matchGameResultId: 11, - linkedPlayerNames: ["", "", "", "", "l1", "l2", "l3", "l4"], + linkedPlayerNames: ["", "", "w3", "w4", "l1", "l2", "l3", "l4"], }), testGame({ matchGameResultId: 12, playedAt: 2000 }), ], @@ -267,7 +289,7 @@ describe("matchedGames", () => { test("matches matches to games by mode and stage", () => { const matched = Scoreboards.matchedGames({ - matches: [testMatch({ mode: "RM", stage: 1, t: 60 })], + matches: [testMatch({ mode: "RM", stage: 1 })], games: [ testGame({ mapIndex: 0, mode: "SZ", stageId: 0 as StageId }), testGame({ mapIndex: 1, mode: "RM", stageId: 1 as StageId }), @@ -281,11 +303,11 @@ describe("matchedGames", () => { const matched = Scoreboards.matchedGames({ matches: [ testMatch({ - t: 60, + playedAt: 1000, names: ["a", "b", "c", "d", "e", "f", "g", "h"], }), testMatch({ - t: 5000, + playedAt: 2000, names: ["i", "j", "k", "l", "m", "n", "o", "p"], }), ], @@ -303,7 +325,7 @@ describe("matchedGames", () => { test("skips duplicate detections of the same game", () => { const matched = Scoreboards.matchedGames({ - matches: [testMatch({ t: 60 }), testMatch({ t: 65 })], + matches: [testMatch(), testMatch({ playedAt: 1005 })], games: [ testGame({ tournamentMatchId: 1, playedAt: 1000 }), testGame({ tournamentMatchId: 2, playedAt: 2000 }), @@ -317,9 +339,9 @@ describe("matchedGames", () => { test("skips a duplicate detection despite a couple of OCR-misread names", () => { const matched = Scoreboards.matchedGames({ matches: [ - testMatch({ t: 60 }), + testMatch(), testMatch({ - t: 65, + playedAt: 1005, names: ["w1", "vv2", "w3", "w4", "l1", "l2", "l3", "I4"], }), ], @@ -354,9 +376,9 @@ describe("matchedGames", () => { test("skips matches that have no matching game left", () => { const matched = Scoreboards.matchedGames({ matches: [ - testMatch({ t: 60 }), + testMatch(), testMatch({ - t: 5000, + playedAt: 1300, names: ["i", "j", "k", "l", "m", "n", "o", "p"], }), ], @@ -463,8 +485,8 @@ describe("matchedGames", () => { test("does not assign a game played before the previously assigned one", () => { const matched = Scoreboards.matchedGames({ matches: [ - testMatch({ t: 60, mode: "RM", stage: 1 }), - testMatch({ t: 1000, mode: "SZ", stage: 0 }), + testMatch({ playedAt: 2000, mode: "RM", stage: 1 }), + testMatch({ playedAt: 2100, mode: "SZ", stage: 0 }), ], games: [ testGame({ @@ -484,6 +506,36 @@ describe("matchedGames", () => { expect(matched.map(tournamentMatchIdOf)).toEqual([2]); }); + + test("never links a read without a play time", () => { + const matched = Scoreboards.matchedGames({ + matches: [testMatch({ playedAt: null })], + games: [testGame()], + }); + + expect(matched).toHaveLength(0); + }); + + test("links the play of a map reported nearest the read", () => { + const matched = Scoreboards.matchedGames({ + matches: [testMatch({ playedAt: 3900 })], + games: [ + testGame({ tournamentMatchId: 1, playedAt: 2600 }), + testGame({ tournamentMatchId: 2, playedAt: 4000 }), + ], + }); + + expect(matched.map(tournamentMatchIdOf)).toEqual([2]); + }); + + test("leaves a read unlinked when no game was reported within 30 minutes of it", () => { + const matched = Scoreboards.matchedGames({ + matches: [testMatch({ playedAt: 1000 + 31 * 60 })], + games: [testGame({ playedAt: 1000 })], + }); + + expect(matched).toHaveLength(0); + }); }); describe("deriveScoreboardData", () => { @@ -541,14 +593,14 @@ describe("deriveScoreboardData", () => { time: 300, score: [100, 100], penalty: [null, null], - control: [false, false], + control: null, }, { t: 30, time: 270, score: [80, 100], penalty: [null, 12], - control: [true, false], + control: 0, }, ], }); @@ -568,6 +620,21 @@ describe("deriveScoreboardData", () => { expect(swapped!.objective).toEqual(straight!.objective); }); + test("derives track positions winner-first", () => { + const straight = derive([ + { data: testMatch({ objective: testTrackObjective() }), povUserId: null }, + ]); + const swapped = derive([ + { + data: swapSides(testMatch({ objective: testTrackObjective() })), + povUserId: null, + }, + ]); + + expect(straight!.objective!.samples[0]!.position).toBe(25); + expect(swapped!.objective).toEqual(straight!.objective); + }); + test("rebases status samples onto the same origin as the counter's", () => { const data = derive([ { @@ -719,9 +786,9 @@ describe("deriveScoreboardData", () => { }); }); -describe("winnerFirstPlayerNames", () => { +describe("recognizablePlayerNames", () => { test("returns names winner-first with unread names empty", () => { - const names = Scoreboards.winnerFirstPlayerNames( + const names = Scoreboards.recognizablePlayerNames( swapSides( testMatch({ names: ["w1", "", "w3", "w4", "l1", "l2", "l3", "l4"] }), ), @@ -732,7 +799,103 @@ describe("winnerFirstPlayerNames", () => { test("returns null for a match without a linkable scoreboard", () => { expect( - Scoreboards.winnerFirstPlayerNames({ ...testMatch(), winner: null }), + Scoreboards.recognizablePlayerNames({ ...testMatch(), winner: null }), + ).toBe(null); + }); + + test("returns null for a read with too few names to recognize a re-detection", () => { + expect( + Scoreboards.recognizablePlayerNames( + testMatch({ names: ["w1", "", "", "w4", "l1", "", "l3", "l4"] }), + ), + ).toBe(null); + }); +}); + +describe("Scoreboards.povWeaponId", () => { + const SENDER_ID = 1; + const rosterGame = () => + testGame({ + winnerUserIds: [1, 2, 3, 4], + loserUserIds: [5, 6, 7, 8], + inGameNameByUserId: new Map( + NAMES.map((name, i) => [i + 1, `${name}#1234`]), + ), + }); + const weapons: MainWeaponId[] = [10, 20, 30, 40, 50, 60, 70, 80]; + + test.each([ + { + why: "the sender's own seat", + povIndex: 0, + povUserId: SENDER_ID, + expected: 10, + }, + { + why: "a losing sender's own seat", + povIndex: 4, + povUserId: 5, + expected: 50, + }, + { + why: "no POV seat", + povIndex: null, + povUserId: SENDER_ID, + expected: null, + }, + { + why: "a sender in neither roster (caster)", + povIndex: 0, + povUserId: 99, + expected: null, + }, + { + why: "a seat on the sender's opponents' side", + povIndex: 4, + povUserId: SENDER_ID, + expected: null, + }, + { + why: "a teammate's seat (their recording)", + povIndex: 1, + povUserId: SENDER_ID, + expected: null, + }, + ])("$why", ({ povIndex, povUserId, expected }) => { + expect( + Scoreboards.povWeaponId({ + match: testMatch({ povIndex, weapons }), + game: rosterGame(), + povUserId, + }), + ).toBe(expected); + }); + + test.each([ + { why: "unread", seatName: "" }, + { why: "garbled", seatName: "???1" }, + ])( + "a seat whose name is $why can't contradict the sender", + ({ seatName }) => { + const names = NAMES.map((name, i) => (i === 0 ? seatName : name)); + + expect( + Scoreboards.povWeaponId({ + match: testMatch({ povIndex: 0, names, weapons }), + game: rosterGame(), + povUserId: SENDER_ID, + }), + ).toBe(10); + }, + ); + + test("returns null when the seat's weapon was not read", () => { + expect( + Scoreboards.povWeaponId({ + match: testMatch({ povIndex: 0, weapons: [null, ...weapons.slice(1)] }), + game: rosterGame(), + povUserId: SENDER_ID, + }), ).toBe(null); }); }); @@ -751,7 +914,7 @@ describe("resolveContext", () => { mapIndex: i, mode, stageId: stageId as StageId, - playedAt: 1000 + i, + playedAt: 1000 + i * 600, ...partial, }), context: { type: "tournament", tournamentId }, @@ -768,7 +931,7 @@ describe("resolveContext", () => { mapIndex: i, mode, stageId: stageId as StageId, - playedAt: 1000 + i, + playedAt: 1000 + i * 600, }), target: { type: "sendouq", @@ -780,8 +943,8 @@ describe("resolveContext", () => { } const seenSequence = [ - testMatch({ t: 60, mode: "SZ", stage: 0 }), - testMatch({ t: 600, mode: "TC", stage: 1 }), + testMatch({ playedAt: 1000, mode: "SZ", stage: 0 }), + testMatch({ playedAt: 1600, mode: "TC", stage: 1 }), ]; test("resolves the tournament whose games match the seen sequence", () => { @@ -859,7 +1022,7 @@ describe("resolveContext", () => { const context = Scoreboards.resolveContext({ matches: [ seenSequence[0]!, - testMatch({ t: 300, stage: null }), + testMatch({ playedAt: 1300, stage: null }), seenSequence[1]!, ], games: [ diff --git a/app/features/scanner-ingest/core/Scoreboards.ts b/app/features/scanner-ingest/core/Scoreboards.ts index 61e3a8522..89231c5ed 100644 --- a/app/features/scanner-ingest/core/Scoreboards.ts +++ b/app/features/scanner-ingest/core/Scoreboards.ts @@ -3,7 +3,10 @@ import type { ScannerMatchObjective, ScannerMatchPlayerStatus, } from "~/features/scanner/core/scanner-match"; -import type { ScannerLobby } from "~/features/scanner/scanner-types"; +import { + isLinkableLobby, + type ScannerLobby, +} from "~/features/scanner/scanner-types"; import type { AbilityWithUnknown, MainWeaponId, @@ -13,16 +16,13 @@ import type { import { databaseTimestampToJavascriptTimestamp } from "~/utils/dates"; import * as Matches from "./Matches"; -/** Lobby header value scoreboards of tournament/SendouQ games are expected to have. */ -const TOURNAMENT_LOBBY = "PRIVATE"; - /** Of 8 player rows, how many must share name and position for a match to count as a re-detection (tolerates a couple of OCR misreads). */ const MIN_LINKED_DUPLICATE_NAME_MATCHES = 6; /** How many players on the winning (first) resp. losing side of a scoreboard. */ const PLAYERS_PER_TEAM = 4; -/** Matches that must align with one context's games before content resolution trusts it: one (mode, stage, sides) is common across a user's history, two carry order. */ +/** Matches that must align with one context's games before content resolution trusts it. */ const MIN_RESOLVED_SCOREBOARDS = 2; /** @@ -31,7 +31,7 @@ const MIN_RESOLVED_SCOREBOARDS = 2; * plays of one map. Outside every candidate the scan stays unlinked (re-sendable) rather than * silently putting strangers on a match page. */ -const PLAYED_AT_TOLERANCE_MS = 30 * 60 * 1000; +export const PLAYED_AT_TOLERANCE_MS = 30 * 60 * 1000; /** The match context an ingest request was resolved to belong to. */ export type IngestContext = @@ -62,9 +62,11 @@ export interface IngestableGame { winnerInGameNames: string[]; /** known in-game names of the losing team's roster, the side fallback for reads without a POV seat */ loserInGameNames: string[]; + /** both rosters' known in-game names by user id, tells whose seat a POV read marks */ + inGameNameByUserId: Map; /** timestamp of the game's report: the chronological key and what a scan's play time is measured against */ playedAt: number; - /** winner-first row-order names of an already linked ingest of the game, null when none; lets matching skip taken games yet recognize re-detections */ + /** winner-first row-order names of the game's earliest linked ingest that read enough names to recognize a re-detection, null when none; lets matching skip taken games yet recognize re-detections */ linkedPlayerNames: string[] | null; } @@ -127,11 +129,10 @@ export function resolveContext({ /** * Decides which game result each ingested match links to. * - * Only matches with a known winner and two full teams qualify (minimap-only reads never link). - * Matches and games are walked chronologically: each match takes an unassigned game of the same - * mode+stage whose sides agree with what is known. A match with a wall clock takes the game - * reported nearest it (none beyond `PLAYED_AT_TOLERANCE_MS`); a VoD read (video offsets only) - * takes the next in sequence. + * Only matches with a known winner, two full teams and a play time qualify (minimap-only and VoD + * reads never link). Matches and games are walked chronologically: each match takes the unassigned + * game of the same mode+stage reported nearest it whose sides agree with what is known, none + * beyond `PLAYED_AT_TOLERANCE_MS`. * * The sender is the POV player, so the roster they sit in pins the scan's sides — OCR'd names * are too unreliable to overrule it. Only without a POV seat (cast footage) do in-game names @@ -154,12 +155,12 @@ export function matchedGames({ const views = dedupeViews( matches .map((match, matchIndex) => { - const view = winnerFirstView(match, matchIndex); + const view = winnerFirstView(match); return view ? { ...view, matchIndex } : null; }) - .filter((view): view is IndexedView => view !== null) - .filter((view) => !view.lobby || view.lobby === TOURNAMENT_LOBBY) - .sort((a, b) => a.order - b.order), + .filter((view): view is TimedView => view?.playedAt != null) + .filter((view) => isLinkableLobby(view.lobby)) + .sort((a, b) => a.playedAt - b.playedAt), ); const orderedGames = games.toSorted( (a, b) => a.playedAt - b.playedAt || a.mapIndex - b.mapIndex, @@ -181,6 +182,38 @@ export function matchedGames({ return result; } +/** + * The weapon the sender played in a linked game, read off the scan's POV seat. Null unless the + * sender is in the roster on the seat's side and the seat's name isn't another roster member's: + * a caster's or a teammate's recording marks someone else's seat. + */ +export function povWeaponId({ + match, + game, + povUserId, +}: { + match: ScannerMatch; + game: IngestableGame; + povUserId: number; +}): MainWeaponId | null { + const view = winnerFirstView(match); + if (!view || view.povIndex === null) return null; + if (povSideAgreement(view, game, povUserId) !== true) return null; + + const seat = view.players[view.povIndex]!; + const seatName = Matches.normalizeInGameName(seat.name); + const seatUserIds = [...game.inGameNameByUserId] + .filter( + ([, inGameName]) => Matches.normalizeInGameName(inGameName) === seatName, + ) + .map(([userId]) => userId); + if (seatName && seatUserIds.length > 0 && !seatUserIds.includes(povUserId)) { + return null; + } + + return seat.weaponId; +} + export interface IngestedScoreboardPlayer { name: string; tournamentTeamId: number | null; @@ -201,7 +234,7 @@ export interface IngestedScoreboardData { scores: [number | null, number | null]; /** in scoreboard order: rows 0-3 winning team, rows 4-7 losing team */ players: IngestedScoreboardPlayer[]; - /** objective-counter progress, per-team values [winner, loser], `t` in seconds since the game's first read (the source video is not stored). Absent when no counter was read. */ + /** objective-counter progress winner-first (per-team values [winner, loser], `control` 0 = winner), `t` in seconds since the game's first read (the source video is not stored). Absent when no counter was read. */ objective?: ScannerMatchObjective; /** per-player special/death samples, teams winner-first, `t` on the same origin as `objective`. Absent when the icon strip was never read. */ playerStatus?: ScannerMatchPlayerStatus; @@ -231,7 +264,7 @@ export function deriveScoreboardData({ merged = Matches.mergeMatches(merged, other.data).merged; } - const view = winnerFirstView(merged, 0); + const view = winnerFirstView(merged); if (!view) return null; const players = view.players.map( @@ -258,10 +291,22 @@ export function deriveScoreboardData({ }; } -/** A match's players winner-first in row order (unread names as ""), or null without such a view — a game's `linkedPlayerNames`. */ -export function winnerFirstPlayerNames(match: ScannerMatch): string[] | null { - const view = winnerFirstView(match, 0); - return view ? view.players.map((player) => player.name.trim()) : null; +/** + * A linked match's players winner-first in row order (unread names as ""), the game's + * `linkedPlayerNames`. Null when it can't form a view or read too few names to ever recognize + * a re-detection: such a read must not lock the game against every other POV's scan. + */ +export function recognizablePlayerNames(match: ScannerMatch): string[] | null { + const view = winnerFirstView(match); + if (!view) return null; + + const names = view.players.map((player) => player.name.trim()); + const readNamesCount = names.filter((name) => + Matches.normalizeInGameName(name), + ).length; + if (readNamesCount < MIN_LINKED_DUPLICATE_NAME_MATCHES) return null; + + return names; } /** Winner-first row view of a match (unread names as ""). Null when it can't link: unknown winner or a team not fully seen. */ @@ -279,12 +324,11 @@ interface WinnerFirstView { povIndex: number | null; /** wall-clock ms the game was played, when the read carried a clock at all */ playedAt: number | null; - /** chronological walk key: wall-clock, else video time, else input order */ - order: number; } -interface IndexedView extends WinnerFirstView { +interface TimedView extends WinnerFirstView { matchIndex: number; + playedAt: number; } interface WinnerFirstPlayer { @@ -297,10 +341,7 @@ interface WinnerFirstPlayer { abilities?: AbilityWithUnknown[][]; } -function winnerFirstView( - match: ScannerMatch, - index: number, -): WinnerFirstView | null { +function winnerFirstView(match: ScannerMatch): WinnerFirstView | null { if (match.winner === null) return null; const winners = match.teams[match.winner]; const losers = match.teams[match.winner === 0 ? 1 : 0]; @@ -342,7 +383,6 @@ function winnerFirstView( ? match.pov.index : PLAYERS_PER_TEAM + match.pov.index, playedAt: match.playedAt, - order: match.playedAt ?? match.startsAt ?? index, }; } @@ -373,7 +413,21 @@ function winnerFirstObjective( time: sample.time, score: winnerFirst(sample.score), penalty: winnerFirst(sample.penalty), - control: winnerFirst(sample.control), + control: + winner === 0 || sample.control === null + ? sample.control + : sample.control === 0 + ? 1 + : 0, + // a track position is measured toward the end the first side pushes to + ...(sample.position !== undefined + ? { + position: + winner === 1 && sample.position !== null + ? 0 - sample.position + : sample.position, + } + : null), })), }; } @@ -410,7 +464,7 @@ function attributePovUsers( ) { for (const { data, povUserId } of linked) { if (povUserId === null || data.pov === null) continue; - const view = winnerFirstView(data, 0); + const view = winnerFirstView(data); if (!view || view.povIndex === null) continue; if (players.some((player) => player.userId === povUserId)) continue; @@ -444,8 +498,8 @@ function attributionIndex( } /** Drops re-detections of the same game within one request, with the same OCR-jitter tolerance as isLinkedDuplicate. */ -function dedupeViews(sorted: IndexedView[]): IndexedView[] { - const result: IndexedView[] = []; +function dedupeViews(sorted: TimedView[]): TimedView[] { + const result: TimedView[] = []; for (const view of sorted) { const isDuplicate = result.some( @@ -465,11 +519,11 @@ function dedupeViews(sorted: IndexedView[]): IndexedView[] { /** * Index of the game `view` links to, or null. Only games from `from` on are considered so two - * scans of one request never take the same game. A scan with a play time takes the candidate - * reported nearest it (keeps two plays of one map apart); one without takes the next in sequence. + * scans of one request never take the same game. The candidate reported nearest the scan's play + * time wins, which keeps two plays of one map apart. */ function pickGame( - view: IndexedView, + view: TimedView, orderedGames: IngestableGame[], from: number, povUserId: number | null, @@ -479,7 +533,6 @@ function pickGame( for (let i = from; i < orderedGames.length; i++) { const game = orderedGames[i]!; if (!canLink(view, game, povUserId)) continue; - if (view.playedAt === null) return i; const distance = Math.abs( view.playedAt - databaseTimestampToJavascriptTimestamp(game.playedAt), diff --git a/app/features/scanner-ingest/core/relink.server.ts b/app/features/scanner-ingest/core/relink.server.ts new file mode 100644 index 000000000..2f8264c74 --- /dev/null +++ b/app/features/scanner-ingest/core/relink.server.ts @@ -0,0 +1,67 @@ +import { dateToDatabaseTimestamp } from "~/utils/dates"; +import { logger } from "~/utils/logger"; +import * as ScannerIngestRepository from "../ScannerIngestRepository.server"; +import * as Scoreboards from "./Scoreboards"; + +type ReportedMatch = + | { type: "tournament"; tournamentId: number; tournamentMatchId: number } + | { type: "sendouq"; groupMatchId: number }; + +/** + * Links stored matches to a game reported just now. Senders stop resending once sendou.ink has + * stored a match, so a scan that beat the report (or lost its link to a corrected one) would + * otherwise never link. Only matches hinted to the reported match's context and played recently + * enough to be its game are considered, and only that match's games are candidates. Failures are + * logged, never thrown: the report itself already went through. + */ +export async function linkStoredMatches(reported: ReportedMatch) { + try { + const stored = await ScannerIngestRepository.findUnlinkedMatchesByHint({ + context: + reported.type === "tournament" + ? { type: "tournament", tournamentId: reported.tournamentId } + : { type: "sendouq", groupMatchId: reported.groupMatchId }, + playedSince: dateToDatabaseTimestamp( + new Date(Date.now() - Scoreboards.PLAYED_AT_TOLERANCE_MS), + ), + }); + + // sides are pinned by the sender's seat, so each sender's scans match on their own + const byPovUserId = new Map(); + for (const match of stored) { + byPovUserId.set(match.povUserId, [ + ...(byPovUserId.get(match.povUserId) ?? []), + match, + ]); + } + + for (const [povUserId, matches] of byPovUserId) { + // reloaded per sender: an earlier sender's links mark games as taken + const games = + reported.type === "tournament" + ? await ScannerIngestRepository.gamesInTournamentMatch( + reported.tournamentMatchId, + ) + : await ScannerIngestRepository.gamesInGroupMatch( + reported.groupMatchId, + ); + const matched = Scoreboards.matchedGames({ + matches: matches.map((match) => match.data), + games, + povUserId, + }); + if (matched.length === 0) continue; + + await ScannerIngestRepository.addLinks({ + links: matched.map(({ matchIndex, game }) => ({ + ingestedMatchId: matches[matchIndex]!.id, + match: matches[matchIndex]!.data, + game, + })), + povUserId, + }); + } + } catch (error) { + logger.error("Linking stored scanner matches failed", error); + } +} diff --git a/app/features/scanner-ingest/core/tests/fixtures.ts b/app/features/scanner-ingest/core/tests/fixtures.ts index cb1095922..789d6acf5 100644 --- a/app/features/scanner-ingest/core/tests/fixtures.ts +++ b/app/features/scanner-ingest/core/tests/fixtures.ts @@ -72,5 +72,21 @@ export function sideSwapped(match: ScannerMatch): ScannerMatch { match.matchScores === null ? null : [match.matchScores[1], match.matchScores[0]], + objective: + match.objective === null + ? null + : { + ...match.objective, + samples: match.objective.samples.map((sample) => ({ + ...sample, + score: [sample.score[1], sample.score[0]], + penalty: [sample.penalty[1], sample.penalty[0]], + control: + sample.control === null ? null : sample.control === 0 ? 1 : 0, + ...(sample.position != null + ? { position: 0 - sample.position } + : null), + })), + }, }; } diff --git a/app/features/scanner-ingest/tests/harness.ts b/app/features/scanner-ingest/tests/harness.ts index 6f30ed670..e6c9f9785 100644 --- a/app/features/scanner-ingest/tests/harness.ts +++ b/app/features/scanner-ingest/tests/harness.ts @@ -1,8 +1,6 @@ /** Everything ingest scenario cases need: world builders (arrange), ingest wrapper (act), page-loader wrappers and row fetchers (assert). See README.md. */ import { addHours, addMinutes, subDays, subMinutes } from "date-fns"; import * as R from "remeda"; -import { afterAll, beforeAll } from "vitest"; -import { Config } from "~/config"; import { backdate } from "~/db/seed/core/backdate"; import * as SQMatchFactory from "~/db/seed/factories/SQMatchFactory"; import * as TournamentFactory from "~/db/seed/factories/TournamentFactory"; @@ -75,29 +73,6 @@ export interface ScannedOptions extends Partial { partial?: boolean; } -/** Forces the scanner gate open for the suite (`import.meta.env` may not carry the config's `test.env`), restoring it afterwards. Call at the top of the test file. */ -export function setupScannerGate() { - let original: boolean; - beforeAll(() => { - original = Config.scannerEnabled; - Config.scannerEnabled = true; - }); - afterAll(() => { - Config.scannerEnabled = original; - }); -} - -/** Runs `fn` with the scanner gate closed, restoring it even on a throw. */ -export async function withScannerDisabled(fn: () => Promise) { - const original = Config.scannerEnabled; - Config.scannerEnabled = false; - try { - await fn(); - } finally { - Config.scannerEnabled = original; - } -} - /** 8 users with deterministic in-game names (`Alpha1#1111`, …) and an unreported SendouQ match between them; `conclude()` plays it out (alpha sweeps), making its maps linkable, and returns the refreshed map rows. */ export async function sendouqWorld( options: { diff --git a/app/features/scanner-ingest/tests/ingest-scenarios.test.ts b/app/features/scanner-ingest/tests/ingest-scenarios.test.ts index 6437c950f..802aa55e3 100644 --- a/app/features/scanner-ingest/tests/ingest-scenarios.test.ts +++ b/app/features/scanner-ingest/tests/ingest-scenarios.test.ts @@ -1,5 +1,8 @@ import { describe, expect, test } from "vitest"; +import * as SQMatchRepository from "~/features/sendouq-match/SQMatchRepository.server"; +import * as TournamentMatchRepository from "~/features/tournament-match/TournamentMatchRepository.server"; import { databaseTimestampToJavascriptTimestamp } from "~/utils/dates"; +import { linkStoredMatches } from "../core/relink.server"; import { ALPHA_NAMES, anotherSendouqMatch, @@ -16,34 +19,19 @@ import { renamed, scannedGame, sendouqWorld, - setupScannerGate, tournamentMatchPage, tournamentWorld, WEAPONS, - withScannerDisabled, } from "./harness"; -setupScannerGate(); - describe("gating & request filtering", () => { - test("G1 gate closed: scanner disabled and non-privileged user → 403, nothing stored", async () => { - const w = await sendouqWorld(); - - await withScannerDisabled(async () => { - await expect( - ingest(w.bravoUsers[1]!, [w.scanned(w.maps[0]!)]), - ).rejects.toThrow("403"); - }); - - expect(await fetchIngestedMatches()).toHaveLength(0); - }); - - test("G2 non-private lobby: only X-battle matches in the request → skipped entirely", async () => { + test("G2 other lobbies: anarchy and casual matches are skipped entirely", async () => { const w = await sendouqWorld(); await w.conclude(); const res = await ingest(w.povUser, [ - w.scanned(w.maps[0]!, { lobby: "X" }), + w.scanned(w.maps[0]!, { lobby: "SERIES" }), + w.scanned(w.maps[1]!, { lobby: "REGULAR" }), ]); expect(res).toEqual({ @@ -56,22 +44,102 @@ describe("gating & request filtering", () => { expect(await fetchIngestedMatches()).toHaveLength(0); }); - test("G3 mixed request keeps indices: only the private match is stored and linked", async () => { + test("G3 X battle: stored without a context hint and never linked", async () => { + const w = await sendouqWorld(); + await w.conclude(); + + const res = await ingest(w.povUser, [ + w.scanned(w.maps[0]!, { lobby: "X" }), + ]); + + expect(res).toEqual({ + storedMatchesCount: 1, + mergedMatchesCount: 0, + linkedGamesCount: 0, + linkedMatches: [], + contextResolved: false, + }); + const rows = await fetchIngestedMatches(); + expect(rows).toHaveLength(1); + expect(rows[0]!.data.lobby).toBe("X"); + expect(rows[0]!.groupMatchIdHint).toBeNull(); + expect(await fetchLinks()).toHaveLength(0); + }); + + test("G4 mixed request keeps indices: X battle stored, private match stored and linked", async () => { const w = await sendouqWorld(); await w.conclude(); const res = await ingest(w.povUser, [ w.scanned(w.maps[1]!, { lobby: "X" }), + w.scanned(w.maps[0]!, { lobby: "REGULAR" }), w.scanned(w.maps[0]!), ]); - expect(res.storedMatchesCount).toBe(1); + expect(res.storedMatchesCount).toBe(2); expect(res.linkedMatches).toEqual([ - { matchIndex: 1, link: { type: "sendouq", groupMatchId: w.match.id } }, + { matchIndex: 2, link: { type: "sendouq", groupMatchId: w.match.id } }, ]); const rows = await fetchIngestedMatches(); - expect(rows).toHaveLength(1); - expect(rows[0]!.data.lobby).toBe("PRIVATE"); + expect(new Set(rows.map((row) => row.data.lobby))).toEqual( + new Set(["PRIVATE", "X"]), + ); + expect(await fetchLinks()).toHaveLength(1); + }); + + test("G5 implausible values: stats, scores and names the game can't show are stored as unread", async () => { + const w = await sendouqWorld(); + await w.conclude(); + const scan = w.scanned(w.maps[0]!); + const [winners, losers] = scan.teams; + + const res = await ingest(w.povUser, [ + { + ...scan, + matchScores: [99999, -5], + teams: [ + { + players: [ + { + ...winners.players[0]!, + name: "x".repeat(100), + ka: 1e12, + d: -3, + s: 1.5, + paint: 1.5e300, + }, + ...winners.players.slice(1), + ], + }, + losers, + ], + }, + ]); + expect(res.linkedGamesCount).toBe(1); + + const scoreboard = (await qMatchPage(w.match.id)).ingestedScoreboards[0]!; + expect(scoreboard.data.scores).toEqual([null, null]); + expect(scoreboard.data.players[0]).toMatchObject({ + name: "", + ka: null, + d: null, + s: null, + paint: null, + }); + }); + + test("G6 a play time far in the future: stored without one and never linked", async () => { + const w = await sendouqWorld(); + await w.conclude(); + + const res = await ingest(w.povUser, [ + w.scanned(w.maps[0]!, { playedAt: 1e300 }), + ]); + + expect(res.storedMatchesCount).toBe(1); + expect(res.linkedGamesCount).toBe(0); + const rows = await fetchIngestedMatches(); + expect(rows[0]!.data.playedAt).toBeNull(); }); }); @@ -259,6 +327,49 @@ describe("SendouQ flow", () => { ]); }); + test("Q12 a nameless first link: the opponent's named read still joins the game", async () => { + const w = await sendouqWorld(); + await w.conclude(); + const scan = w.scanned(w.maps[0]!); + + await ingest(w.povUser, [ + { + ...scan, + teams: [ + { + players: scan.teams[0].players.map((p) => ({ ...p, name: null })), + }, + { + players: scan.teams[1].players.map((p) => ({ ...p, name: null })), + }, + ], + }, + ]); + const res = await ingest(w.bravoUsers[0]!, [ + w.scanned(w.maps[0]!, { seenFrom: "loser" }), + ]); + + expect(res.linkedGamesCount).toBe(1); + const scoreboard = (await qMatchPage(w.match.id)).ingestedScoreboards[0]!; + expect(scoreboard.data.players.map((p) => p.name)).toEqual([ + ...ALPHA_NAMES, + ...BRAVO_NAMES, + ]); + expect(scoreboard.data.players[0]!.userId).toBe(w.povUser.id); + expect(scoreboard.data.players[4]!.userId).toBe(w.bravoUsers[0]!.id); + }); + + test("Q13 a teammate's recording: the game links, but their weapon isn't credited to the sender", async () => { + const w = await sendouqWorld(); + await w.conclude(); + + // the POV seat is Alpha1's, sent by their teammate Alpha2 + const res = await ingest(w.alphaUsers[1]!, [w.scanned(w.maps[0]!)]); + + expect(res.linkedGamesCount).toBe(1); + expect(await fetchReportedWeapons()).toHaveLength(0); + }); + test("Q11 POV read misflagged as cast: the sender's seat still resolves and links their match", async () => { const w = await sendouqWorld(); await w.conclude(); @@ -348,6 +459,62 @@ describe("SendouQ flow", () => { const page = await qMatchPage(w.match.id); expect(page.ingestedScoreboards.map((sb) => sb.mapIndex)).toEqual([0, 1]); }); + + test("Q14 send before report: the report links the stored read without a resend", async () => { + const w = await sendouqWorld(); + const res = await ingest(w.povUser, [w.scanned(w.maps[0]!)]); + expect(res.linkedGamesCount).toBe(0); + + await SQMatchRepository.reportMapWinner({ + matchId: w.match.id, + winnerId: w.match.alphaGroup.id, + reportedByUserId: w.povUser.id, + reportedCount: 0, + }); + await linkStoredMatches({ type: "sendouq", groupMatchId: w.match.id }); + + const page = await qMatchPage(w.match.id); + expect(page.ingestedScoreboards.map((sb) => sb.mapIndex)).toEqual([0]); + expect(page.reportedWeapons).toEqual([ + { + groupMatchId: w.match.id, + mapIndex: 0, + userId: w.povUser.id, + weaponSplId: WEAPONS[0], + }, + ]); + }); + + test("Q15 corrected map report: the undo unlinks the read, which relinks only to a report it agrees with", async () => { + const w = await sendouqWorld(); + const reportFirstMap = async (winnerId: number) => { + await SQMatchRepository.reportMapWinner({ + matchId: w.match.id, + winnerId, + reportedByUserId: w.povUser.id, + reportedCount: 0, + }); + await linkStoredMatches({ type: "sendouq", groupMatchId: w.match.id }); + }; + const undoFirstMap = () => + SQMatchRepository.undoMapReport({ matchId: w.match.id, mapIndex: 0 }); + + await reportFirstMap(w.match.alphaGroup.id); + await ingest(w.povUser, [w.scanned(w.maps[0]!)]); + expect(await fetchLinks()).toHaveLength(1); + + await undoFirstMap(); + expect(await fetchLinks()).toHaveLength(0); + + await reportFirstMap(w.match.bravoGroup.id); + expect(await fetchLinks()).toHaveLength(0); + expect((await qMatchPage(w.match.id)).ingestedScoreboards).toHaveLength(0); + + await undoFirstMap(); + await reportFirstMap(w.match.alphaGroup.id); + expect(await fetchLinks()).toHaveLength(1); + expect((await qMatchPage(w.match.id)).ingestedScoreboards).toHaveLength(1); + }); }); describe("tournament flow", () => { @@ -432,35 +599,21 @@ describe("tournament flow", () => { expect(laterPage.ingestedScoreboards).toHaveLength(0); }); - test("T2 VoD scan spanning two sets links each read into its own set", async () => { + test("T2 reads without a play time (VoD) are stored but never linked", async () => { const w = await tournamentWorld(); - const [set1, set2] = w.matchesOfTeam(w.championTeamId); - const games = [...(await w.games(set1!.id)), ...(await w.games(set2!.id))]; + const [set1] = w.matchesOfTeam(w.championTeamId); + const games = await w.games(set1!.id); const res = await ingest( w.povUser, games.map((game) => w.scanned(game, { playedAt: null })), ); - expect(res.linkedGamesCount).toBe(4); - expect(res.linkedMatches).toEqual( - [set1, set1, set2, set2].map((set, matchIndex) => ({ - matchIndex, - link: { - type: "tournament", - tournamentId: w.tournamentId, - matchId: set!.id, - }, - })), - ); - const set1Page = await tournamentMatchPage(w.tournamentId, set1!.id); - expect(set1Page.ingestedScoreboards.map((sb) => sb.mapIndex)).toEqual([ - 0, 1, - ]); - const set2Page = await tournamentMatchPage(w.tournamentId, set2!.id); - expect(set2Page.ingestedScoreboards.map((sb) => sb.mapIndex)).toEqual([ - 0, 1, - ]); + expect(res.storedMatchesCount).toBe(games.length); + expect(res.linkedGamesCount).toBe(0); + expect(res.linkedMatches).toEqual([]); + const page = await tournamentMatchPage(w.tournamentId, set1!.id); + expect(page.ingestedScoreboards).toHaveLength(0); }); test("T3 partial then fuller resend: the replay read merges into the stored partial and links", async () => { @@ -514,6 +667,56 @@ describe("tournament flow", () => { const page = await tournamentMatchPage(w.tournamentId, finalMatch.id); expect(page.ingestedScoreboards.map((sb) => sb.mapIndex)).toEqual([0, 1]); }); + + test("T7 cast footage with a POV seat: the caster gets no weapon for a set they didn't play", async () => { + const w = await tournamentWorld(); + const finalMatch = w.matches.at(-1)!; + await w.cast(finalMatch.id); + const caster = await createUser(); + await w.staff(caster); + const [firstGame, secondGame] = await w.games(finalMatch.id); + + const res = await ingest(caster, [ + w.scanned(firstGame!, { cast: true }), + w.scanned(secondGame!, { cast: true, pov: { team: 0, index: 0 } }), + ]); + + expect(res.linkedGamesCount).toBe(2); + expect(await fetchReportedWeapons()).toHaveLength(0); + }); + + test("T8 undone and re-reported game: the read relinks to the new result without a resend", async () => { + const w = await tournamentWorld(); + const finalMatch = w.matches.at(-1)!; + const [game1] = await w.games(finalMatch.id); + await ingest(w.povUser, [w.scanned(game1!)]); + const [result] = await TournamentMatchRepository.findResultsByMatchId( + finalMatch.id, + ); + + await TournamentMatchRepository.deleteResultById(result!.id); + expect(await fetchLinks()).toHaveLength(0); + + const reReported = await TournamentMatchRepository.insertResult({ + matchId: finalMatch.id, + mode: result!.mode, + stageId: result!.stageId, + reporterId: w.author.id, + winnerTeamId: result!.winnerTeamId, + number: 1, + source: result!.source, + }); + await linkStoredMatches({ + type: "tournament", + tournamentId: w.tournamentId, + tournamentMatchId: finalMatch.id, + }); + + const links = await fetchLinks(); + expect(links.map((link) => link.tournamentMatchGameResultId)).toEqual([ + reReported.id, + ]); + }); }); describe("response contract & idempotency", () => { diff --git a/app/features/scanner/README.md b/app/features/scanner/README.md index 2b0f2677b..3a55e609e 100644 --- a/app/features/scanner/README.md +++ b/app/features/scanner/README.md @@ -1,19 +1,231 @@ # Scanner — Splatoon match-event detection -Browser app (route `/scanner`, dev-only until promoted) that watches OBS -Virtual Camera footage, VoD files, or screenshots, detects Splatoon 3 UI -screens with OpenCV.js in a Web Worker, and parses them into events speaking -sendou.ink ids (`ModeShort`/`StageId`/weapon ids/`Ability`). Events aggregate -client-side into `ScannerMatch` objects (`core/scanner-match.ts`) — one -detected game per object, every field nullable — which feed `/ingest` -(features/scanner-ingest) and the `/vods/new` prefill. Imported from the -emberz repo; see `MIGRATION.md` there. +Browser app (route `/scanner`, linked from the Tools menu) that watches a +capture card or OBS Virtual Camera while you play, or scans VoD files, +detects Splatoon 3 UI screens with OpenCV.js in a Web Worker, and parses them +into events speaking sendou.ink ids (`ModeShort`/`StageId`/weapon ids/`Ability`). +Events aggregate client-side into `ScannerMatch` objects +(`core/scanner-match.ts`) — one detected game per object, every field +nullable — which feed `/ingest` (features/scanner-ingest, live captures only), the `/vods/new` +prefill, the match cards and the clip cutter. Imported from the emberz repo; +see `MIGRATION.md` there. -Deliberate convention exceptions (dev tool, ported wholesale): the UI is -English-only (no i18next), `tests/node-test-compat.ts` uses a default export -to stay a `node:test` drop-in, and the suites assert with `node:assert/strict` -rather than the repo-wide `expect`. Keep whichever file you touch on the -idiom it already uses — a half-migration would leave three idioms behind. +Deliberate convention exceptions (ported wholesale): the UI is English-only +(no i18next; the public release should flip that), `tests/node-test-compat.ts` +uses a default export to stay a `node:test` drop-in, and the suites assert +with `node:assert/strict` rather than the repo-wide `expect`. Keep whichever +file you touch on the idiom it already uses — a half-migration would leave +three idioms behind. + +## Product shape + +Anyone can capture, scan files and get clips locally; only uploading needs a +login (`components/upload.ts` mirrors the root loader's user for the +controllers). The landing (`components/LandingView.tsx`) is a title and a +"How it works" disclosure (requirements, what's read, privacy, what's +uploaded — keep it in sync with `UPLOADED_LOBBIES`), two entry cards +(Live / File), the clip history strip and the sessions list; everything else +is one component, `components/SessionView.tsx`, rendered identically for the +running capture (`LiveView`), a past session (`PastSessionView`) and a +scanned VoD (`VodView`): header, clip strip, then match cards +(`components/MatchCard.tsx`) newest first. Views are picked by the `view` +search param (`scanner-search-params.ts`: `live`, `session&id=`, +`vod&name=`, `clips`, `debug` and the dev-only `fixtures` and `montage`). Nothing links to +the `debug` screenshot view: dropping an image on the landing's File card +opens it, for anyone, through the same handoff Inspect uses. + +- **Controllers are module singletons**, not view state: the capture + (`components/live-session.ts`) and a running VoD scan + (`components/vod-scan.ts`) are mounted above the view switch, so moving + between views never stops them; a capture runs until Stop (even off the + page), a scan until it finishes or the scanner page is left. + Views subscribe through `useSyncExternalStore` hooks, as they do to the + event feed (`components/events-feed.ts`, sessions built once per refresh), + the clip list (`clips-feed.ts`), the VoD list (`vods-feed.ts`) and the + localStorage settings (`settings.ts`: source, upload toggle, clip toggle). +- **Sessions** are client-only and derived at render (`core/sessions.ts`): + live events ordered by `detectedAt`, split wherever a gap of ≥ 2 h opens, + keyed by the first event's `detectedAt` (the URL id). A VoD is its own + session keyed by file name. Live `t` is wall-clock seconds (`Date.now()`) + so events from different page loads share one timeline; the ring buffer + stamps footage the same way. +- **Retention** (`store/events.ts`, on a throttled pass at every save): + whole sessions older than 30 days, beyond the newest 20 or past the + `MAX_STORED_EVENTS` budget (~140 SZ games; the newest session is never + cut) go — never part of one, so a kept session's cards always expand to + their full timeline and scoreboards. A saved event re-reads only what was + saved since the last read (plus the events a send status write named), + and a match whose events are unchanged objects is reused rather than + rebuilt (`MatchBuildCache`); live sends take their matches from the feed. + So neither the store's size nor a long session costs much per saved event. +- **Compaction** (`events-feed.ts` on refresh, `store/compacted-matches.ts`): + 72 h after a session ends its games are frozen as built, one record each, + keeping every source event except the per-second Objective/PlayerStatus/ + StripWeapons reads (`compactSources`; ~85% of a game's events, ~6× its + bytes), and its raw events are deleted in the same transaction. Cards, + uploads (`sendCompacted`) and the game data zip read a compacted session like + a raw one, but it no longer picks up match builder fixes and its + `Raw detections` CSV option is disabled. Compacted sessions share the + 30-day / 20-session retention with the raw ones. Every listed event keeps + its full-res analyzed frame as a lossless WebP (~1.1 MB at 1080p, half a + PNG; the montage's VoD scans skip it with `attachFrames: false`), live and + VoD frames alike bounded by 72 h and a shared 500 MB budget, newest first + (`store/frames.ts`), the event staying with `hasFrame: false`. Clips have + their own cap and outlive session deletion. +- **Upload** is on by default when logged in (settings toggle, persisted). + Live: a scoreboard closes its match and sends it, a 15 s tick retries + unlinked matches on a backoff (`sendou-ingest.ts`) and flushes closed + matches whose send was skipped; Stop sends what is left. Server-side, a + game report (tournament or SendouQ) links the stored, still unlinked + matches hinted to its context (`scanner-ingest/core/relink.server.ts`), + and a SendouQ report undo drops the map's links (a tournament undo deletes + the result, cascading them), so a correction relinks without a resend. + Per-event send + statuses back the cards' upload status button (`UploadStatus.tsx`) and its + Retry/Upload. VoD scans never upload: their reads carry no wall clock, and + without one a stranger's game can't be told apart from the uploader's + (linking needs a `playedAt` within 30 min of the game's report). +- **CSV** is a normal feature: `⇩ CSV` in every session header offers + `Matches` (`core/csv/matches.ts`, one row per game, the rows the cards + render) and `Raw detections` (`core/csv/events.ts`, one row per event). + Column names stay English keys. +- **Game data** (`components/match-zip.ts`): an expanded match card's + download, what users report a misread with — `match.json` (the card's + match), `events.json` (every source event, each naming its frame folder) + and `frames/--/` with the frame plus a prefilled + `expected.json`, fixture-ready. +- **Debug gate** (`use-debug.ts`: DEV/ADMIN role or `?debug=true`, which + Settings → Debug → `Enable debug` sets): + `Save frame as fixture` (Settings → Debug, live only) and + `?telemetry=true`. The screenshot view (`ScreenshotPage.tsx`) is not + gated (drop a frame from a game data zip on the landing to inspect it); + the fixtures and montage views are dev-only. + +## Clips + +Clips are real video with audio, cut in the browser. `core/clips/scoring.ts` +is the pure, swappable scorer: only the POV player's own kills feed it +(`ScannerMatch.kills`, off the kill feed); a streak is consecutive kills with +no POV death between them and no pause over `STREAK_MAX_GAP_S`, cut where the +clip would outgrow `MAX_CLIP_SECONDS`; `MIN_KILLS` (4) makes it a window, +scored `kills² + kills / span`. Both controllers run the same +`scoreWindows(match, deaths)` → cut → `store/clips.ts` path, over the +session's matches plus the stretches no scoreboard or minimap backs +(`buildScannerMatches(…, { unbacked: true })`, `LiveSession.clipMatches`): +a streak must not wait for a results screen that may never be read, and live +the ring would have dropped it by then: + +- One capture per browser profile: `startCapture` holds a Web Lock + (`CAPTURE_LOCK`) for its lifetime, so a second tab gets an error instead + of a second pipeline writing every game twice (two samplers on one store + double each event, and the repeated scoreboard opens a duplicate match). +- **Live** (`capture/ring-buffer.ts` + `ring-buffer.worker.ts`): the + stream's tracks become `MediaStreamTrackProcessor` streams, transferred to + a worker so a busy page never costs the footage a frame (on the main + thread ~12% of a 60 fps track was lost to the one-frame processor + buffer). There the video runs through a `VideoEncoder` (H.264, keyframe + every 2 s, bitrate scaled by pixel rate: ~12.5 Mbps at 1080p60) into a ring + of GOPs holding the last + `RING_BUFFER_SECONDS`; the audio through an `AudioEncoder` (AAC, else + Opus) into the same ring. `openCapture` asks for 60 fps explicitly, as + Chromium's default of 30 would halve a capture card. Packets carry the + wall-clock time their frame was captured (noted at encoder input, claimed + at output, so encoder latency never shifts audio against video): video by + frame timestamp, audio by sample position (`SampleClock`), because audio + timestamps drift against the sample count on some sources (a display + capture, an element's `captureStream`) while the AAC encoder counts + samples — matching by timestamp pushed later packets into the future and + a cut lost its second half of audio. In the MP4 the audio runs on by + sample count from its first packet and only jumps forward on a delivery + gap over `AUDIO_RESYNC_S`. The remaining constant offset between a + capture card's picture and another path's sound (desktop audio, a + loopback device) is the `audioOffsetMs` setting, applied at cut time. + The audio input is watched for signal: a device that opens but sends + silence, and an encoder that gives up, show on the live status line. A window is cut once `windowClosed` (no + kill can join and the + tail is captured): the GOP at or before its start through its end, muxed + to MP4 with mediabunny's `EncodedVideoPacketSource` — no decode; a ring + that only begins more than `MAX_MISSING_LEAD_S` after the asked start + (a capture restarted mid-streak) yields nothing rather than a clip + missing its kills. A kill belongs to one clip: a window over footage + already cut — redrawn by a late-read kill, or seen again because the + session outlived the capture that first cut it — is skipped unless it + scores higher, when it is cut and the clips it overlaps are deleted + (`cuts` plus the session's saved clips, `live-session.ts`). What + the clips hear is the `audioSource` setting: the source's own input + (`audioInputFor`: same `groupId`, else a shared label prefix; OBS Virtual + Camera carries none), the desktop's sound (`openDesktopAudio`: the share + picker's audio track, opened before the camera so the click's activation + still covers it, its video surface dropped), any audio input, or off. +- **VoD** (`capture/vod-clips.ts`): packets from the keyframe at or before + the window start are copied into a fresh MP4 (video + audio, no + re-encode, so a minute of 1080p takes well under a second). mediabunny's + `Conversion` with `trim` always transcodes; keep using the packet copy. +- **Encoder**: hardware first, then software — Chromium's + `prefer-hardware` never falls back by itself (Linux, VMs, older GPUs). A + hardware encoder can also fail only once frames flow (NVENC sessions taken + by OBS): the worker swaps in software and restarts the ring. If nothing is + left the status line shows clips as failed; the ring still cuts what it + holds. +- **Buckets** (`store/clips.ts`): `session` holds the running session's + clips (kept until Stop, budget permitting); Stop rolls them into `history`, + where `MAX_HISTORY_CLIPS` (20) applies by score — download to keep. A + file's clips (`vod`) live for one visit and are purged on the next page + load; the file is on disk. Bytes are capped too: the live clips (session + and history together) stay under `LIVE_CLIPS_MAX_BYTES` (4 GB) and each + file's clips under `VOD_CLIPS_MAX_BYTES` (2 GB, they keep the source + bitrate), lowest score dropped first, and a clip that + would eat into the last 250 MB of quota (the events share it) is not + saved — on a file the scan carries on with the next window. Clip records carry the real `start`/`end` + seconds (a packet-copied clip starts at a keyframe), and a card's deaths + and kills get a ▶ when a clip covers their `t`. + +## Tournament montage (dev only, PoC) + +`view=montage` (Settings → Debug → Tournament montage, development only) +builds the basis of a tournament highlight video. `pnpm vods:download ` +writes the tournament's match VoDs to `scripts/output/vods//` together +with `tournament.json` (name, start, logo, top 8, each VoD's POV team — +`MontageManifest` in `core/montage.ts`) and the logos it names; the view +opens that folder through the File System Access API's directory picker +(Chrome). `components/montage.ts` scans every VoD without games yet through +`startVodScan` (`clips: false`: saved like any file scan, so they also show +in the landing's list) and lists every streak meeting the criteria as a +candidate, best score first (`montageWindows`): min splats (default 3) and +a length cap per splat count before fast-forwarding (defaults 2: 20 s, +3: 30 s, 4: 45 s, 5+: 60 s), picked on the page and kept in localStorage. +Candidates are grouped by the POV player's base weapon (read off the +game's scoreboard seat, variants folded to their base). Picking one hides +that team's other candidates (a cast VoD has no team: only its own go). The scanned games, the picks (stored whole, so changing +the criteria never drops one) and the folder's handle live in the +`montages` store (`store/montages.ts`, one record per tournament): a reload +restores the latest montage at once and reconnects the folder itself when +Chrome kept the permission, else `Reconnect folder` asks again. + +Rendering (`capture/montage-render.ts`, overlays in `montage-overlays.ts`) +re-encodes everything at 1920×1080 60 fps: the title card, the picks in +order, then the top 8 (player names for the top 3, and each team's comp: +`montage-comps.ts` builds it the way the results image export does, from +the scans alone — every scoreboard names both sides' weapons and the POV +seat tells which side is the streamer's team, so a team needs VoDs of its +own or of its opponents to get one). The cards and each +clip's ribbon (round, both teams, the POV player and their profile link; +fades out after 5 s) are the image export's graphic blocks +(`features/img-export`), mounted off screen by `MontageGraphics.tsx` in the +dark theme and rasterized with snapdom at the scale that makes them full HD — +the renderer asks for them through `setMontageGraphicCapture`, so the page +must stay open. The manifest carries what they show (round names, both +sides, the streamer, top-3 players, tier, organization, counts — no +weapons: those come from the scans) and the +download script saves every image it names next to the VoDs. Dead time +between two kills — from 3 s after one to 3 s before the next, when at +least 2 s — plays at 4×, muted and badged (`playbackSegments`). Each clip's +sound is normalized to -16 LUFS (`core/loudness.ts`: BS.1770 integrated +loudness, at most +24 dB) with a look-ahead limiter holding peaks under +-1 dBFS. An optional music (or video) file replaces the game sound for the +whole video: looped as needed, faded in and out, normalized the same way; +it is kept in the montage's record. Chrome's save dialog streams the MP4 to disk; without it the file +is built in memory and downloaded. ## Commands @@ -23,19 +235,24 @@ pnpm test:unit:browser # includes tests/logic/ — the fixture- pnpm scanner:report # accuracy table + name character error rate across fixtures pnpm scanner:fixtures [name-substring] # run detectors over matching fixtures, verbose pnpm scanner:replay # replay ffmpeg-extracted frames through the scheduler+detectors -pnpm scanner:scan-vod