Files
WT-Offline/apps/server/src/battle/battle-report.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

174 lines
7.5 KiB
TypeScript

/**
* The battle report of `cln_wto_finish_battle` (patches/gui/battle/report-local-battle-settlement):
* what happened in the battle, as facts only. The client sends no money; the local server
* prices every event (battle-economy.ts).
*
* sessionId:t outcome:t ("win" | "lose" | "running") localTeam:i
* teamAScore:i teamBScore:i sessionStart:r sessionEnd:r (mission time, seconds)
* localPlayerId:i the local player's MPlayer id
* lives{ life{ unit:t start:r end:r died:b } ... } one per spawn of the local player
* events{ e{ time:r unit:t code:i expClass:t victim:t victimIsPlayer:b friendly:b
* modifier:t damage:i } ... } the local player's combat events
* feed{ k{ time:r killer:i killerTeam:i victim:i victimTeam:i } ... }
* every kill of the HUD kill log
* (HUD_MSG_MULTIPLAYER_DMG, isKill)
* players{ p{ id:i team:i score:i kills:i groundKills:i humanKills:i navalKills:i aiKills:i
* aiGroundKills:i aiNavalKills:i assists:i deaths:i } ... }
* the final scoreboard
* strikes{ s{ time:r } ... } explosions of the local player's
* nuclear bombs (native local_mp_nuke.c)
* spares{ spawn{ slot:i unit:t backup:t } ... } confirmed backup spawns
*/
import { findBlock, type BlkBlock } from '@wto/blk';
import type { CombatEventFact } from './battle-economy.ts';
import type { BackupSpawn } from './battle-spares.ts';
export type BattleOutcomeKind = 'win' | 'lose' | 'running';
export interface BattleLife {
readonly unit: string;
readonly start: number;
readonly end: number;
/** The life ended with the vehicle destroyed (not with the battle's end or a vehicle change). */
readonly died: boolean;
}
/** One kill of the HUD kill log; ids are MPlayer ids, -1 when the unit had no player. */
export interface FeedKill {
readonly time: number;
readonly killer: number;
readonly killerTeam: number;
readonly victim: number;
readonly victimTeam: number;
}
/** One row of the final scoreboard (the stock mplayer fields, scripts/mplayerParamType.nut). */
export interface ScoreboardPlayer {
readonly id: number;
readonly team: number;
readonly score: number;
readonly kills: number;
readonly groundKills: number;
readonly humanKills: number;
readonly navalKills: number;
readonly aiKills: number;
readonly aiGroundKills: number;
readonly aiNavalKills: number;
readonly assists: number;
readonly deaths: number;
}
const SCOREBOARD_FIELDS = [
'score', 'kills', 'groundKills', 'humanKills', 'navalKills', 'aiKills', 'aiGroundKills', 'aiNavalKills', 'assists', 'deaths',
] as const;
export interface ReportedCombatEvent extends CombatEventFact {
readonly time: number;
readonly unit: string;
readonly victim: string;
readonly modifier: string;
}
export interface BattleReport {
readonly sessionId: string;
readonly outcome: BattleOutcomeKind;
readonly localTeam: number;
readonly teamAScore: number;
readonly teamBScore: number;
readonly sessionStart: number;
readonly sessionEnd: number;
readonly localPlayerId: number;
readonly lives: readonly BattleLife[];
readonly events: readonly ReportedCombatEvent[];
readonly feed: readonly FeedKill[];
readonly players: readonly ScoreboardPlayer[];
/** Mission times at which the local player's nuclear bombs exploded. */
readonly nuclearStrikes: readonly number[];
readonly spareSpawns: readonly BackupSpawn[];
}
/** Undefined when the report is malformed; lives and events that make no sense are dropped. */
export function parseBattleReport(payload: BlkBlock): BattleReport | undefined {
const sessionId = strOf(payload, 'sessionId');
if (!sessionId || !/^[0-9a-f]{16}$/i.test(sessionId)) return undefined;
const rawOutcome = strOf(payload, 'outcome');
const sessionStart = Math.max(0, realOf(payload, 'sessionStart'));
const sessionEnd = Math.max(sessionStart, realOf(payload, 'sessionEnd'));
const lives = (findBlock(payload, 'lives')?.blocks ?? []).flatMap((block): BattleLife[] => {
const unit = strOf(block, 'unit');
const start = Math.max(sessionStart, realOf(block, 'start'));
const end = Math.min(sessionEnd, Math.max(start, realOf(block, 'end')));
return unit ? [{ unit, start, end, died: boolOf(block, 'died') }] : [];
});
const livedUnits = new Set(lives.map((life) => life.unit));
const events = (findBlock(payload, 'events')?.blocks ?? []).flatMap((block): ReportedCombatEvent[] => {
const unit = strOf(block, 'unit');
if (!unit || !livedUnits.has(unit)) return [];
return [{
time: realOf(block, 'time'),
unit,
code: intOf(block, 'code'),
expClass: strOf(block, 'expClass') ?? '',
victim: strOf(block, 'victim') ?? '',
victimIsPlayer: boolOf(block, 'victimIsPlayer'),
friendly: boolOf(block, 'friendly'),
modifier: strOf(block, 'modifier') ?? '',
damage: Math.max(0, intOf(block, 'damage')),
}];
});
return {
sessionId: sessionId.toLowerCase(),
outcome: rawOutcome === 'win' || rawOutcome === 'lose' ? rawOutcome : 'running',
localTeam: intOf(payload, 'localTeam', 1),
teamAScore: intOf(payload, 'teamAScore'),
teamBScore: intOf(payload, 'teamBScore'),
sessionStart,
sessionEnd,
localPlayerId: intOf(payload, 'localPlayerId', -1),
lives,
events,
feed: (findBlock(payload, 'feed')?.blocks ?? []).map((block) => ({
time: realOf(block, 'time'),
killer: intOf(block, 'killer', -1),
killerTeam: intOf(block, 'killerTeam'),
victim: intOf(block, 'victim', -1),
victimTeam: intOf(block, 'victimTeam'),
})).sort((a, b) => a.time - b.time),
players: (findBlock(payload, 'players')?.blocks ?? []).map((block): ScoreboardPlayer => ({
id: intOf(block, 'id', -1),
team: intOf(block, 'team'),
...Object.fromEntries(SCOREBOARD_FIELDS.map((field) => [field, Math.max(0, intOf(block, field))])) as Record<(typeof SCOREBOARD_FIELDS)[number], number>,
})),
nuclearStrikes: (findBlock(payload, 'strikes')?.blocks ?? []).map((block) => realOf(block, 'time')).sort((a, b) => a - b),
spareSpawns: (findBlock(payload, 'spares')?.blocks ?? []).flatMap((block): BackupSpawn[] => {
const slot = intOf(block, 'slot', -1);
const unit = strOf(block, 'unit');
const backup = strOf(block, 'backup');
return slot >= 0 && unit && backup ? [{ slot, unit, backup }] : [];
}),
};
}
function strOf(block: BlkBlock, name: string): string | undefined {
const value = block.params.find((p) => p.name === name)?.value;
return value?.type === 'string' && value.value !== '' ? value.value : undefined;
}
function intOf(block: BlkBlock, name: string, fallback = 0): number {
const value = block.params.find((p) => p.name === name)?.value;
if (value?.type === 'int') return value.value;
if (value?.type === 'int64') return Number(value.value);
return fallback;
}
function realOf(block: BlkBlock, name: string): number {
const value = block.params.find((p) => p.name === name)?.value;
if (value?.type === 'real' || value?.type === 'int') return Number.isFinite(value.value) ? value.value : 0;
return 0;
}
function boolOf(block: BlkBlock, name: string): boolean {
const value = block.params.find((p) => p.name === name)?.value;
return value?.type === 'bool' ? value.value : value?.type === 'int' ? value.value !== 0 : false;
}