Files
WT-Offline/apps/server/src/store/store-purchase.ts
T
N0VAN0M4LY 27fc535301 Split the repository into apps, packages and patches
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:

  apps/server        the local server (was packages/local-service),
                     package @wto/server
  packages/patcher   builds the game copy (was packages/build),
                     package @wto/patcher
  patches/exe        exe patch definitions (was exe-patches)
  patches/gui        GUI script patches (was gui-patches)

Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".

The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.

The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.


Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
2026-11-29 03:20:54 +08:00

127 lines
5.5 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 };
}
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);
}