/** * Buying classic items (cln_buy_item, implementation notes; scripts items/itemsBase.nut * _requestBuy): name:t count:i cost:i costGold:i. * * The client prices a purchase as the price-table price of one piece times the pieces it buys * (getCost: wp_get_item_cost x sell_count_step; count is sell_count_step), so the offered price * must be the piece price times count. */ import type { BlkBlock } from '@wto/blk'; import { canAfford, pay, type Price } from '../profile/profile-currency.ts'; import type { ServerTime } from '../profile/server-time.ts'; import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import { isForSale, type ItemCatalogue } from './item-catalogue.ts'; import { grantItem } from './item-grants.ts'; export function buyItem(profile: BlkBlock, catalogue: ItemCatalogue, name: string, pieces: number, offered: Price, now: ServerTime): RuleOutcome { const item = catalogue.item(name); if (!item || pieces < 1 || pieces > catalogue.maxPiecesPerPurchase) return refused('BAD_PARAMS'); if (!isForSale(item)) return refused('NOT_AVAILABLE'); const price: Price = { silverLions: item.silverLions * pieces, goldenEagles: item.goldenEagles * pieces }; if (price.silverLions !== offered.silverLions || price.goldenEagles !== offered.goldenEagles) return refused('PRICE_CHANGED'); if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY'); pay(profile, price); grantItem(profile, item, pieces, now); return ACCEPTED; }