Use the signed i64 profile encoding identified in PR #13. Make attached backups selectable in LocalMP after the daily quota, and debit backups only after the engine confirms the selected slot entered play. Record the stock daily allowance once per room and slot. Settle owned backups using the confirmed spawn facts and keep repeat settlement from charging the same battle again. Verified server and launcher types, MSVC compilation, three client builds for patch and Quirrel checks, and a temporary 2.61.0.58 build. In-game acceptance is pending. Co-authored-by: duduyagame <[email protected]>
111 lines
3.6 KiB
TypeScript
111 lines
3.6 KiB
TypeScript
/**
|
|
* 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<number, string>;
|
|
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<string, BattleSession>();
|
|
readonly #pendingByUser = new Map<string, string>();
|
|
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<string, BattleSession>): 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;
|
|
}
|
|
}
|