// Practice "hype" calls: short, upbeat stats the speaker calls during practice to cheer a // driver on. Pure functions: decoder_runtime.js gathers the numbers and plays what // pickPracticeHype returns. At most one call per lap, and at least MIN_GAP_MS between two // calls for the same driver (lap milestones excepted), so the speaker never nags. export const PRACTICE_HYPE_EVENTS = Object.freeze([ "practice_beat_last_time", "practice_class_rank", "practice_lap_milestone", "practice_faster_streak", "practice_average_down", "practice_close_to_best", "practice_duel", ]); export const MIN_GAP_MS = 30000; const COOLDOWN_MS = { practice_class_rank: 60000, practice_faster_streak: 120000, practice_average_down: 900000, practice_close_to_best: 90000, practice_duel: 300000, }; const MILESTONE_EVERY = 50; const CLOSE_TO_BEST_MS = 100; const AVERAGE_WINDOW = 10; const AVERAGE_MIN_GAIN_MS = 200; const DUEL_WINDOW_MS = 200; const DUEL_ACTIVE_MS = 120000; const average = (values) => values.reduce((sum, value) => sum + value, 0) / values.length; // This driver's valid lap times in the session, oldest first. export function getDriverLapTimes(passings, competitorKey) { return (Array.isArray(passings) ? passings : []) .filter((item) => String(item?.competitorKey || "") === String(competitorKey || "") && item.validLap !== false && !item.suspect && Number(item.lapMs) > 500) .map((item) => Number(item.lapMs)); } // Laps that end a run of faster laps: 17.9, 17.6, 17.4, 17.2 is 3. export function countFasterStreak(laps) { let streak = 0; for (let index = laps.length - 1; index > 0 && laps[index] < laps[index - 1]; index -= 1) streak += 1; return streak; } // First 10 laps against the last 10, crash and pit laps (over 130 % of the best) left out. export function getAverageGain(laps) { if (laps.length < AVERAGE_WINDOW * 2) return null; const best = Math.min(...laps); const clean = laps.filter((ms) => ms <= best * 1.3); if (clean.length < AVERAGE_WINDOW * 2) return null; const first = average(clean.slice(0, AVERAGE_WINDOW)); const last = average(clean.slice(-AVERAGE_WINDOW)); return { gainMs: first - last, lastAvgMs: last }; } // 1-based rank by best lap among the rows of one class (rows: { key, bestLapMs }). export function getClassRank(rows, key) { const ranked = rows.filter((row) => Number(row.bestLapMs) > 500).sort((a, b) => a.bestLapMs - b.bestLapMs); const index = ranked.findIndex((row) => row.key === key); return { rank: index >= 0 ? index + 1 : null, size: ranked.length }; } // The driver's best lap on the most recent earlier day they drove this layout. // sessions: [{ passings }] from any session; dayStartTs: midnight today. export function getLastTimeBestMs(passingLists, { driverId, layoutId, dayStartTs }) { const byDay = new Map(); passingLists.forEach((passings) => (passings || []).forEach((item) => { if (!driverId || item?.driverId !== driverId || item.validLap === false || item.suspect) return; if (String(item.trackLayoutId || "") !== String(layoutId || "")) return; const ms = Number(item.lapMs), ts = Number(item.timestamp); if (!(ms > 500) || !(ts < dayStartTs)) return; const day = new Date(ts).toDateString(); byDay.set(day, { ts: Math.max(byDay.get(day)?.ts || 0, ts), best: Math.min(byDay.get(day)?.best ?? Infinity, ms) }); })); const latest = [...byDay.values()].sort((a, b) => b.ts - a.ts)[0]; return latest ? latest.best : null; } // The closest other driver on track whose best lap is within 0.2 s of this driver's. export function findDuelRival(rows, { key, bestLapMs, now }) { return rows .filter((row) => row.key !== key && Number(row.bestLapMs) > 500 && now - Number(row.lastTimestamp || 0) <= DUEL_ACTIVE_MS) .map((row) => ({ ...row, gapMs: row.bestLapMs - bestLapMs })) .filter((row) => Math.abs(row.gapMs) <= DUEL_WINDOW_MS) .sort((a, b) => Math.abs(a.gapMs) - Math.abs(b.gapMs))[0] || null; } // ctx: { laps (incl. this lap), lapMs, previousBestMs, lapCount, classRankBefore, classRankAfter, // classSize, lastTimeBestMs, rival, now, memory }. memory is kept per driver per session by the // caller and updated here. Returns { key, variables } (gaps in ms) or null. export function pickPracticeHype(ctx) { const { laps, lapMs, previousBestMs, lapCount, now, memory } = ctx; memory.calledAt ||= {}; const isPb = Boolean(previousBestMs && lapMs < previousBestMs); const ready = (key) => now - (memory.calledAt[key] ?? -Infinity) >= (COOLDOWN_MS[key] || 0); const gapOk = now - (memory.lastHypeAt ?? -Infinity) >= MIN_GAP_MS; const candidates = []; if (isPb && ctx.lastTimeBestMs && lapMs < ctx.lastTimeBestMs && !memory.beatLastTime) { candidates.push({ key: "practice_beat_last_time", variables: { gapMs: ctx.lastTimeBestMs - lapMs }, once: "beatLastTime" }); } if (ctx.classRankAfter && ctx.classRankAfter <= 3 && ctx.classSize >= 3 && (!ctx.classRankBefore || ctx.classRankAfter < ctx.classRankBefore)) { candidates.push({ key: "practice_class_rank", variables: { rank: ctx.classRankAfter } }); } if (lapCount > 0 && lapCount % MILESTONE_EVERY === 0 && memory.lastMilestone !== lapCount) { candidates.push({ key: "practice_lap_milestone", variables: { laps: lapCount }, milestone: true }); } if (countFasterStreak(laps) === 3) candidates.push({ key: "practice_faster_streak", variables: { laps: 3 } }); if (laps.length % 5 === 0) { const gain = getAverageGain(laps); if (gain && gain.gainMs >= AVERAGE_MIN_GAIN_MS) candidates.push({ key: "practice_average_down", variables: { gapMs: gain.gainMs, avgMs: gain.lastAvgMs } }); } if (!isPb && previousBestMs && laps.length >= 5 && lapMs - previousBestMs <= CLOSE_TO_BEST_MS) { candidates.push({ key: "practice_close_to_best", variables: { gapMs: lapMs - previousBestMs } }); } if (isPb && ctx.rival) candidates.push({ key: "practice_duel", variables: { gapMs: Math.abs(ctx.rival.gapMs), ahead: ctx.rival.gapMs >= 0, rival: ctx.rival.name } }); const pick = candidates.find((item) => ready(item.key) && (gapOk || item.milestone)); if (!pick) return null; memory.calledAt[pick.key] = now; memory.lastHypeAt = now; if (pick.milestone) memory.lastMilestone = lapCount; if (pick.once) memory[pick.once] = true; return { key: pick.key, variables: pick.variables }; }