Persist the actual grant in the purchase log and open one native receipt per unread entry. Keep pending receipts during log trimming and resume them when the price table becomes available. Verified TypeScript types, GUI patch application, and Quirrel syntax.
96 lines
4.3 KiB
TypeScript
96 lines
4.3 KiB
TypeScript
/**
|
|
* 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 };
|
|
}
|
|
|
|
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);
|
|
}
|