/** * Redeem & offline cheat codes (implementation notes section G; implementation notes; * local verification): * - PREMIUM-D / WTO-PREM-D / PREM-D / VIP-D (1..3650 days): adds days of Premium Account; * - WTO-GJN- / GJN- (1..1000000): offline player cheat code adding 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。', }, }; }