/** * Buying weapon presets (cln_buy_weapon: aircraft:t weapon:t count:i cost:i costGold:i, scripts * weaponry/weaponsPurchase.nut sendPurchaseWeaponRequest). * * Paid presets (bombs, rockets) are bought by the piece: the price is the wpcost.blk value times * the amount, and the player holds at most maxCount pieces (wp_get_weapon_max_count; the client * offers maxCount minus what it has). Pieces are spent in battle. Free presets come with the * vehicle (module-state.ts) and are never bought. */ import type { BlkBlock } from '@wto/blk'; import { canAfford, pay, type Price } from '../profile/profile-currency.ts'; import { ownsVehicle } from '../profile/profile-hangar.ts'; import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import { isPaidWeapon, type EquipmentCatalogue } from './equipment-catalogue.ts'; import { arePrerequisitesOwned } from './module-status.ts'; export interface WeaponOrder { readonly vehicle: string; readonly weapon: string; readonly amount: number; readonly offered: Price; } export function buyWeapons(profile: BlkBlock, catalogue: EquipmentCatalogue, order: WeaponOrder): RuleOutcome { const weapon = catalogue.weapon(order.vehicle, order.weapon); if (!weapon || !ownsVehicle(profile, order.vehicle) || order.amount < 1) return refused('BAD_PARAMS'); if (!isPaidWeapon(weapon) || !arePrerequisitesOwned(profile, order.vehicle, weapon.requires)) return refused('NOT_AVAILABLE'); const record = weaponRecord(profile, order.vehicle, order.weapon); const held = record?.count ?? 0; if (held + order.amount > weapon.maxCount) return refused('NOT_AVAILABLE'); const price: Price = { silverLions: weapon.silverLions * order.amount, goldenEagles: weapon.goldenEagles * order.amount }; if (price.silverLions !== order.offered.silverLions || price.goldenEagles !== order.offered.goldenEagles) return refused('PRICE_CHANGED'); if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY'); pay(profile, price); writeWeaponRecord(profile, order.vehicle, order.weapon, { silverLionsSpent: (record?.silverLionsSpent ?? 0) + price.silverLions, goldenEaglesSpent: (record?.goldenEaglesSpent ?? 0) + price.goldenEagles, count: held + order.amount, }); return ACCEPTED; }