Files
WT-Offline/apps/server/src/battle/battle-report.ts
T
N0VAN0M4LY 930ea07630 refactor(repo)!: move apps, shared packages, and patch definitions
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:

  apps/server        the local server (was packages/local-service),
                     package @wto/server
  packages/patcher   builds the game copy (was packages/build),
                     package @wto/patcher
  patches/exe        exe patch definitions (was exe-patches)
  patches/gui        GUI script patches (was gui-patches)

Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".

The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.

The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.


Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.

BREAKING CHANGE: Use @wto/server and @wto/patcher from the new workspace paths. Run the development build with scripts/build-game-copy.ts and pass --data.
2026-11-29 03:20:54 +08:00

177 lines
7.6 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{ <unit>:i ... } backup vehicles used
*/
import { findBlock, type BlkBlock } from '@wto/blk';
import type { CombatEventFact } from './battle-economy.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 usedSpares: Readonly<Record<string, number>>;
}
/** 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),
usedSpares: parseSpareMap(findBlock(payload, 'spares')),
};
}
function parseSpareMap(sparesBlk: BlkBlock | undefined): Record<string, number> {
const out: Record<string, number> = {};
for (const param of sparesBlk?.params ?? []) {
const count = param.value.type === 'int' ? param.value.value : 0;
if (param.name && count > 0) out[param.name] = count;
}
return out;
}
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;
}