Expose free payment in the in-game dialog, local store, and marketplace. Grant the selected item without debiting GJN and reject unknown payment methods. Apply the configured per-purchase limit consistently. Type checking, binary and GUI patch checks, and local purchase checks passed. In-game acceptance is pending.
81 lines
3.8 KiB
TypeScript
81 lines
3.8 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 };
|
|
}
|
|
|
|
/** Direct free purchase ("老福爷付过了") for players who prefer not to farm GJN. */
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|