Files
WT-Offline/packages/local-service/src/inventory/web/app.js
T
N0VAN0M4LY 9cf7df5299 feat(store): open store pages in-game and centralize economy tuning
Use the native embedded browser and pass the game language through
auto_local. Share web assets and extract vehicle icons, flags, and
localized names from the installed client.

Load and validate per-domain JSONC tuning and merge local overrides
by field. Use official store USD prices for GJN products, keep GJN
balances in integer cents, and split the native and web store
handlers by protocol. Multi-vehicle packs are still listed as single
vehicles at this point.

Type checking, patch checks, and configuration loading passed.
In-game acceptance is pending.
2026-11-25 11:01:05 +08:00

147 lines
7.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Local marketplace page, shown in the game's built-in browser.
// The game opens it as <marketplaceURL>?a=<app id>[&viewitem&n=<market_hash_name>] (inventoryClient.nut).
'use strict';
const T = {
zh: {
title: 'WTOffline 本地交易所', hint: '买到的兑换券直接进游戏库存(机库 → 道具 → 库存)', toStore: '前往商店',
eagles: '金鹰', lions: '银狮',
catAll: '全部', catVehicle: '载具券', catSkin: '涂装券', catAttachable: '挂件券', catDecal: '贴花券', catUnlock: '头像与解锁券',
search: '搜索名称或编号', allCountries: '全部国家', allClasses: '全部兵种', rank: '{rank} 级',
held: '库存 ×{n}', activated: '已拥有', buy: '购买', pickAccount: '请先选择账号', empty: '没有符合条件的兑换券',
},
en: {
title: 'WTOffline Marketplace', hint: 'Coupons go straight to your inventory (Items → Inventory in the hangar)', toStore: 'Go to Store',
eagles: 'Golden Eagles', lions: 'Silver Lions',
catAll: 'All', catVehicle: 'Vehicles', catSkin: 'Camouflages', catAttachable: 'Decorations', catDecal: 'Decals', catUnlock: 'Icons and unlocks',
search: 'Search name or id', allCountries: 'All nations', allClasses: 'All types', rank: 'Rank {rank}',
held: 'In inventory ×{n}', activated: 'Owned', buy: 'Buy', pickAccount: 'Pick an account first', empty: 'Nothing matches the filters',
},
};
// Vehicle class of a listing -> the game's own type name (lang/menu.csv).
const CLASS_LABELS = {
aircraft: 'mainmenu/type_aircraft', helicopter: 'mainmenu/type_helicopter', tank: 'mainmenu/type_tank',
ship: 'mainmenu/type_ship_only', boat: 'mainmenu/type_boat_only',
};
const ROMAN = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
const t = (key, params) => WtoUi.tr(T, key, params);
const $ = (id) => document.getElementById(id);
const wantedItem = WtoUi.query.get('n') || '';
let state = { accounts: [], currentUserId: '', balance: { gjnCents: 0, goldenEagles: 0, silverLions: 0 }, listings: [] };
let activeCat = 'all';
function applyStaticTexts() {
for (const el of document.querySelectorAll('[data-t]')) el.textContent = t(el.dataset.t);
$('searchInput').placeholder = t('search');
$('searchInput').value = wantedItem;
$('storeLink').href = WtoUi.pageLink('/store');
const classSelect = $('classFilter');
classSelect.appendChild(new Option(t('allClasses'), ''));
for (const [unitClass, key] of Object.entries(CLASS_LABELS)) classSelect.appendChild(new Option(WtoUi.label(key), unitClass));
}
async function loadState(userId) {
state = await WtoUi.getJson('/trade.gaijin.net/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
if (userId && !state.currentUserId) return loadState('');
if (state.currentUserId) WtoUi.saveAccount(state.currentUserId);
WtoUi.fillAccountSelect($('accountSelect'), state.accounts, state.currentUserId);
$('gjnVal').textContent = WtoUi.gjn(state.balance.gjnCents);
$('goldVal').textContent = WtoUi.num(state.balance.goldenEagles);
$('silverVal').textContent = WtoUi.num(state.balance.silverLions);
renderCountries();
renderGrid();
}
function renderCountries() {
const select = $('countryFilter');
const previous = select.value;
select.innerHTML = '';
select.appendChild(new Option(t('allCountries'), ''));
for (const country of [...new Set(state.listings.map((l) => l.country).filter(Boolean))]) {
select.appendChild(new Option(WtoUi.label(country), country, false, country === previous));
}
}
function matches(listing) {
const country = $('countryFilter').value;
const unitClass = $('classFilter').value;
const query = $('searchInput').value.trim().toLowerCase();
if (activeCat !== 'all' && listing.category !== activeCat) return false;
if (country && listing.country !== country) return false;
if (unitClass && listing.unitClass !== unitClass) return false;
return !query || [listing.title.zh, listing.title.en, listing.marketHashName, String(listing.itemdefId)].some((s) => s.toLowerCase().includes(query));
}
function renderGrid() {
const grid = $('grid');
grid.innerHTML = '';
const listings = state.listings.filter(matches);
if (listings.length === 0) grid.innerHTML = '<div class="empty">' + WtoUi.escapeHtml(t('empty')) + '</div>';
for (const listing of listings) grid.appendChild(listingCard(listing));
}
function listingCard(listing) {
const card = document.createElement('div');
card.className = 'card' + (wantedItem && listing.marketHashName === wantedItem ? ' highlight' : '');
const head = document.createElement('div');
head.className = 'card-head';
if (listing.category === 'vehicle') head.appendChild(WtoUi.iconImg('vehicle', listing.targetKey, 'vehicle-icon'));
const titles = document.createElement('div');
titles.innerHTML = '<h3 class="card-title quality"></h3><div class="card-sub"></div>';
const title = titles.querySelector('.card-title');
title.textContent = WtoUi.text(listing.title);
if (listing.nameColor) title.style.setProperty('--quality', '#' + listing.nameColor);
const sub = titles.querySelector('.card-sub');
if (listing.country) sub.appendChild(WtoUi.iconImg('flag', listing.country, 'flag'));
const parts = [];
if (listing.country) parts.push(WtoUi.label(listing.country));
if (listing.rank) parts.push(t('rank', { rank: ROMAN[listing.rank] || listing.rank }));
if (!parts.length) parts.push(WtoUi.text(listing.subtitle));
sub.appendChild(document.createTextNode(parts.join(' · ')));
head.appendChild(titles);
card.appendChild(head);
const badges = document.createElement('div');
badges.className = 'badges';
const badge = (text, owned) => badges.appendChild(Object.assign(document.createElement('span'), { className: 'badge' + (owned ? ' owned' : ''), textContent: text }));
badge('#' + listing.itemdefId, false);
if (listing.couponCount > 0) badge(t('held', { n: listing.couponCount }), true);
if (listing.alreadyActivated) badge(t('activated'), true);
card.appendChild(badges);
const foot = document.createElement('div');
foot.className = 'card-foot';
foot.innerHTML = '<span class="price"></span><button class="buy"></button>';
foot.querySelector('.price').textContent = WtoUi.gjn(listing.gjnCostCents) + ' GJN';
const button = foot.querySelector('button');
button.textContent = state.currentUserId ? t('buy') : t('pickAccount');
button.disabled = !state.currentUserId;
button.onclick = () => buy(listing.itemdefId);
card.appendChild(foot);
return card;
}
async function buy(itemdefId) {
const result = await WtoUi.postJson('/trade.gaijin.net/api/buy', { userId: state.currentUserId, itemdefId, quantity: 1 });
WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok);
if (result.ok) await loadState(state.currentUserId);
}
$('tabs').onclick = (event) => {
const tab = event.target.closest('.tab');
if (!tab) return;
for (const other of document.querySelectorAll('.tab')) other.classList.toggle('active', other === tab);
activeCat = tab.dataset.cat;
renderGrid();
};
$('accountSelect').onchange = (event) => loadState(event.target.value);
$('countryFilter').onchange = renderGrid;
$('classFilter').onchange = renderGrid;
$('searchInput').oninput = renderGrid;
WtoUi.loadLabels().then(() => {
applyStaticTexts();
return loadState(WtoUi.savedAccount());
});