Files
WT-Offline/packages/local-service/src/store/redeem-codes.ts
T
N0VAN0M4LY 29e5cf2bd8 fix(server): persist squadrons and align inventory wire formats
Read squadron action parameters from the request headers and persist
squadron state on profile refresh. Use floating-point trophy weights
to match the client probability display and assign unique log IDs
when opening multiple chests.

Tighten shop, marketplace, squadron, and cloud settings validation.
Move PBBD and inventory encoding into the protocol modules and share
CSV reading. Update the research and acceptance instructions.

Type checking and all eight executable patch checks passed.
In-game acceptance is pending.
2026-11-25 09:10:09 +08:00

87 lines
3.3 KiB
TypeScript

/**
* Redeem & offline cheat codes (implementation notes section G; implementation notes;
* local verification):
* - PREMIUM-<N>D / WTO-PREM-<N>D / PREM-<N>D / VIP-<N>D (1..3650 days): adds <N> days of Premium Account;
* - WTO-GJN-<N> / GJN-<N> (1..1000000): offline player cheat code adding <N> GJN;
* - WTO-STARTER (once per save): grants 7 days of Premium Account, 1,000 Golden Eagles and 100 GJN.
*/
import type { BlkBlock } from '@wto/blk';
import type { LocalizedText } from '../game-data/datasets/unit-names.ts';
import { addGoldenEagles, balanceOf, setGjn } from '../profile/profile-currency.ts';
import { extendPremiumAccount, hasEntitlement, recordEntitlementPurchase } from '../profile/profile-entitlements.ts';
const STARTER_ENTITLEMENT = 'wto_redeem_starter';
export type RedeemOutcome =
| { readonly ok: true; readonly message: LocalizedText }
| { readonly ok: false; readonly reason: 'INVALID_CODE' | 'ALREADY_REDEEMED'; readonly message: LocalizedText };
export function redeemStoreCode(profile: BlkBlock, rawCode: string, nowFiletime: bigint): RedeemOutcome {
const code = rawCode.trim().toUpperCase();
if (!code) {
return { ok: false, reason: 'INVALID_CODE', message: { en: 'Enter a redeem code.', zh: '请输入兑换码。' } };
}
if (code === 'WTO-STARTER') return redeemStarterCode(profile, nowFiletime);
const premiumMatch = /^(?:PREMIUM|WTO-PREM|PREM|VIP)-(\d{1,4})D$/.exec(code);
if (premiumMatch) {
const days = Number(premiumMatch[1]);
if (days >= 1 && days <= 3_650) {
extendPremiumAccount(profile, days, nowFiletime);
return {
ok: true,
message: {
en: `Redeemed ${code}: +${days} days of Premium Account.`,
zh: `兑换成功:高级账户增加 ${days} 天。`,
},
};
}
}
const gjnMatch = /^(?:WTO-GJN|GJN)-(\d{1,7})$/.exec(code);
if (gjnMatch) {
const amount = Number(gjnMatch[1]);
if (amount >= 1 && amount <= 1_000_000) {
setGjn(profile, balanceOf(profile).gjn + amount);
return {
ok: true,
message: {
en: `Redeemed ${code}: +${amount.toLocaleString('en-US')} GJN.`,
zh: `兑换成功:GJN 增加 ${amount.toLocaleString('en-US')}。`,
},
};
}
}
return {
ok: false,
reason: 'INVALID_CODE',
message: {
en: 'Unknown redeem code. Examples: PREMIUM-7D, PREMIUM-30D, WTO-PREM-90D, WTO-GJN-100, WTO-STARTER.',
zh: '无效的兑换码。可用示例:PREMIUM-7D、PREMIUM-30D、WTO-PREM-90D、WTO-GJN-100、WTO-STARTER。',
},
};
}
function redeemStarterCode(profile: BlkBlock, nowFiletime: bigint): RedeemOutcome {
if (hasEntitlement(profile, STARTER_ENTITLEMENT, nowFiletime)) {
return {
ok: false,
reason: 'ALREADY_REDEEMED',
message: { en: 'This starter code has already been redeemed on this save.', zh: '当前存档已使用过该新手礼包兑换码。' },
};
}
recordEntitlementPurchase(profile, STARTER_ENTITLEMENT);
extendPremiumAccount(profile, 7, nowFiletime);
addGoldenEagles(profile, 1_000);
setGjn(profile, balanceOf(profile).gjn + 100);
return {
ok: true,
message: {
en: 'Redeemed WTO-STARTER: +7d Premium Account, +1,000 Golden Eagles, +100 GJN.',
zh: '兑换成功:高级账户 +7 天、金鹰 +1,000、GJN +100。',
},
};
}