/** * Matching request `wto_battle.fact` (patches/gui/battle/record-local-battle-awards): one fact of a * running LocalMP battle; the answer lists the battle awards it completed, which the client shows * as the stock award banner (scripts/streaks.nut add_streak_message). Online the battle server * sends those banners. * * params: sessionId:s localPlayerId:i localTeam:i kind:s, and by kind * "event": time:f unit:s code:i expClass:s victim:s victimIsPlayer:b friendly:b modifier:s damage:i * "life": unit:s start:f end:f died:b * "kill": time:f killer:i killerTeam:i victim:i victimTeam:i * "strike": time:f the local player's nuclear bomb exploded * answer: { streaks: [{ id:s stage:i (-1 = single-stage) stageParam:i wp:i exp:i }] } */ import type { BattleLiveLogs } from '../battle/battle-live-log.ts'; import type { BattleSessionStore } from '../battle/battle-session-store.ts'; import type { BattleStreaks } from '../battle/battle-streaks.ts'; import { withoutSupportPlanes } from '../battle/battle-support-planes.ts'; import type { MatchingRequestHandler } from './matching-requests.ts'; export interface BattleLiveDependencies { readonly sessions: BattleSessionStore; readonly liveLogs: BattleLiveLogs; readonly streaks: BattleStreaks; /** Units whose lives and events earn no awards (battle-support-planes.ts). */ readonly supportPlanes: ReadonlySet; } export function battleLiveRequests(deps: BattleLiveDependencies): Array<[string, MatchingRequestHandler]> { return [['wto_battle.fact', (request, connection) => { const userId = connection.authenticatedUserId(); const p = request.params; const sessionId = typeof p.sessionId === 'string' ? p.sessionId.toLowerCase() : ''; const session = userId ? deps.sessions.findForUser(sessionId, userId) : undefined; if (!session || session.settled) return { streaks: [] }; const log = deps.liveLogs.of(session.sessionId); if (!recordFact(log, p)) return { streaks: [] }; const awarded = deps.streaks.evaluate({ ...withoutSupportPlanes({ events: log.events, lives: log.lives }, deps.supportPlanes), feed: log.feed, nuclearStrikes: log.nuclearStrikes, localPlayerId: int(p.localPlayerId, -1), localTeam: int(p.localTeam, 0), sessionEnd: Math.max(0, ...log.lives.map((life) => life.end), ...log.events.map((e) => e.time), ...log.nuclearStrikes), }, session); const fresh = awarded.filter((streak) => !streak.hidden && !log.announced.has(streak.key)); for (const streak of fresh) log.announced.add(streak.key); return { streaks: fresh.map((streak) => ({ id: streak.id, stage: streak.stage ?? -1, stageParam: streak.stageParam, wp: streak.wp, exp: streak.exp, })), }; }]]; } function recordFact(log: ReturnType, p: Readonly>): boolean { switch (p.kind) { case 'event': log.events.push({ time: num(p.time), unit: str(p.unit), code: int(p.code, -1), expClass: str(p.expClass), victim: str(p.victim), victimIsPlayer: p.victimIsPlayer === true, friendly: p.friendly === true, modifier: str(p.modifier), damage: int(p.damage, 0), }); return true; case 'life': log.lives.push({ unit: str(p.unit), start: num(p.start), end: num(p.end), died: p.died === true }); return true; case 'kill': log.feed.push({ time: num(p.time), killer: int(p.killer, -1), killerTeam: int(p.killerTeam, 0), victim: int(p.victim, -1), victimTeam: int(p.victimTeam, 0) }); return true; case 'strike': log.nuclearStrikes.push(num(p.time)); return true; default: return false; } } function str(value: unknown): string { return typeof value === 'string' ? value : ''; } function num(value: unknown): number { return typeof value === 'number' && Number.isFinite(value) ? value : 0; } function int(value: unknown, fallback: number): number { return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : fallback; }