/** * The game's own store requests (yuplay2, implementation notes), sent to api.gaijinent.com * and routed here by wto_ws.dll: * POST /item_info.php GJN prices of the bundle GUIDs (scripts/onlineShop/entitlementsInfo.nut) * POST /item_purchase.php pay=gjn purchase from the in-game payment window * POST /redeem_code.php the in-game redeem code window * The player is identified by the session token or JWT the local login service issued; a * request whose player cannot be identified is refused. */ import type { Account, AccountStore } from '../accounts/account-store.ts'; import { userIdFromJwt, userIdFromToken } from '../login/session-credentials.ts'; import { yuplayAnswer } from '../login/yuplay-answer.ts'; import type { PlayerProfiles } from '../profile/player-profiles.ts'; import { serverTimeNow } from '../profile/server-time.ts'; import type { ServiceRequest, ServiceResponse } from '../transport/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 { buyFreeStoreProduct, buyGjnProduct, canBuyGjnProduct, type GjnPurchaseOutcome } from './store-purchase.ts'; export const YUPLAY_STORE_PATHS: ReadonlySet = new Set(['/item_info.php', '/item_purchase.php', '/redeem_code.php']); export class YuplayStoreApi { readonly #accounts: AccountStore; readonly #profiles: PlayerProfiles; readonly #catalogue: StoreCatalogue; readonly #redeemCodes: RedeemTuning; /** Base of the local store page, e.g. http://127.0.0.1:47115/store; item_info.php `url` points into it. */ readonly #storePageBase: string; constructor(accounts: AccountStore, profiles: PlayerProfiles, catalogue: StoreCatalogue, redeemCodes: RedeemTuning, storePageBase: string) { this.#storePageBase = storePageBase; this.#accounts = accounts; this.#profiles = profiles; this.#catalogue = catalogue; this.#redeemCodes = redeemCodes; } handle(request: ServiceRequest): ServiceResponse { if (request.method !== 'POST') return yuplayAnswer('ERROR', {}, `${request.method} ${request.path} not supported`); const form = new URLSearchParams(request.body.toString('utf8')); if (request.path === '/item_info.php') return this.#itemInfo(form); if (request.path === '/item_purchase.php') return this.#itemPurchase(form); return this.#redeemCode(form); } #itemInfo(form: URLSearchParams): ServiceResponse { const guids = [...form.getAll('guids[]'), ...form.getAll('guids'), ...form.getAll('guid')].filter(Boolean); const account = this.#account(form); const profile = account ? this.#profiles.profileOf(account.userId) : undefined; const now = serverTimeNow(); const items: Record = {}; let numericId = 1; for (const guid of guids) { const product = this.#catalogue.byGuid(guid); if (!product || product.gjnCostCents === undefined) continue; items[guid] = { item_id: numericId++, shop_price: product.gjnCostCents / 100, price_usd: product.gjnCostCents / 100, discount_mul: 1.0, discount_till: 0, title: product.title.en, // The game opens this page for the pack's own store entry (onlineShopModel.nut // doBrowserPurchaseByGuid → get_url_for_purchase → yu_itemInfo.cpp `url`). url: `${this.#storePageBase}?item=${encodeURIComponent(guid)}`, short_desc: product.title.en, shop_price_curr: 'gjn', multi_purch: product.kind !== 'vehicle-pack', can_be_bought: profile ? canBuyGjnProduct(profile, product, now) : true, }; } return yuplayAnswer('OK', { items }, `item_info (${Object.keys(items).length}/${guids.length} items)`); } #itemPurchase(form: URLSearchParams): ServiceResponse { const account = this.#account(form); if (!account) return yuplayAnswer('LOGINERROR', {}, 'item_purchase: unknown token'); const rawPay = form.get('pay'); // The free offline payment method passes YU2_PAY_NONE, so PurchaseAction::run omits `pay`. 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}`); let outcome: GjnPurchaseOutcome = { ok: false, reason: 'NOT_FOUND' }; const now = serverTimeNow(); this.#profiles.updateWhen(account.userId, (saved) => { outcome = payMethod === 'free' ? buyFreeStoreProduct(saved, product, now) : buyGjnProduct(saved, product, now); return outcome.ok; }); 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`); } #redeemCode(form: URLSearchParams): ServiceResponse { const account = this.#account(form); if (!account) return yuplayAnswer('LOGINERROR', {}, 'redeem_code: unknown token'); const code = form.get('code') ?? ''; let outcome: RedeemOutcome | undefined; const now = serverTimeNow(); this.#profiles.updateWhen(account.userId, (saved) => { outcome = redeemStoreCode(saved, code, now, this.#redeemCodes); return outcome.ok; }); if (outcome?.ok) return yuplayAnswer('OK', {}, `redeem_code ${code} -> OK`); return yuplayAnswer(outcome?.reason === 'ALREADY_REDEEMED' ? 'ALREADY' : 'NOTFOUND', {}, `redeem_code ${code} -> ${outcome?.reason ?? 'INVALID_CODE'}`); } #account(form: URLSearchParams): Account | undefined { const userId = userIdFromToken(form.get('token') ?? undefined) ?? userIdFromJwt(form.get('jwt') ?? undefined); return userId ? this.#accounts.byUserId(userId) : undefined; } }