Files
WT-Offline/apps/server/src/char/actions/store-actions.ts
T
N0VAN0M4LY 12a573a333 feat(store): default free offline payment label to Direct Claim and make it tunable
Add tuning/store.jsonc freePaymentLabel (default Direct Claim in Chinese and English) and apply it consistently across the local web store, local marketplace, and the in-game payment dialog via cln_get_entitlements_price_ex top-level metadata.
2026-12-04 19:55:04 +08:00

99 lines
5.3 KiB
TypeScript

/**
* Store char actions:
* - cln_get_entitlements_price_ex: encodes the store catalogue into Gaijin's entitlements BLK;
* - cln_buy_entitlement: Golden-Eagle entitlement purchase (aces.exe 0x17a0f50 -> 0x2a1240
* sends `entitlement` and `costGold` in the HTTP headers, job type 5, answer applied as a
* full profile update). Note: `cln_buy_entitlement_and_get_reward_for_day` is not referenced
* anywhere in 2.61.0.37 GUI scripts and is intentionally not registered.
*/
import { appendParam, emptyBlock, setParam, type BlkBlock } from '@wto/blk';
import type { AccountStore } from '../../accounts/account-store.ts';
import type { LocalizedText } from '../../game-data/datasets/unit-names.ts';
import { clientProfile } from '../../profile/client-profile.ts';
import type { PlayerProfiles } from '../../profile/player-profiles.ts';
import { serverTimeNow } from '../../profile/server-time.ts';
import type { RuleOutcome } from '../../research/rule-outcome.ts';
import type { StoreCatalogue, StoreProduct } from '../../store/store-catalogue.ts';
import { buyGoldEntitlement } from '../../store/store-purchase.ts';
import { freePaymentLabelForStore } from '../../store/store-tuning.ts';
import type { CharActionHandler } from '../char-action.ts';
import { withAccount } from './account-context.ts';
import { headerOrPayloadInt, headerOrPayloadString } from './payload-params.ts';
export function entitlementsPriceEx(
store: StoreCatalogue,
freePaymentLabel?: LocalizedText,
): CharActionHandler {
const label = freePaymentLabel ?? freePaymentLabelForStore(store);
const table = buildEntitlementsPriceBlk(store.allProducts(), label);
return () => ({ kind: 'data', blk: table });
}
export function buyEntitlementAction(profiles: PlayerProfiles, accounts: AccountStore, store: StoreCatalogue): CharActionHandler {
return (context) => withAccount(context, accounts, (account) => {
const entitlement = headerOrPayloadString(context, 'entitlement');
const offeredGold = headerOrPayloadInt(context, 'costGold');
if (!entitlement || offeredGold === undefined || offeredGold < 0) {
return { kind: 'error', code: 'BAD_PARAMS' };
}
let outcome: RuleOutcome = { ok: false, refusal: 'BAD_PARAMS' };
const now = serverTimeNow();
const updated = profiles.updateWhen(account.userId, (saved) => {
outcome = buyGoldEntitlement(saved, store, entitlement, offeredGold, now);
return outcome.ok;
});
if (updated) return { kind: 'data', blk: clientProfile(updated, account) };
return { kind: 'error', code: outcome.ok ? 'BAD_PARAMS' : outcome.refusal };
});
}
function buildEntitlementsPriceBlk(
products: readonly StoreProduct[],
freePaymentLabel: LocalizedText,
): BlkBlock {
const root = emptyBlock();
// Top-level string params are ignored by stock blockCount() enumeration and read by the
// free-offline-payment-method GUI patch to localize the no-GJN payment option.
setParam(root, 'wtoFreePaymentLabelZh', { type: 'string', value: freePaymentLabel.zh });
setParam(root, 'wtoFreePaymentLabelEn', { type: 'string', value: freePaymentLabel.en });
for (const product of products) {
const block = emptyBlock(product.id);
setParam(block, 'chapter', { type: 'string', value: product.chapter });
if (product.kind === 'eagles') {
setParam(block, 'onlinePurchase', { type: 'bool', value: true });
setParam(block, 'group', { type: 'string', value: 'eagles100' });
setParam(block, 'useGroupAmount', { type: 'bool', value: true });
setParam(block, 'goldIncome', { type: 'int', value: product.goldIncome! });
} else if (product.kind === 'warpoints') {
setParam(block, 'group', { type: 'string', value: '1KWP' });
setParam(block, 'useGroupAmount', { type: 'bool', value: true });
setParam(block, 'wpIncome', { type: 'int', value: product.silverIncome! });
setParam(block, 'goldCost', { type: 'int', value: product.goldCost! });
} else if (product.kind === 'premium-gold') {
setParam(block, 'alias', { type: 'string', value: 'PremiumAccount' });
setParam(block, 'group', { type: 'string', value: 'PremiumAccount' });
setParam(block, 'ttl', { type: 'int', value: product.premiumDays! });
setParam(block, 'goldCost', { type: 'int', value: product.goldCost! });
} else if (product.kind === 'premium-gjn') {
setParam(block, 'onlinePurchase', { type: 'bool', value: true });
setParam(block, 'alias', { type: 'string', value: 'PremiumAccount' });
setParam(block, 'group', { type: 'string', value: 'PremiumAccount' });
setParam(block, 'ttl', { type: 'int', value: product.premiumDays! });
} else if (product.kind === 'vehicle-pack' && product.vehicles) {
setParam(block, 'onlinePurchase', { type: 'bool', value: true });
setParam(block, 'wtoTitleEn', { type: 'string', value: product.title.en });
setParam(block, 'wtoTitleZh', { type: 'string', value: product.title.zh });
for (const vehicle of product.vehicles) {
appendParam(block, 'aircraftGift', { type: 'string', value: vehicle.name });
}
if (product.goldIncome) setParam(block, 'goldIncome', { type: 'int', value: product.goldIncome });
if (product.silverIncome) setParam(block, 'wpIncome', { type: 'int', value: product.silverIncome });
}
root.blocks.push(block);
}
return root;
}