// Series helpers every profile's derivation shares (release 30, lane P). Pure. // // A model reading a frame misreads now and then — a digit dropped, a // neighbour's stack taken for the hero's — and a single frame that jumps // 100× is a misread, not a double-up. The rule is AGREEMENT OF NEIGHBOURS: a // point is set aside when the points either side of it agree with each other // and it agrees with neither. A real change is followed by points that agree // with it, so it stays. export type Pt = { t: number; v: number }; /** How far apart two values are, as a ratio ≥ 1 (Infinity when one is 0 and the other is not). */ export function ratioOf(a: number, b: number): number { if (a === b) return 1; if (a <= 0 || b <= 0) return Infinity; return a > b ? a / b : b / a; } export type AgreeRule = { ratio: number; absolute?: number }; /** Whether two values agree under a rule: within `ratio`, or within `absolute` of each other. */ export function agree(a: number, b: number, rule: AgreeRule): boolean { if (rule.absolute !== undefined && Math.abs(a - b) <= rule.absolute) return true; return ratioOf(a, b) <= rule.ratio; } /** * The points with misreads set aside: `kept` and `dropped`. A middle point is * dropped when its two neighbours agree and it agrees with neither; an end * point when the next two (or last two) agree and it disagrees with both by * the square of the ratio — but a last point of 0 is kept (a series that ends * at 0 ended there). * One pass over the original points, so two misreads in a row both stay (the * rule cannot tell them from a change) — that is what `flags` and the review * are for. */ export function dropMisreads(points: readonly Pt[], rule: AgreeRule): { kept: Pt[]; dropped: Pt[] } { const kept: Pt[] = []; const dropped: Pt[] = []; const n = points.length; for (let i = 0; i < n; i++) { const p = points[i]; let a: Pt | undefined; let b: Pt | undefined; if (i > 0 && i < n - 1) { a = points[i - 1]; b = points[i + 1]; } else if (i === 0 && n >= 3) { a = points[1]; b = points[2]; } else if (i === n - 1 && n >= 3 && p.v !== 0) { // The last point has nothing after it to agree with, so it is judged by // the two before it — except a 0, which ends a series for real (a bust). a = points[n - 2]; b = points[n - 3]; } // An end point has only one side to be judged by, so it must disagree // twice as hard (the square of the ratio): a first reading followed by a // real change is kept, a 100× outlier is not. const end = i === 0 || i === n - 1; const r: AgreeRule = end ? { ratio: rule.ratio * rule.ratio, ...(rule.absolute !== undefined ? { absolute: rule.absolute * 2 } : {}) } : rule; if (a && b && agree(a.v, b.v, rule) && !agree(p.v, a.v, r) && !agree(p.v, b.v, r)) dropped.push(p); else kept.push(p); } return { kept, dropped }; } /** Collapse a series to its changes: a point equal to the one before it is left out. */ export function changesOnly(points: readonly Pt[]): Pt[] { const out: Pt[] = []; for (const p of points) if (!out.length || out[out.length - 1].v !== p.v) out.push(p); return out; } /** The point nearest `t` within `window` seconds, or null. */ export function nearest(points: readonly Pt[], t: number, window: number): Pt | null { let best: Pt | null = null; for (const p of points) { const d = Math.abs(p.t - t); if (d <= window && (!best || d < Math.abs(best.t - t))) best = p; } return best; } /** * Excursions set aside: a run of points that leaves a level and comes back to * it (within `returnRatio`) inside `windowS` seconds of the last point before * it. A stack read mid-hand (chips in the pot are not in front of the player) * dips and returns when the pot is won; a misread does the same. Neither is a * change of the stack. A level that is left and not returned to stays. */ export function dropExcursions(points: readonly Pt[], o: { returnRatio: number; windowS: number }): { kept: Pt[]; dropped: Pt[] } { const kept: Pt[] = []; const dropped: Pt[] = []; const back: AgreeRule = { ratio: o.returnRatio }; let i = 0; while (i < points.length) { const anchor = kept[kept.length - 1]; const p = points[i]; if (!anchor || agree(p.v, anchor.v, back)) { kept.push(p); i++; continue; } let j = i + 1; while (j < points.length && points[j].t - anchor.t <= o.windowS && !agree(points[j].v, anchor.v, back)) j++; if (j < points.length && points[j].t - anchor.t <= o.windowS) { for (let k = i; k < j; k++) dropped.push(points[k]); i = j; } else { kept.push(p); i++; } } return { kept, dropped }; }