/** * The `mrpc.generic_notify` message that starts a local battle (`from: "wto_battle"`, * `func: "launch"`), received by the client's launch script patch * (patches/gui/battle/launch-local-battle-from-queue). * * - `mission`: the complete mission settings for `select_training_mission`, as a typed tree * `{ p: [[name, type, value], ...], b: [[name, node], ...] }` with type "t", "i", "r" or "b". * Matching notifications are JSON, which cannot tell 1 from 1.0; the engine reads ints and reals * with typed getters, so the type travels with every value. Repeated blocks stay separate. * - `lobby`: the session lobby settings the client's own room scripts read during the battle * (game mode and both sides' countries). * - `battle`: what the client keeps for this battle (session id for the settlement, extra slots, * `spawnScore` for Ground Realistic Battles, and `awards` for the in-battle reward banner). */ import type { BlkBlock, BlkValue } from '@wto/blk'; import type { BannerParams } from '../battle/battle-economy.ts'; import type { BattleLaunchPlan } from '../battle/battle-matchmaker.ts'; import { battleMissionSettings } from '../battle/battle-mission-settings.ts'; type WireParam = readonly [name: string, type: 't' | 'i' | 'r' | 'b', value: string | number | boolean]; interface WireBlock { readonly p: readonly WireParam[]; readonly b: ReadonlyArray; } export interface BattleLaunchNotify extends Record { readonly from: 'wto_battle'; readonly func: 'launch'; readonly mission: WireBlock; readonly lobby: Readonly>; readonly battle: Readonly>; } export function battleLaunchNotify( plan: BattleLaunchPlan, awards: BannerParams, scores: Readonly>, ): BattleLaunchNotify { return { from: 'wto_battle', func: 'launch', mission: toWire(battleMissionSettings(plan, scores)), lobby: { game_mode_id: plan.modeId, game_mode_name: plan.modeName, economicName: plan.economicName, symmetricTeams: plan.symmetricTeams, country_allies: plan.countriesAllies.map(withCountryPrefix), country_axis: plan.countriesAxis.map(withCountryPrefix), mission: { name: plan.missionName, difficulty: plan.difficulty, ...(plan.maxRespawns === undefined ? {} : { maxRespawns: plan.maxRespawns }), }, }, battle: { sessionId: plan.sessionId, modeName: plan.modeName, ...(plan.modeTag ? { modeTag: plan.modeTag } : {}), economicName: plan.economicName, country: plan.country, missionName: plan.missionName, ...(plan.selectedSlot ? { selectedSlot: plan.selectedSlot } : {}), extraBattleSlots: plan.extraBattleSlots, ...(plan.spawnScore ? { spawnScore: plan.spawnScore } : {}), awards, }, }; } function toWire(block: BlkBlock): WireBlock { return { p: block.params.map((param) => [param.name, ...wireValue(param.name, param.value)] as const), b: block.blocks.map((child) => [child.name, toWire(child)] as const), }; } function wireValue(name: string, value: BlkValue): readonly ['t' | 'i' | 'r' | 'b', string | number | boolean] { switch (value.type) { case 'string': return ['t', value.value]; case 'int': return ['i', value.value]; case 'real': return ['r', value.value]; case 'bool': return ['b', value.value]; default: throw new Error(`mission settings parameter ${name} has type ${value.type}, which the launch message cannot carry`); } } function withCountryPrefix(country: string): string { return country.startsWith('country_') ? country : `country_${country}`; }