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

82 lines
3.9 KiB
TypeScript

/**
* Char action `cln_wto_finish_battle`: receives the facts of a local battle (battle-report.ts),
* settles them into the player's save (battle-settlement.ts) and answers the updated profile,
* whose new `EULT_SESSION_RESULT` log carries the debriefing table.
*/
import type { BattleSettlementContext } from '@wto/mods';
import type { AccountStore } from '../../accounts/account-store.ts';
import { parseBattleReport } from '../../battle/battle-report.ts';
import { hasBattleResultLog } from '../../battle/battle-result-log.ts';
import type { BattleSessionStore } from '../../battle/battle-session-store.ts';
import { settleBattle, type BattleSettlementDeps } from '../../battle/battle-settlement.ts';
import type { BattleEconomy } from '../../battle/battle-economy.ts';
import type { BattleStreaks } from '../../battle/battle-streaks.ts';
import { withoutSupportPlanes } from '../../battle/battle-support-planes.ts';
import type { CrewSkillCatalogue } from '../../crew/crew-skill-rules.ts';
import type { ItemCatalogue } from '../../items/item-catalogue.ts';
import type { EquipmentCatalogue } from '../../modifications/equipment-catalogue.ts';
import { clientProfile } from '../../profile/client-profile.ts';
import type { PlayerProfiles } from '../../profile/player-profiles.ts';
import type { ResearchEfficiency } from '../../research/research-efficiency.ts';
import type { RepairRules } from '../../repair/repair-rules.ts';
import type { ResearchRules } from '../../research/research-rules.ts';
import type { CharActionHandler } from '../char-action.ts';
import { withAccount } from './account-context.ts';
export interface BattleActionDependencies {
readonly sessions: BattleSessionStore;
readonly economy: BattleEconomy;
readonly efficiency: ResearchEfficiency;
readonly streaks: BattleStreaks;
/** Units whose lives and events are not settled (battle-support-planes.ts). */
readonly supportPlanes: ReadonlySet<string>;
readonly onAfterSettle?: (ctx: BattleSettlementContext) => void;
}
export interface FinishBattleContext {
readonly profiles: PlayerProfiles;
readonly accounts: AccountStore;
readonly research: ResearchRules;
readonly equipment: EquipmentCatalogue;
readonly crew: CrewSkillCatalogue;
readonly items: ItemCatalogue;
readonly repair: RepairRules;
}
export function finishBattleAction(ctx: FinishBattleContext, battle: BattleActionDependencies): CharActionHandler {
const settlementDeps: BattleSettlementDeps = {
research: ctx.research,
efficiency: battle.efficiency,
equipment: ctx.equipment,
crew: ctx.crew,
economy: battle.economy,
streaks: battle.streaks,
items: ctx.items,
repair: ctx.repair,
};
return (context) => withAccount(context, ctx.accounts, (account) => {
const parsed = context.payload && parseBattleReport(context.payload);
if (!parsed) return { kind: 'error', code: 'BAD_PARAMS' };
const report = withoutSupportPlanes(parsed, battle.supportPlanes);
const session = battle.sessions.findForUser(report.sessionId, account.userId);
if (!session) return { kind: 'error', code: 'BAD_PARAMS' };
if (session.settled) {
return { kind: 'data', blk: clientProfile(ctx.profiles.profileOf(account.userId), account) };
}
const updated = ctx.profiles.updateWhen(account.userId, (profile) => {
// The result log and expenses are written together. A retry can arrive after the profile
// was saved but before the session was marked settled.
if (hasBattleResultLog(profile, session.roomId)) return false;
settleBattle(profile, report, session, settlementDeps);
battle.onAfterSettle?.({
userId: account.userId,
roomId: session.sessionId,
summary: { profile, report, session },
});
return true;
});
battle.sessions.markSettled(session.sessionId);
return { kind: 'data', blk: clientProfile(updated ?? ctx.profiles.profileOf(account.userId), account) };
});
}