diff --git a/apps/api/src/autopilot/home-rest.test.ts b/apps/api/src/autopilot/home-rest.test.ts index 8b4f4b5..a1d055b 100644 --- a/apps/api/src/autopilot/home-rest.test.ts +++ b/apps/api/src/autopilot/home-rest.test.ts @@ -5,10 +5,11 @@ const start = Date.parse("2026-12-02T20:15:00Z"); const leadMin = 10; const homeNames = new Set(["joao silva"]); const homeClubs = new Set(["u. leiria"]); +const awayNames = new Set(["costa"]); +const awayClubs = new Set(["sporting cp b"]); function decide(extra: { at: number; - facesStatus?: string; playerNames?: string[]; faces?: FaceHint[]; homeTeamId?: string | null; @@ -17,22 +18,24 @@ function decide(extra: { at: extra.at, start, leadMin, - facesStatus: extra.facesStatus ?? "DONE", playerNames: extra.playerNames ?? [], faces: extra.faces ?? [], homeTeamId: extra.homeTeamId ?? "home", homeNames, homeClubs, + awayTeamId: "away", + awayNames, + awayClubs, }); } describe("leftover picks before and during a match", () => { it("sends every leftover pick until ten minutes before kick-off", () => { expect(decide({ at: start - 10 * 60_000 })).toBe("send"); - expect(decide({ at: start - 10 * 60_000 - 1, facesStatus: "PENDING" })).toBe("send"); + expect(decide({ at: start - 10 * 60_000 - 1 })).toBe("send"); }); - it("sends a home player after the cutoff and holds a frame whose faces are still open", () => { + it("sends a home player and a frame we cannot name after the cutoff", () => { const after = start - 10 * 60_000 + 1; expect(decide({ at: after, playerNames: ["João Silva"] })).toBe("send"); expect( @@ -44,14 +47,13 @@ describe("leftover picks before and during a match", () => { expect( decide({ at: after, - facesStatus: "DONE", faces: [{ teamId: null, name: null, club: null, open: true }], }), - ).toBe("wait"); - expect(decide({ at: after, facesStatus: "PENDING" })).toBe("wait"); + ).toBe("send"); + expect(decide({ at: after })).toBe("send"); }); - it("does not send an away player once the faces are settled", () => { + it("does not send a frame that is clearly only the away team", () => { const after = start + 30_000; expect( decide({ @@ -59,6 +61,21 @@ describe("leftover picks before and during a match", () => { faces: [{ teamId: "away", name: "Costa", club: "Sporting CP B", open: false }], }), ).toBe("skip"); - expect(decide({ at: after, facesStatus: "FAILED" })).toBe("skip"); + expect(decide({ at: after, playerNames: ["Costa"] })).toBe("skip"); + expect( + decide({ + at: after, + faces: [ + { teamId: "away", name: "Costa", club: "Sporting CP B", open: false }, + { teamId: null, name: null, club: null, open: true }, + ], + }), + ).toBe("send"); + expect( + decide({ + at: after, + faces: [{ teamId: null, name: "Árbitro", club: "Arbitragem", open: false }], + }), + ).toBe("send"); }); }); diff --git a/apps/api/src/autopilot/home-rest.ts b/apps/api/src/autopilot/home-rest.ts index c078e72..2d487c5 100644 --- a/apps/api/src/autopilot/home-rest.ts +++ b/apps/api/src/autopilot/home-rest.ts @@ -81,28 +81,85 @@ export function isHomePlayer(input: { return false; } +function matchesSide( + name: string | null, + club: string | null, + teamId: string | null, + sideTeamId: string | null, + sideNames: ReadonlySet, + sideClubs: ReadonlySet, +): boolean { + if (sideTeamId && teamId === sideTeamId) return true; + const foldedName = foldName(name); + if (foldedName && sideNames.has(foldedName)) return true; + const foldedClub = foldName(club); + if (foldedClub && sideClubs.has(foldedClub)) return true; + return false; +} + +/** + * True when every identified person in the frame is the away side, and nobody is still unnamed. + * A missing face, a weak guess, or a home player is not this case. + */ +export function isClearlyOnlyAway(input: { + playerNames: readonly string[]; + faces: readonly FaceHint[]; + awayTeamId: string | null; + awayNames: ReadonlySet; + awayClubs: ReadonlySet; + homeTeamId: string | null; + homeNames: ReadonlySet; + homeClubs: ReadonlySet; +}): boolean { + const namedPlayers: string[] = []; + for (const name of input.playerNames) { + if (foldName(name)) namedPlayers.push(name); + } + if (namedPlayers.length === 0 && input.faces.length === 0) return false; + + for (const face of input.faces) { + if (face.open) return false; + } + + for (const name of namedPlayers) { + const home = matchesSide(name, null, null, input.homeTeamId, input.homeNames, input.homeClubs); + if (home) return false; + const away = matchesSide(name, null, null, input.awayTeamId, input.awayNames, input.awayClubs); + if (!away) return false; + } + + for (const face of input.faces) { + const home = matchesSide(face.name, face.club, face.teamId, input.homeTeamId, input.homeNames, input.homeClubs); + if (home) return false; + const away = matchesSide(face.name, face.club, face.teamId, input.awayTeamId, input.awayNames, input.awayClubs); + if (!away) return false; + } + + return true; +} + /** * Leftover picks for the rest destination. * Up to `leadMin` before kick-off, every leftover pick goes. - * After that, only a home-team player. Faces still being read stay queued (`wait`). + * After that, a pick still goes unless the frame is clearly only the away team. + * An unidentified player is sent: the reference set is incomplete, and the client deletes the odd frame. */ export function homeRestDecision(input: { at: number; start: number; leadMin: number; - facesStatus: string; playerNames: readonly string[]; faces: readonly FaceHint[]; homeTeamId: string | null; homeNames: ReadonlySet; homeClubs: ReadonlySet; + awayTeamId: string | null; + awayNames: ReadonlySet; + awayClubs: ReadonlySet; }): "send" | "skip" | "wait" { const cutoff = input.start - input.leadMin * 60_000; if (input.at <= cutoff) return "send"; if (isHomePlayer(input)) return "send"; - if (input.facesStatus === "PENDING" || input.facesStatus === "RUNNING") return "wait"; - for (const face of input.faces) { - if (face.open) return "wait"; - } - return "skip"; + if (isClearlyOnlyAway(input)) return "skip"; + return "send"; } diff --git a/apps/api/src/autopilot/service.ts b/apps/api/src/autopilot/service.ts index 94ac2d7..f09ca65 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -95,8 +95,10 @@ export interface AutopilotRecord { gallery?: Record; /** Leftover pick already queued for the rest destination. */ restSent?: boolean; - /** Leftover pick that is not a home player, so it stays out of the rest destination. */ + /** Leftover pick that is clearly only the away team, so it stays out of the rest destination. */ restSkip?: boolean; + /** `restSkip` was decided by the away-team rule. Older skips without this are looked at again. */ + restAway?: boolean; /** * Planned develop/crop ops from analysis. Applied only when this frame is picked for the burst — never on the * near-duplicates left unflagged. @@ -667,7 +669,7 @@ export class AutopilotService { return chosenCount; } - /** Events whose leftover picks are still waiting on a face name. */ + /** Events whose leftover picks have not been sent, including skips made before the away-team rule. */ private async flushHomeRest(): Promise { const { db, log } = this.deps; const rows = await db.$queryRaw<{ event_id: string }[]>` @@ -679,7 +681,10 @@ export class AutopilotService { AND p.created_at > now() - interval '2 days' AND (p.autopilot->>'curated') = 'true' AND (p.autopilot->>'restSent') IS DISTINCT FROM 'true' - AND (p.autopilot->>'restSkip') IS DISTINCT FROM 'true' + AND ( + (p.autopilot->>'restSkip') IS DISTINCT FROM 'true' + OR (p.autopilot->>'restAway') IS DISTINCT FROM 'true' + ) AND jsonb_typeof(e.autopilot->'restHomeFromMin') = 'number' AND e.autopilot->>'restTargetId' IS NOT NULL AND jsonb_typeof(e.autopilot->'galleries') = 'array' @@ -696,7 +701,7 @@ export class AutopilotService { /** * Picks that did not make a gallery. Until `restHomeFromMin` before kick-off they all go to the rest - * destination. From then on, only a home-team player does, and a frame whose faces are still open waits. + * destination. From then on they still go, unless every identified person in the frame is the away team. */ private async sendGalleryRest(eventId: string, settings: AutopilotSettings): Promise { const leadMin = settings.restHomeFromMin; @@ -715,22 +720,27 @@ export class AutopilotService { ingestUntil: true, home: true, homeShort: true, + away: true, + awayShort: true, homeTeamId: true, + awayTeamId: true, fixture: { select: { season: true } }, homeTeam: { select: { name: true, abbreviation: true } }, + awayTeam: { select: { name: true, abbreviation: true } }, }, }); if (!event?.createdById) return 0; const photos = await db.photo.findMany({ where: { eventId, flag: "PICK", autopilotStatus: "DONE" }, - select: { id: true, capturedAt: true, createdAt: true, players: true, facesStatus: true, autopilot: true }, + select: { id: true, capturedAt: true, createdAt: true, players: true, autopilot: true }, }); const cutoff = event.startsAt.getTime() - leadMin * 60_000; const pending: typeof photos = []; for (const photo of photos) { const rec = photo.autopilot as AutopilotRecord | null; if (!rec?.curated) continue; - if (rec.restSent || rec.restSkip) continue; + if (rec.restSent) continue; + if (rec.restSkip && rec.restAway) continue; if (rec.hold === "ball" || rec.chosen) continue; if (!takenDuring(photo.capturedAt, event)) continue; let inGallery = false; @@ -742,34 +752,11 @@ export class AutopilotService { } if (!pending.length) return 0; - const homeNames = new Set(); - const homeClubs = new Set(); - for (const label of [event.home, event.homeShort, event.homeTeam?.name, event.homeTeam?.abbreviation]) { - const folded = foldName(label); - if (folded) homeClubs.add(folded); - } - if (event.homeTeamId) { - const season = event.fixture?.season ?? null; - let squad: { name: string; fullName: string | null }[] = []; - if (season) { - squad = await db.sportsPlayer.findMany({ - where: { teamId: event.homeTeamId, goneAt: null, season }, - select: { name: true, fullName: true }, - }); - } - if (!squad.length) { - squad = await db.sportsPlayer.findMany({ - where: { teamId: event.homeTeamId, goneAt: null }, - select: { name: true, fullName: true }, - }); - } - for (const player of squad) { - const name = foldName(player.name); - const fullName = foldName(player.fullName); - if (name) homeNames.add(name); - if (fullName) homeNames.add(fullName); - } - } + const season = event.fixture?.season ?? null; + const homeNames = await squadNames(db, event.homeTeamId, season); + const awayNames = await squadNames(db, event.awayTeamId, season); + const homeClubs = labelSet([event.home, event.homeShort, event.homeTeam?.name, event.homeTeam?.abbreviation]); + const awayClubs = labelSet([event.away, event.awayShort, event.awayTeam?.name, event.awayTeam?.abbreviation]); const faceIds: string[] = []; for (const photo of pending) { @@ -863,12 +850,14 @@ export class AutopilotService { at, start: event.startsAt.getTime(), leadMin, - facesStatus: photo.facesStatus, playerNames: photo.players, faces: hints.get(photo.id) ?? [], homeTeamId: event.homeTeamId, homeNames, homeClubs, + awayTeamId: event.awayTeamId, + awayNames, + awayClubs, }); if (decision === "wait") continue; if (decision === "skip") { @@ -907,13 +896,53 @@ export class AutopilotService { private async markRest(photo: { id: string; autopilot: unknown }, key: "restSent" | "restSkip"): Promise { const rec = photo.autopilot; if (!rec || typeof rec !== "object" || Array.isArray(rec)) return; + let extra: { restSkip: true; restAway: true } | { restSent: true }; + if (key === "restSkip") { + extra = { restSkip: true, restAway: true }; + } else { + extra = { restSent: true }; + } await this.deps.db.photo.update({ where: { id: photo.id }, - data: { autopilot: { ...rec, [key]: true } as object }, + data: { autopilot: { ...rec, ...extra } as object }, }); } } +function labelSet(labels: readonly (string | null | undefined)[]): Set { + const names = new Set(); + for (const label of labels) { + const folded = foldName(label); + if (folded) names.add(folded); + } + return names; +} + +async function squadNames(db: Db, teamId: string | null, season: string | null): Promise> { + const names = new Set(); + if (!teamId) return names; + let squad: { name: string; fullName: string | null }[] = []; + if (season) { + squad = await db.sportsPlayer.findMany({ + where: { teamId, goneAt: null, season }, + select: { name: true, fullName: true }, + }); + } + if (squad.length === 0) { + squad = await db.sportsPlayer.findMany({ + where: { teamId, goneAt: null }, + select: { name: true, fullName: true }, + }); + } + for (const player of squad) { + const name = foldName(player.name); + const fullName = foldName(player.fullName); + if (name) names.add(name); + if (fullName) names.add(fullName); + } + return names; +} + /** Gallery flags for a frame whose window was decided this pass. Unmentioned destinations stay as they were. */ function galleryMarks( id: string, diff --git a/packages/contracts/src/library.ts b/packages/contracts/src/library.ts index 91876fb..7c27e6d 100644 --- a/packages/contracts/src/library.ts +++ b/packages/contracts/src/library.ts @@ -106,8 +106,8 @@ export const AutopilotSettings = z.object({ restTargetId: z.string().uuid().nullable().default(null), /** * With galleries and `restTargetId`: leftover picks go to the rest destination until this many minutes - * before kick-off. After that, only a leftover pick of a home-team player does. Omitted: galleries do not - * send leftovers (an older app leaves this out; the stored value is kept). + * before kick-off. After that they still go, unless the frame is clearly only the away team. Omitted: + * galleries do not send leftovers (an older app leaves this out; the stored value is kept). */ restHomeFromMin: z.number().int().min(0).max(180).nullable().optional(), });