/** * 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 { ClanStore } from '../../clans/clan-store.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; 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; readonly clans?: { readonly store: ClanStore } | undefined; } 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, onBattleRpEarned: ctx.clans ? (baseVehicleRp, nowSec) => { ctx.clans!.store.recordBattleActivity(account.userId, baseVehicleRp, nowSec); ctx.clans!.store.syncProfile(profile, account, nowSec); } : undefined, }); 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) }; }); }