/** * In-memory registry of battle sessions created by `match.enqueue` and settled once by * `cln_wto_finish_battle`. * * Each session gets a non-zero positive 63-bit integer `roomId` formatted as a 16-character * lowercase hexadecimal `sessionId`, matching `userlogs` `roomId:i64` (`aces.exe` `0x1799d30`). */ import { randomBytes } from 'node:crypto'; import type { ModeDifficulty, UnitClass } from '../game-modes/catalogue/game-mode-declaration.ts'; export interface BattleSession { readonly sessionId: string; readonly roomId: bigint; readonly userId: string; readonly modeId: number; readonly modeName: string; readonly economicName: string; readonly difficulty: ModeDifficulty; readonly requiredClass: UnitClass; /** A random battle (the mode's displayType random_battle), as opposed to an event. */ readonly randomBattle: boolean; readonly missionName: string; readonly missionPath: string; readonly missionLocName?: string; readonly country: string; readonly startedAtSec: number; /** Owned lineup slots that may use a backup in this session. Filled when launch is sent. */ spareSlots?: ReadonlyMap; settled: boolean; } export interface NewBattleSession { readonly userId: string; readonly modeId: number; readonly modeName: string; readonly economicName: string; readonly difficulty: ModeDifficulty; readonly requiredClass: UnitClass; readonly randomBattle: boolean; readonly missionName: string; readonly missionPath: string; readonly missionLocName?: string; readonly country: string; readonly startedAtSec: number; } export class BattleSessionStore { readonly #sessions = new Map(); readonly #pendingByUser = new Map(); readonly #maxRetained: number; constructor(maxRetained: number) { this.#maxRetained = Math.max(1, maxRetained); } create(input: NewBattleSession): BattleSession { const roomId = generateRoomId(this.#sessions); const sessionId = roomId.toString(16).padStart(16, '0'); const session: BattleSession = { ...input, sessionId, roomId, settled: false }; this.#sessions.set(sessionId, session); this.#pendingByUser.set(input.userId, sessionId); this.#trim(); return session; } /** Cancel a queued session if the player leaves the queue before entering flight. */ cancelPendingForUser(userId: string): void { this.#pendingByUser.delete(userId); } isPending(userId: string, sessionId: string): boolean { return this.#pendingByUser.get(userId) === sessionId; } clearPending(userId: string, sessionId: string): void { if (this.#pendingByUser.get(userId) === sessionId) { this.#pendingByUser.delete(userId); } } findForUser(sessionId: string, userId: string): BattleSession | undefined { const session = this.#sessions.get(sessionId); return session && session.userId === userId ? session : undefined; } markSettled(sessionId: string): void { const session = this.#sessions.get(sessionId); if (session) { session.settled = true; this.clearPending(session.userId, sessionId); } } #trim(): void { while (this.#sessions.size > this.#maxRetained) { const oldestKey = this.#sessions.keys().next().value; if (oldestKey === undefined) break; this.#sessions.delete(oldestKey); } } } function generateRoomId(existing: ReadonlyMap): bigint { for (;;) { const raw = randomBytes(8).readBigUInt64BE(0) & 0x7fffffffffffffffn; const roomId = raw === 0n ? 1n : raw; const hex = roomId.toString(16).padStart(16, '0'); if (!existing.has(hex)) return roomId; } }