Send unsure leftover picks unless the frame is only the away team.
ci / rust (pull_request) Successful in 3m59s
ci / typescript (pull_request) Successful in 9m26s

Reference faces are missing for part of the squad, so holding every unnamed pick left the rest client empty during the match.
This commit is contained in:
spark committed 2026-12-02 20:40:12 +00:00
1 parent 5da37adf33
commit 312c1f1a68
4 files changed
+157 -54

No files matched your search

+26 -9
View File
@@ -5,10 +5,11 @@ const start = Date.parse("2026-12-02T20:15:00Z");
const leadMin = 10; const leadMin = 10;
const homeNames = new Set(["joao silva"]); const homeNames = new Set(["joao silva"]);
const homeClubs = new Set(["u. leiria"]); const homeClubs = new Set(["u. leiria"]);
const awayNames = new Set(["costa"]);
const awayClubs = new Set(["sporting cp b"]);
function decide(extra: { function decide(extra: {
at: number; at: number;
facesStatus?: string;
playerNames?: string[]; playerNames?: string[];
faces?: FaceHint[]; faces?: FaceHint[];
homeTeamId?: string | null; homeTeamId?: string | null;
@@ -17,22 +18,24 @@ function decide(extra: {
at: extra.at, at: extra.at,
start, start,
leadMin, leadMin,
facesStatus: extra.facesStatus ?? "DONE",
playerNames: extra.playerNames ?? [], playerNames: extra.playerNames ?? [],
faces: extra.faces ?? [], faces: extra.faces ?? [],
homeTeamId: extra.homeTeamId ?? "home", homeTeamId: extra.homeTeamId ?? "home",
homeNames, homeNames,
homeClubs, homeClubs,
awayTeamId: "away",
awayNames,
awayClubs,
}); });
} }
describe("leftover picks before and during a match", () => { describe("leftover picks before and during a match", () => {
it("sends every leftover pick until ten minutes before kick-off", () => { 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 })).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; const after = start - 10 * 60_000 + 1;
expect(decide({ at: after, playerNames: ["João Silva"] })).toBe("send"); expect(decide({ at: after, playerNames: ["João Silva"] })).toBe("send");
expect( expect(
@@ -44,14 +47,13 @@ describe("leftover picks before and during a match", () => {
expect( expect(
decide({ decide({
at: after, at: after,
facesStatus: "DONE",
faces: [{ teamId: null, name: null, club: null, open: true }], faces: [{ teamId: null, name: null, club: null, open: true }],
}), }),
).toBe("wait"); ).toBe("send");
expect(decide({ at: after, facesStatus: "PENDING" })).toBe("wait"); 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; const after = start + 30_000;
expect( expect(
decide({ decide({
@@ -59,6 +61,21 @@ describe("leftover picks before and during a match", () => {
faces: [{ teamId: "away", name: "Costa", club: "Sporting CP B", open: false }], faces: [{ teamId: "away", name: "Costa", club: "Sporting CP B", open: false }],
}), }),
).toBe("skip"); ).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");
}); });
}); });
+64 -7
View File
@@ -81,28 +81,85 @@ export function isHomePlayer(input: {
return false; return false;
} }
function matchesSide(
name: string | null,
club: string | null,
teamId: string | null,
sideTeamId: string | null,
sideNames: ReadonlySet<string>,
sideClubs: ReadonlySet<string>,
): 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<string>;
awayClubs: ReadonlySet<string>;
homeTeamId: string | null;
homeNames: ReadonlySet<string>;
homeClubs: ReadonlySet<string>;
}): 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. * Leftover picks for the rest destination.
* Up to `leadMin` before kick-off, every leftover pick goes. * 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: { export function homeRestDecision(input: {
at: number; at: number;
start: number; start: number;
leadMin: number; leadMin: number;
facesStatus: string;
playerNames: readonly string[]; playerNames: readonly string[];
faces: readonly FaceHint[]; faces: readonly FaceHint[];
homeTeamId: string | null; homeTeamId: string | null;
homeNames: ReadonlySet<string>; homeNames: ReadonlySet<string>;
homeClubs: ReadonlySet<string>; homeClubs: ReadonlySet<string>;
awayTeamId: string | null;
awayNames: ReadonlySet<string>;
awayClubs: ReadonlySet<string>;
}): "send" | "skip" | "wait" { }): "send" | "skip" | "wait" {
const cutoff = input.start - input.leadMin * 60_000; const cutoff = input.start - input.leadMin * 60_000;
if (input.at <= cutoff) return "send"; if (input.at <= cutoff) return "send";
if (isHomePlayer(input)) return "send"; if (isHomePlayer(input)) return "send";
if (input.facesStatus === "PENDING" || input.facesStatus === "RUNNING") return "wait"; if (isClearlyOnlyAway(input)) return "skip";
for (const face of input.faces) { return "send";
if (face.open) return "wait";
}
return "skip";
} }
+65 -36
View File
@@ -95,8 +95,10 @@ export interface AutopilotRecord {
gallery?: Record<string, boolean>; gallery?: Record<string, boolean>;
/** Leftover pick already queued for the rest destination. */ /** Leftover pick already queued for the rest destination. */
restSent?: boolean; 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?: 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 * Planned develop/crop ops from analysis. Applied only when this frame is picked for the burst — never on the
* near-duplicates left unflagged. * near-duplicates left unflagged.
@@ -667,7 +669,7 @@ export class AutopilotService {
return chosenCount; 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<void> { private async flushHomeRest(): Promise<void> {
const { db, log } = this.deps; const { db, log } = this.deps;
const rows = await db.$queryRaw<{ event_id: string }[]>` 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.created_at > now() - interval '2 days'
AND (p.autopilot->>'curated') = 'true' AND (p.autopilot->>'curated') = 'true'
AND (p.autopilot->>'restSent') IS DISTINCT FROM '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 jsonb_typeof(e.autopilot->'restHomeFromMin') = 'number'
AND e.autopilot->>'restTargetId' IS NOT NULL AND e.autopilot->>'restTargetId' IS NOT NULL
AND jsonb_typeof(e.autopilot->'galleries') = 'array' 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 * 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<number> { private async sendGalleryRest(eventId: string, settings: AutopilotSettings): Promise<number> {
const leadMin = settings.restHomeFromMin; const leadMin = settings.restHomeFromMin;
@@ -715,22 +720,27 @@ export class AutopilotService {
ingestUntil: true, ingestUntil: true,
home: true, home: true,
homeShort: true, homeShort: true,
away: true,
awayShort: true,
homeTeamId: true, homeTeamId: true,
awayTeamId: true,
fixture: { select: { season: true } }, fixture: { select: { season: true } },
homeTeam: { select: { name: true, abbreviation: true } }, homeTeam: { select: { name: true, abbreviation: true } },
awayTeam: { select: { name: true, abbreviation: true } },
}, },
}); });
if (!event?.createdById) return 0; if (!event?.createdById) return 0;
const photos = await db.photo.findMany({ const photos = await db.photo.findMany({
where: { eventId, flag: "PICK", autopilotStatus: "DONE" }, 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 cutoff = event.startsAt.getTime() - leadMin * 60_000;
const pending: typeof photos = []; const pending: typeof photos = [];
for (const photo of photos) { for (const photo of photos) {
const rec = photo.autopilot as AutopilotRecord | null; const rec = photo.autopilot as AutopilotRecord | null;
if (!rec?.curated) continue; 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 (rec.hold === "ball" || rec.chosen) continue;
if (!takenDuring(photo.capturedAt, event)) continue; if (!takenDuring(photo.capturedAt, event)) continue;
let inGallery = false; let inGallery = false;
@@ -742,34 +752,11 @@ export class AutopilotService {
} }
if (!pending.length) return 0; if (!pending.length) return 0;
const homeNames = new Set<string>(); const season = event.fixture?.season ?? null;
const homeClubs = new Set<string>(); const homeNames = await squadNames(db, event.homeTeamId, season);
for (const label of [event.home, event.homeShort, event.homeTeam?.name, event.homeTeam?.abbreviation]) { const awayNames = await squadNames(db, event.awayTeamId, season);
const folded = foldName(label); const homeClubs = labelSet([event.home, event.homeShort, event.homeTeam?.name, event.homeTeam?.abbreviation]);
if (folded) homeClubs.add(folded); const awayClubs = labelSet([event.away, event.awayShort, event.awayTeam?.name, event.awayTeam?.abbreviation]);
}
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 faceIds: string[] = []; const faceIds: string[] = [];
for (const photo of pending) { for (const photo of pending) {
@@ -863,12 +850,14 @@ export class AutopilotService {
at, at,
start: event.startsAt.getTime(), start: event.startsAt.getTime(),
leadMin, leadMin,
facesStatus: photo.facesStatus,
playerNames: photo.players, playerNames: photo.players,
faces: hints.get(photo.id) ?? [], faces: hints.get(photo.id) ?? [],
homeTeamId: event.homeTeamId, homeTeamId: event.homeTeamId,
homeNames, homeNames,
homeClubs, homeClubs,
awayTeamId: event.awayTeamId,
awayNames,
awayClubs,
}); });
if (decision === "wait") continue; if (decision === "wait") continue;
if (decision === "skip") { if (decision === "skip") {
@@ -907,13 +896,53 @@ export class AutopilotService {
private async markRest(photo: { id: string; autopilot: unknown }, key: "restSent" | "restSkip"): Promise<void> { private async markRest(photo: { id: string; autopilot: unknown }, key: "restSent" | "restSkip"): Promise<void> {
const rec = photo.autopilot; const rec = photo.autopilot;
if (!rec || typeof rec !== "object" || Array.isArray(rec)) return; 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({ await this.deps.db.photo.update({
where: { id: photo.id }, 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<string> {
const names = new Set<string>();
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<Set<string>> {
const names = new Set<string>();
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. */ /** Gallery flags for a frame whose window was decided this pass. Unmentioned destinations stay as they were. */
function galleryMarks( function galleryMarks(
id: string, id: string,
+2 -2
View File
@@ -106,8 +106,8 @@ export const AutopilotSettings = z.object({
restTargetId: z.string().uuid().nullable().default(null), restTargetId: z.string().uuid().nullable().default(null),
/** /**
* With galleries and `restTargetId`: leftover picks go to the rest destination until this many minutes * 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 * before kick-off. After that they still go, unless the frame is clearly only the away team. Omitted:
* send leftovers (an older app leaves this out; the stored value is kept). * 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(), restHomeFromMin: z.number().int().min(0).max(180).nullable().optional(),
}); });