Files
WT-Offline/apps/server/src/battle/battle-spares.ts
T
N0VAN0M4LYandduduyagame 3282320fb7 fix(battle): show and debit confirmed backup vehicles
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]>
2026-12-04 06:24:34 +08:00

52 lines
2.3 KiB
TypeScript

import type { BlkBlock } from '@wto/blk';
import type { ItemCatalogue } from '../items/item-catalogue.ts';
import { applyUniversalSpares } from '../items/universal-spares.ts';
import { dailySparesLeft, recordDailySpare, usedDailySpare } from '../profile/profile-daily-spares.ts';
import { setSpareCount, spareCount } from '../profile/profile-vehicle-state.ts';
import type { TechTree } from '../research/tech-tree.ts';
import type { BattleSession } from './battle-session-store.ts';
/** LocalMP uses a source token for an attached backup and the stock UID for an inventory item. */
const VEHICLE_BACKUP = 'vehicle';
export const DAILY_BACKUP = 'daily_free_spare';
export interface BackupSpawn {
readonly slot: number;
readonly unit: string;
readonly backup: string;
}
function receiptKey(session: BattleSession, slot: number): string {
return `${session.sessionId}_${slot}`;
}
export function recordDailyBackupSpawn(profile: BlkBlock, session: BattleSession, spawn: BackupSpawn): boolean {
return spawn.backup === DAILY_BACKUP && session.spareSlots?.get(spawn.slot) === spawn.unit
&& recordDailySpare(profile, session.startedAtSec, receiptKey(session, spawn.slot));
}
/** Debit confirmed backup spawns from the lineup that the server sent at launch. */
export function consumeBattleSpares(
profile: BlkBlock, session: BattleSession, spawns: readonly BackupSpawn[], items: ItemCatalogue, techTree: TechTree,
): Readonly<Record<string, number>> {
const seenSlots = new Set<number>();
const consumed = new Map<string, number>();
for (const spawn of spawns) {
const { slot, unit, backup } = spawn;
if (session.spareSlots?.get(slot) !== unit || seenSlots.has(slot)) continue;
seenSlots.add(slot);
if (backup === DAILY_BACKUP) {
recordDailyBackupSpawn(profile, session, spawn);
continue;
}
if (dailySparesLeft(profile, session.startedAtSec) > 0
|| usedDailySpare(profile, session.startedAtSec, receiptKey(session, slot))) continue;
if (backup !== VEHICLE_BACKUP && !applyUniversalSpares(profile, items, techTree, backup, unit, 1).ok) continue;
const owned = spareCount(profile, unit);
if (owned < 1) continue;
setSpareCount(profile, unit, owned - 1);
consumed.set(unit, (consumed.get(unit) ?? 0) + 1);
}
return Object.fromEntries(consumed);
}