Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:
apps/server the local server (was packages/local-service),
package @wto/server
packages/patcher builds the game copy (was packages/build),
package @wto/patcher
patches/exe exe patch definitions (was exe-patches)
patches/gui GUI script patches (was gui-patches)
Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".
The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.
The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.
Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
125 lines
6.3 KiB
TypeScript
125 lines
6.3 KiB
TypeScript
/**
|
|
* 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<string> = 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<string, unknown> = {};
|
|
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;
|
|
}
|
|
}
|