feat(economy): offer a free offline payment option
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.
This commit is contained in:
1 parent
e1468df52b
commit
4d8229ae92
9 files changed
+161
-56
No files matched your search
@@ -55,6 +55,14 @@ export function buyGjnProduct(profile: BlkBlock, product: StoreProduct, nowFilet
|
||||
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);
|
||||
|
||||
@@ -17,7 +17,7 @@ import { jsonResponse, parseJsonBody, requireWebActionAccount, selectWebViewAcco
|
||||
import { describeRedeemCodes, gjnText, redeemStoreCode, type RedeemOutcome } from './redeem-codes.ts';
|
||||
import type { RedeemTuning } from './redeem-tuning.ts';
|
||||
import type { StoreCatalogue, StoreProduct } from './store-catalogue.ts';
|
||||
import { buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
import { buyFreeStoreProduct, buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
|
||||
const NO_ACCOUNT: LocalizedText = { zh: '没有找到这个账号,请先在右上角选择账号。', en: 'Account not found. Pick an account at the top right first.' };
|
||||
|
||||
@@ -67,13 +67,19 @@ export class StoreWebApi {
|
||||
if (!product || product.gjnCostCents === undefined) {
|
||||
return jsonResponse({ ok: false, message: { zh: '没有找到这个商品。', en: 'Product not found.' } }, 'store web buy: unknown product');
|
||||
}
|
||||
if (body.payMethod !== undefined && body.payMethod !== 'gjn' && body.payMethod !== 'free') {
|
||||
return jsonResponse({ ok: false, message: { zh: '不支持这种支付方式。', en: 'Unsupported payment method.' } }, 'store web buy: bad payMethod');
|
||||
}
|
||||
const isFree = body.payMethod === 'free';
|
||||
|
||||
let outcome: GjnPurchaseOutcome = { ok: false, reason: 'NOT_FOUND' };
|
||||
const now = serverTimeNow();
|
||||
this.#profiles.updateWhen(account.userId, (saved) => {
|
||||
outcome = buyGjnProduct(saved, product, now);
|
||||
outcome = isFree ? buyFreeStoreProduct(saved, product, now) : buyGjnProduct(saved, product, now);
|
||||
return outcome.ok;
|
||||
});
|
||||
return jsonResponse({ ok: outcome.ok, message: purchaseMessage(outcome, product) }, `store web buy ${product.id}: ${outcome.ok ? 'OK' : outcome.reason}`);
|
||||
const modeTag = isFree ? 'free' : 'gjn';
|
||||
return jsonResponse({ ok: outcome.ok, message: purchaseMessage(outcome, product, isFree) }, `store web buy ${product.id} (${modeTag}): ${outcome.ok ? 'OK' : outcome.reason}`);
|
||||
}
|
||||
|
||||
#redeem(request: ServiceRequest): ServiceResponse {
|
||||
@@ -91,16 +97,21 @@ export class StoreWebApi {
|
||||
}
|
||||
}
|
||||
|
||||
function purchaseMessage(outcome: GjnPurchaseOutcome, product: StoreProduct): LocalizedText {
|
||||
function purchaseMessage(outcome: GjnPurchaseOutcome, product: StoreProduct, isFree: boolean): LocalizedText {
|
||||
const cost = gjnText(product.gjnCostCents ?? 0);
|
||||
if (outcome.ok) {
|
||||
return {
|
||||
zh: `已购买「${product.title.zh}」,花费 ${cost} GJN。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Bought "${product.title.en}" for ${cost} GJN. Close the store window to refresh the hangar.`,
|
||||
};
|
||||
return isFree
|
||||
? {
|
||||
zh: `老福爷付过了!已直接获得「${product.title.zh}」。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Paid by 老福爷! Claimed "${product.title.en}". Close the store window to refresh the hangar.`,
|
||||
}
|
||||
: {
|
||||
zh: `已购买「${product.title.zh}」,花费 ${cost} GJN。关闭商店窗口后机库会自动刷新。`,
|
||||
en: `Bought "${product.title.en}" for ${cost} GJN. Close the store window to refresh the hangar.`,
|
||||
};
|
||||
}
|
||||
if (outcome.reason === 'NOT_ENOUGH_GJN') {
|
||||
return { zh: `GJN 不够:需要 ${cost} GJN,可在"兑换码"页用 GJN 充值码补充。`, en: `Not enough GJN: ${cost} GJN needed. Use a GJN code on the Redeem tab.` };
|
||||
return { zh: `GJN 不够:需要 ${cost} GJN。可直接点「老福爷付过了」,或在"兑换码"页用 GJN 充值码补充。`, en: `Not enough GJN: ${cost} GJN needed. Use "老福爷付过了" or a GJN code on the Redeem tab.` };
|
||||
}
|
||||
if (outcome.reason === 'ALREADY_BOUGHT') return { zh: '你已经拥有该礼包内的载具。', en: 'You already own the vehicle(s) in this pack.' };
|
||||
return { zh: '这个商品现在买不了。', en: 'This product cannot be bought right now.' };
|
||||
|
||||
@@ -8,7 +8,7 @@ const T = {
|
||||
tabPacks: '礼包载具', tabPremium: '高级账户', tabEagles: '金鹰', tabRedeem: '兑换码',
|
||||
search: '搜索礼包或载具名称', allCountries: '全部国家', allRanks: '全部等级', rank: '{rank} 级',
|
||||
includesUnit: '包含载具:{name}', containsEagles: '附赠 {n} 金鹰', containsSilver: '附赠 {n} 银狮', containsPremium: '附赠 {days} 天高级账户', premiumDays: '{days} 天高级账户',
|
||||
buy: '购买', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品',
|
||||
buy: '购买', freeBuy: '老福爷付过了', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品',
|
||||
},
|
||||
en: {
|
||||
title: 'WTOffline Store', hint: 'Close this window after buying to refresh the hangar', toMarket: 'Go to Marketplace',
|
||||
@@ -16,7 +16,7 @@ const T = {
|
||||
tabPacks: 'Vehicle packs', tabPremium: 'Premium Account', tabEagles: 'Golden Eagles', tabRedeem: 'Redeem code',
|
||||
search: 'Search packs or vehicles', allCountries: 'All nations', allRanks: 'All ranks', rank: 'Rank {rank}',
|
||||
includesUnit: 'Vehicle: {name}', containsEagles: '+{n} Golden Eagles', containsSilver: '+{n} Silver Lions', containsPremium: '+{days} days of Premium Account', premiumDays: '{days} days of Premium Account',
|
||||
buy: 'Buy', owned: 'Owned', pickAccount: 'Pick an account first', redeem: 'Redeem', codePlaceholder: 'Enter a code', empty: 'Nothing matches the filters',
|
||||
buy: 'Buy', freeBuy: '老福爷付过了', owned: 'Owned', pickAccount: 'Pick an account first', redeem: 'Redeem', codePlaceholder: 'Enter a code', empty: 'Nothing matches the filters',
|
||||
},
|
||||
};
|
||||
const ROMAN = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
|
||||
@@ -109,15 +109,20 @@ function productCard(product) {
|
||||
|
||||
const foot = document.createElement('div');
|
||||
foot.className = 'card-foot';
|
||||
foot.innerHTML = '<span class="price"></span>';
|
||||
foot.innerHTML = '<span class="price"></span><div class="buy-actions"></div>';
|
||||
foot.querySelector('.price').textContent = WtoUi.gjn(product.gjnCostCents) + ' GJN';
|
||||
const btn = Object.assign(document.createElement('button'), {
|
||||
className: 'buy',
|
||||
textContent: product.owned ? t('owned') : (state.activeUserId ? t('buy') : t('pickAccount')),
|
||||
disabled: product.owned || !state.activeUserId,
|
||||
});
|
||||
if (!btn.disabled) btn.onclick = () => buy(product.id);
|
||||
foot.appendChild(btn);
|
||||
const actions = foot.querySelector('.buy-actions');
|
||||
if (product.owned || !state.activeUserId) {
|
||||
actions.appendChild(Object.assign(document.createElement('button'), {
|
||||
className: 'buy', textContent: product.owned ? t('owned') : t('pickAccount'), disabled: true,
|
||||
}));
|
||||
} else {
|
||||
const gjnBtn = Object.assign(document.createElement('button'), { className: 'buy', textContent: t('buy') });
|
||||
gjnBtn.onclick = () => buy(product.id, 'gjn');
|
||||
const freeBtn = Object.assign(document.createElement('button'), { className: 'buy free', textContent: t('freeBuy') });
|
||||
freeBtn.onclick = () => buy(product.id, 'free');
|
||||
actions.append(gjnBtn, freeBtn);
|
||||
}
|
||||
card.appendChild(foot);
|
||||
return card;
|
||||
}
|
||||
@@ -156,8 +161,8 @@ function renderRedeemHelp() {
|
||||
for (const line of state.redeemHelp) list.appendChild(Object.assign(document.createElement('li'), { textContent: WtoUi.text(line) }));
|
||||
}
|
||||
|
||||
async function buy(productId) {
|
||||
const result = await WtoUi.postJson('/store/api/buy', { userId: state.activeUserId, productId });
|
||||
async function buy(productId, payMethod) {
|
||||
const result = await WtoUi.postJson('/store/api/buy', { userId: state.activeUserId, productId, payMethod });
|
||||
WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok);
|
||||
await loadState(state.activeUserId);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import type { ServiceRequest, ServiceResponse } from '../server/exchange.ts';
|
||||
import { gjnText, redeemStoreCode, type RedeemOutcome } from './redeem-codes.ts';
|
||||
import type { RedeemTuning } from './redeem-tuning.ts';
|
||||
import type { StoreCatalogue } from './store-catalogue.ts';
|
||||
import { buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
import { buyFreeStoreProduct, buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts';
|
||||
|
||||
export const YUPLAY_STORE_PATHS: ReadonlySet<string> = new Set(['/item_info.php', '/item_purchase.php', '/redeem_code.php']);
|
||||
|
||||
@@ -71,8 +71,12 @@ export class YuplayStoreApi {
|
||||
#itemPurchase(form: URLSearchParams): ServiceResponse {
|
||||
const account = this.#account(form);
|
||||
if (!account) return yuplayAnswer('LOGINERROR', {}, 'item_purchase: unknown token');
|
||||
const payMethod = (form.get('pay') ?? 'gjn').toLowerCase();
|
||||
if (payMethod !== 'gjn') return yuplayAnswer('BADPAYMENT', {}, `item_purchase: unsupported pay=${payMethod}`);
|
||||
const rawPay = form.get('pay');
|
||||
// YU2_PAY_NONE ("老福爷付过了", gui-patches/online-shop/free-offline-payment-method) omits `pay` in PurchaseAction::run.
|
||||
const payMethod = rawPay === null ? 'free' : rawPay.toLowerCase();
|
||||
if (payMethod !== 'gjn' && payMethod !== 'free') {
|
||||
return yuplayAnswer('BADPAYMENT', {}, `item_purchase: unsupported pay=${rawPay}`);
|
||||
}
|
||||
const guid = form.get('guid') ?? '';
|
||||
const product = this.#catalogue.byGuid(guid);
|
||||
if (!product) return yuplayAnswer('NOTFOUND', {}, `item_purchase: unknown guid ${guid}`);
|
||||
@@ -80,10 +84,15 @@ export class YuplayStoreApi {
|
||||
let outcome: GjnPurchaseOutcome = { ok: false, reason: 'NOT_FOUND' };
|
||||
const now = serverTimeNow();
|
||||
this.#profiles.updateWhen(account.userId, (saved) => {
|
||||
outcome = buyGjnProduct(saved, product, now);
|
||||
outcome = payMethod === 'free'
|
||||
? buyFreeStoreProduct(saved, product, now)
|
||||
: buyGjnProduct(saved, product, now);
|
||||
return outcome.ok;
|
||||
});
|
||||
if (outcome.ok) return yuplayAnswer('OK', {}, `item_purchase ${product.id} (${gjnText(product.gjnCostCents ?? 0)} GJN)`);
|
||||
if (outcome.ok) {
|
||||
const costSummary = payMethod === 'free' ? 'free' : `${gjnText(product.gjnCostCents ?? 0)} GJN`;
|
||||
return yuplayAnswer('OK', {}, `item_purchase ${product.id} (${costSummary})`);
|
||||
}
|
||||
if (outcome.reason === 'NOT_ENOUGH_GJN') return yuplayAnswer('NOMONEY', {}, `item_purchase ${product.id}: insufficient GJN`);
|
||||
if (outcome.reason === 'ALREADY_BOUGHT') return yuplayAnswer('ALREADY', {}, `item_purchase ${product.id}: already owned`);
|
||||
return yuplayAnswer('NOTFOUND', {}, `item_purchase ${product.id}: not available`);
|
||||
|
||||
Reference in new issue
Block a user