// Local store page, shown in the game's built-in browser. 'use strict'; const T = { zh: { title: 'WTOffline 本地商店', hint: '买完关闭本窗口,机库会自动刷新', toMarket: '前往交易所', eagles: '金鹰', lions: '银狮', premium: '高级账户', premiumOff: '未开通', premiumLeft: '剩余 {days} 天', tabPacks: '礼包载具', tabPremium: '高级账户', tabEagles: '金鹰', tabRedeem: '兑换码', search: '搜索载具名称', allCountries: '全部国家', allRanks: '全部等级', rank: '{rank} 级', containsEagles: '附赠 {n} 金鹰', containsPremium: '附赠 {days} 天高级账户', premiumDays: '{days} 天高级账户', buy: '购买', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品', }, en: { title: 'WTOffline Store', hint: 'Close this window after buying to refresh the hangar', toMarket: 'Go to Marketplace', eagles: 'Golden Eagles', lions: 'Silver Lions', premium: 'Premium', premiumOff: 'inactive', premiumLeft: '{days} days left', tabPacks: 'Vehicle packs', tabPremium: 'Premium Account', tabEagles: 'Golden Eagles', tabRedeem: 'Redeem code', search: 'Search vehicles', allCountries: 'All nations', allRanks: 'All ranks', rank: 'Rank {rank}', containsEagles: '+{n} Golden Eagles', 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', }, }; 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); let state = { accounts: [], activeUserId: '', balance: { gjnCents: 0, goldenEagles: 0, silverLions: 0 }, premiumDays: 0, products: [], redeemHelp: [] }; let currentTab = 'vehicle-pack'; function applyStaticTexts() { for (const el of document.querySelectorAll('[data-t]')) el.textContent = t(el.dataset.t); $('searchInput').placeholder = t('search'); $('codeInput').placeholder = t('codePlaceholder'); $('marketLink').href = WtoUi.pageLink('/trade.gaijin.net/'); } async function loadState(userId) { state = await WtoUi.getJson('/store/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : '')); if (userId && !state.activeUserId) return loadState(''); if (state.activeUserId) WtoUi.saveAccount(state.activeUserId); renderHeader(); renderFilters(); renderContent(); } function renderHeader() { WtoUi.fillAccountSelect($('accountSelect'), state.accounts, state.activeUserId); $('gjnVal').textContent = WtoUi.gjn(state.balance.gjnCents); $('goldVal').textContent = WtoUi.num(state.balance.goldenEagles); $('silverVal').textContent = WtoUi.num(state.balance.silverLions); $('premVal').textContent = state.premiumDays > 0 ? t('premiumLeft', { days: state.premiumDays }) : t('premiumOff'); } function fillSelect(select, firstText, values, textOf) { const previous = select.value; select.innerHTML = ''; select.appendChild(new Option(firstText, '')); for (const value of values) select.appendChild(new Option(textOf(value), String(value), false, String(value) === previous)); } function renderFilters() { const packs = state.products.filter((p) => p.kind === 'vehicle-pack'); fillSelect($('countryFilter'), t('allCountries'), [...new Set(packs.map((p) => p.country))], WtoUi.label); fillSelect($('rankFilter'), t('allRanks'), [...new Set(packs.map((p) => p.rank))].sort((a, b) => a - b), (r) => t('rank', { rank: ROMAN[r] || r })); } function matchesFilters(product) { if (product.kind !== currentTab) return false; if (currentTab !== 'vehicle-pack') return true; const country = $('countryFilter').value; const rank = $('rankFilter').value; const query = $('searchInput').value.trim().toLowerCase(); if (country && product.country !== country) return false; if (rank && String(product.rank) !== rank) return false; return !query || [product.title.zh, product.title.en, product.unitName].some((s) => (s || '').toLowerCase().includes(query)); } function renderContent() { const isRedeem = currentTab === 'redeem'; $('redeemPanel').hidden = !isRedeem; $('grid').hidden = isRedeem; $('packFilters').hidden = currentTab !== 'vehicle-pack'; if (isRedeem) return renderRedeemHelp(); const grid = $('grid'); grid.innerHTML = ''; const items = state.products.filter(matchesFilters); if (items.length === 0) grid.innerHTML = '
' + WtoUi.escapeHtml(t('empty')) + '
'; for (const product of items) grid.appendChild(productCard(product)); } function productCard(product) { const card = document.createElement('div'); card.className = 'card'; const head = document.createElement('div'); head.className = 'card-head'; if (product.unitName) head.appendChild(WtoUi.iconImg('vehicle', product.unitName, 'vehicle-icon')); const titles = document.createElement('div'); titles.innerHTML = '

'; titles.querySelector('.card-title').textContent = WtoUi.text(product.title); if (product.country) { const sub = titles.querySelector('.card-sub'); sub.appendChild(WtoUi.iconImg('flag', product.country, 'flag')); sub.appendChild(document.createTextNode(WtoUi.label(product.country) + ' · ' + t('rank', { rank: ROMAN[product.rank] || product.rank }))); } head.appendChild(titles); card.appendChild(head); const contents = []; if (product.kind === 'premium-gjn') contents.push(t('premiumDays', { days: product.premiumDays })); else if (product.premiumDays) contents.push(t('containsPremium', { days: product.premiumDays })); if (product.kind === 'vehicle-pack' && product.goldIncome) contents.push(t('containsEagles', { n: WtoUi.num(product.goldIncome) })); if (contents.length) { const list = document.createElement('ul'); list.className = 'contents'; for (const line of contents) list.appendChild(Object.assign(document.createElement('li'), { textContent: line })); card.appendChild(list); } const foot = document.createElement('div'); foot.className = 'card-foot'; foot.innerHTML = ''; foot.querySelector('.price').textContent = WtoUi.gjn(product.gjnCostCents) + ' GJN'; const button = foot.querySelector('button'); button.textContent = product.owned ? t('owned') : state.activeUserId ? t('buy') : t('pickAccount'); button.disabled = product.owned || !state.activeUserId; button.onclick = () => buy(product.id); card.appendChild(foot); return card; } function renderRedeemHelp() { const list = $('redeemHelp'); list.innerHTML = ''; 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 }); WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok); await loadState(state.activeUserId); } async function redeem() { const result = await WtoUi.postJson('/store/api/redeem', { userId: state.activeUserId, code: $('codeInput').value }); WtoUi.showNotice($('notice'), WtoUi.text(result.message), result.ok); if (result.ok) $('codeInput').value = ''; await loadState(state.activeUserId); } $('accountSelect').onchange = (event) => loadState(event.target.value); $('searchInput').oninput = renderContent; $('countryFilter').onchange = renderContent; $('rankFilter').onchange = renderContent; $('redeemBtn').onclick = redeem; $('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); currentTab = tab.dataset.tab; renderContent(); }; applyStaticTexts(); WtoUi.loadLabels().then(() => loadState(WtoUi.savedAccount()));