// Local marketplace page, shown in the game's built-in browser. // The game opens it as ?a=[&viewitem&n=] (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 = '
' + WtoUi.escapeHtml(t('empty')) + '
'; 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 = '

'; 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 = ''; 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()); });