Files
WT-Offline/packages/local-service/src/store/store-purchase.ts
T
N0VAN0M4LY 98812fa9c2 feat(store): add store and marketplace item views and image sources
Add catalogue filters, item detail views, and purchase and resale
actions. Resolve images from the player's asset pack, public image
URLs, or local game data. Add a utility for creating the optional
local asset pack.

Type checking and JavaScript syntax checks passed. Embedded browser
appearance and in-game behavior still need acceptance.
2026-11-26 07:12:38 +08:00

81 lines
3.7 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 { 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 {
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);
for (const part of vehicle.parts) {
if (!ownsVehicle(profile, part)) addOwnedVehicle(profile, part);
}
}
}
}