/** * Store purchase rules: * - buyGoldEntitlement: Golden-Eagle-priced entitlements (Premium Account days, Silver Lions packs) * bought through cln_buy_entitlement; * - buyGjnProduct: GJN-priced products (Golden Eagles packs, GJN Premium Account, vehicle packs) * bought through item_purchase.php or the local web store. */ import type { BlkBlock } from '@wto/blk'; import { addGoldenEagles, addSilverLions, balanceOf, canAfford, pay, payGjnCents } from '../profile/profile-currency.ts'; import { extendPremiumAccount, hasEntitlement, recordEntitlementPurchase } from '../profile/profile-entitlements.ts'; import { addOwnedVehicle, ownsVehicle } from '../profile/profile-hangar.ts'; import { appendEntitlementPurchase } from '../profile/profile-trophies-log.ts'; import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import type { StoreCatalogue, StoreProduct } from './store-catalogue.ts'; export function buyGoldEntitlement( profile: BlkBlock, catalogue: StoreCatalogue, entitlementId: string, offeredGoldCost: number, nowFiletime: bigint, ): RuleOutcome { const product = catalogue.byId(entitlementId); if (!product || product.goldCost === undefined || !Number.isSafeInteger(offeredGoldCost) || offeredGoldCost < 0) { return refused('BAD_PARAMS'); } if (offeredGoldCost !== product.goldCost) return refused('PRICE_CHANGED'); const price = { silverLions: 0, goldenEagles: product.goldCost }; if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY'); pay(profile, price); grantProductRewards(profile, product, nowFiletime); return ACCEPTED; } export type GjnPurchaseOutcome = | { readonly ok: true } | { readonly ok: false; readonly reason: 'NOT_FOUND' | 'ALREADY_BOUGHT' | 'NOT_ENOUGH_GJN' }; export function canBuyGjnProduct(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): boolean { if (product.gjnCostCents === undefined) return false; if (product.kind === 'vehicle-pack' && product.vehicles) { if (hasEntitlement(profile, product.id, nowFiletime)) return false; return !product.vehicles.every((vehicle) => ownsVehicle(profile, vehicle.name)); } return true; } export function buyGjnProduct(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): GjnPurchaseOutcome { if (product.gjnCostCents === undefined) return { ok: false, reason: 'NOT_FOUND' }; if (!canBuyGjnProduct(profile, product, nowFiletime)) return { ok: false, reason: 'ALREADY_BOUGHT' }; if (balanceOf(profile).gjnCents < product.gjnCostCents) return { ok: false, reason: 'NOT_ENOUGH_GJN' }; payGjnCents(profile, product.gjnCostCents); grantProductRewards(profile, product, nowFiletime); return { ok: true }; } /** Grant a store product using the free offline payment method. */ export function buyFreeStoreProduct(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): GjnPurchaseOutcome { if (product.gjnCostCents === undefined) return { ok: false, reason: 'NOT_FOUND' }; if (!canBuyGjnProduct(profile, product, nowFiletime)) return { ok: false, reason: 'ALREADY_BOUGHT' }; grantProductRewards(profile, product, nowFiletime); return { ok: true }; } export type StoreCheckoutOutcome = | { readonly ok: true; readonly costCents: number } | { readonly ok: false; readonly reason: 'NOT_FOUND' | 'ALREADY_BOUGHT'; readonly productId: string } | { readonly ok: false; readonly reason: 'NOT_ENOUGH_GJN'; readonly costCents: number }; /** Stage every grant before committing the rewards and charge to the player's profile. */ export function buyStoreCart( profile: BlkBlock, products: readonly StoreProduct[], payMethod: 'gjn' | 'free', nowFiletime: bigint, ): StoreCheckoutOutcome { const draft = structuredClone(profile); let costCents = 0; for (const product of products) { const price = product.gjnCostCents; if (price === undefined) return { ok: false, reason: 'NOT_FOUND', productId: product.id }; if (!canBuyGjnProduct(draft, product, nowFiletime)) return { ok: false, reason: 'ALREADY_BOUGHT', productId: product.id }; grantProductRewards(draft, product, nowFiletime); costCents += price; } if (payMethod === 'gjn') { if (balanceOf(draft).gjnCents < costCents) return { ok: false, reason: 'NOT_ENOUGH_GJN', costCents }; payGjnCents(draft, costCents); } profile.params = draft.params; profile.blocks = draft.blocks; return { ok: true, costCents: payMethod === 'gjn' ? costCents : 0 }; } function grantProductRewards(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): void { const before = balanceOf(profile); const newVehicles: string[] = []; if (product.id !== 'PremiumAccount') recordEntitlementPurchase(profile, product.id); if (product.goldIncome) addGoldenEagles(profile, product.goldIncome); if (product.silverIncome) addSilverLions(profile, product.silverIncome); if (product.premiumDays) extendPremiumAccount(profile, product.premiumDays, nowFiletime); if (product.vehicles) { for (const vehicle of product.vehicles) { if (!ownsVehicle(profile, vehicle.name)) { addOwnedVehicle(profile, vehicle.name); newVehicles.push(vehicle.name); } for (const part of vehicle.parts) { if (!ownsVehicle(profile, part)) addOwnedVehicle(profile, part); } } } const after = balanceOf(profile); appendEntitlementPurchase(profile, { productId: product.id, goldenEagleCost: product.goldCost, goldenEagles: after.goldenEagles - before.goldenEagles, silverLions: after.silverLions - before.silverLions, premiumDays: product.premiumDays ?? 0, vehicles: newVehicles, }, nowFiletime); }