feat(store): add store and marketplace item views and image sources

Add catalogue filters, item detail views, and purchase and resale
actions. Resolve images from the player's asset pack, public image
URLs, or local game data. Add a utility for creating the optional
local asset pack.

Type checking and JavaScript syntax checks passed. Embedded browser
appearance and in-game behavior still need acceptance.
This commit is contained in:
N0VAN0M4LY committed 2026-11-26 07:12:38 +08:00
1 parent a73635407d
commit 98812fa9c2
39 files changed
+1309 -400

No files matched your search

+123 -104
View File
@@ -1,30 +1,37 @@
// Local store page, shown in the game's built-in browser.
// Local store page (implementation notes; layout after the official Gaijin.Net store,
// implementation notes), shown in the game's built-in browser.
// The game opens /store?item=<bundle GUID> for a gift vehicle's own pack (item_info.php `url`,
// implementation notes); the page then shows that pack's detail view.
'use strict';
const T = {
zh: {
title: 'WTOffline 本地商店', hint: '买完关闭本窗口,机库会自动刷新', toMarket: '前往交易所',
eagles: '金鹰', lions: '银狮', premium: '高级账户', premiumOff: '未开通', premiumLeft: '剩余 {days} 天',
tabPacks: '礼包载具', tabPremium: '高级账户', tabEagles: '金鹰', tabRedeem: '兑换码',
search: '搜索礼包或载具名称', allCountries: '全部国家', allRanks: '全部等级', rank: '{rank} 级',
includesUnit: '包含载具:{name}', containsEagles: '附赠 {n} 金鹰', containsSilver: '附赠 {n} 银狮', containsPremium: '附赠 {days} 天高级账户', premiumDays: '{days} 天高级账户',
buy: '购买', freeBuy: '老福爷付过了', owned: '已拥有', pickAccount: '请先选择账号', redeem: '兑换', codePlaceholder: '输入兑换码', empty: '没有符合条件的商品',
store: '商店', toMarket: '交易所', eagles: '金鹰', lions: '银狮', premium: '高级账户', premiumOff: '未开通', premiumLeft: '剩余 {days} 天',
tabPacks: '高级载具', tabEagles: '金鹰', tabPremium: '高级帐户', tabRedeem: '优惠码',
search: '搜索礼包或载具', allCountries: '所有国家', allRanks: '所有等级', rank: '{rank} 级', sortBest: '最畅销', sortPriceAsc: '价格从低到高', sortPriceDesc: '价格从高到低',
vehicle: '{name}', containsEagles: '{n} 金鹰', containsSilver: '{n} 银狮', containsPremium: '{days} 天高级账户',
buy: '购买', freeBuy: '老福爷付过了', owned: '已拥有', pickAccount: '请先在右上角选择账号', redeem: '兑换', codePlaceholder: '输入兑换码',
empty: '没有符合条件的商品', includes: '本特惠包包含', back: '‹ 返回目录', bonusTitle: 'Premium bonuses', bonusText: '高级载具获得的银狮与研发点有加成,并可直接使用。',
footer: 'WTOffline 本地商店 · 买完关闭本窗口,机库会自动刷新', badgeOwned: '已拥有', badgeHot: '人气商品',
},
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 packs or vehicles', allCountries: 'All nations', allRanks: 'All ranks', rank: 'Rank {rank}',
includesUnit: 'Vehicle: {name}', containsEagles: '+{n} Golden Eagles', containsSilver: '+{n} Silver Lions', containsPremium: '+{days} days of Premium Account', premiumDays: '{days} days of Premium Account',
buy: 'Buy', freeBuy: '老福爷付过了', owned: 'Owned', pickAccount: 'Pick an account first', redeem: 'Redeem', codePlaceholder: 'Enter a code', empty: 'Nothing matches the filters',
store: 'Store', toMarket: 'Market', eagles: 'Golden Eagles', lions: 'Silver Lions', premium: 'Premium', premiumOff: 'inactive', premiumLeft: '{days} days left',
tabPacks: 'Premium vehicles', tabEagles: 'Golden Eagles', tabPremium: 'Premium account', tabRedeem: 'Promo code',
search: 'Search packs or vehicles', allCountries: 'All nations', allRanks: 'All ranks', rank: 'Rank {rank}', sortBest: 'Best-selling', sortPriceAsc: 'Price: low to high', sortPriceDesc: 'Price: high to low',
vehicle: '{name}', containsEagles: '{n} Golden Eagles', containsSilver: '{n} Silver Lions', containsPremium: '{days} days of Premium Account',
buy: 'Buy', freeBuy: '老福爷付过了', owned: 'Owned', pickAccount: 'Pick an account at the top right', redeem: 'Redeem', codePlaceholder: 'Enter a code',
empty: 'Nothing matches the filters', includes: 'This pack includes', back: '‹ Back to catalog', bonusTitle: 'Premium bonuses', bonusText: 'Premium vehicles earn more Silver Lions and research points and are ready to use.',
footer: 'WTOffline local store · close this window after buying to refresh the hangar', badgeOwned: 'Owned', badgeHot: 'Best seller',
},
};
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 wantedGuid = (WtoUi.query.get('item') || '').toUpperCase();
let state = { accounts: [], activeUserId: '', balance: { gjnCents: 0, goldenEagles: 0, silverLions: 0 }, premiumDays: 0, products: [], redeemHelp: [] };
let currentTab = 'vehicle-pack';
let detailId = '';
function applyStaticTexts() {
for (const el of document.querySelectorAll('[data-t]')) el.textContent = t(el.dataset.t);
@@ -35,21 +42,18 @@ function applyStaticTexts() {
async function loadState(userId) {
state = await WtoUi.getJson('/store/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
if (userId && !state.activeUserId) {
WtoUi.clearSavedAccount();
return loadState('');
}
renderHeader();
renderFilters();
renderContent();
}
function renderHeader() {
if (userId && !state.activeUserId) { WtoUi.clearSavedAccount(); return loadState(''); }
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');
if (wantedGuid && !detailId) {
const wanted = state.products.find((p) => (p.guid || '').toUpperCase() === wantedGuid);
if (wanted) { detailId = wanted.id; currentTab = wanted.kind; }
}
renderFilters();
render();
}
function fillSelect(select, firstText, values, textOf) {
@@ -62,97 +66,116 @@ function fillSelect(select, firstText, values, textOf) {
function renderFilters() {
const packs = state.products.filter((p) => p.kind === 'vehicle-pack');
fillSelect($('countryFilter'), t('allCountries'), [...new Set(packs.map((p) => p.country).filter(Boolean))], WtoUi.label);
const allRanks = packs.flatMap((p) => p.ranks || (p.rank ? [p.rank] : []));
fillSelect($('rankFilter'), t('allRanks'), [...new Set(allRanks)].sort((a, b) => a - b), (r) => t('rank', { rank: ROMAN[r] || r }));
fillSelect($('rankFilter'), t('allRanks'), [...new Set(packs.flatMap((p) => p.ranks || []))].sort((a, b) => a - b), (r) => t('rank', { rank: ROMAN[r] || r }));
}
function matchesFilters(product) {
function matches(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 && !(product.ranks || [product.rank]).map(String).includes(rank)) return false;
if ($('countryFilter').value && product.country !== $('countryFilter').value) return false;
if ($('rankFilter').value && !(product.ranks || []).map(String).includes($('rankFilter').value)) return false;
if (!query) return true;
const searchTerms = [product.title.zh, product.title.en, product.unitName];
for (const v of product.vehicles || []) searchTerms.push(v.unitName, v.title && v.title.zh, v.title && v.title.en);
return searchTerms.some((s) => (s || '').toLowerCase().includes(query));
const terms = [product.title.zh, product.title.en];
for (const v of product.vehicles || []) terms.push(v.unitName, v.title && v.title.zh, v.title && v.title.en);
return terms.some((s) => (s || '').toLowerCase().includes(query));
}
function renderContent() {
const isRedeem = currentTab === 'redeem';
$('redeemPanel').hidden = !isRedeem;
$('grid').hidden = isRedeem;
function render() {
for (const tab of document.querySelectorAll('.snav-item')) tab.classList.toggle('active', tab.dataset.tab === currentTab);
const product = detailId && state.products.find((p) => p.id === detailId);
$('redeemPanel').hidden = currentTab !== 'redeem';
$('catalog').hidden = currentTab === 'redeem' || !!product;
$('detail').hidden = !product;
$('packFilters').hidden = currentTab !== 'vehicle-pack';
if (isRedeem) return renderRedeemHelp();
if (currentTab === 'redeem') return renderRedeemHelp();
if (product) return renderDetail(product);
const items = state.products.filter(matches);
const mode = $('sortSelect').value;
if (mode === 'priceAsc') items.sort((a, b) => a.gjnCostCents - b.gjnCostCents);
if (mode === 'priceDesc') items.sort((a, b) => b.gjnCostCents - a.gjnCostCents);
const grid = $('grid');
grid.innerHTML = '';
const items = state.products.filter(matchesFilters);
if (items.length === 0) grid.innerHTML = '<div class="empty">' + WtoUi.escapeHtml(t('empty')) + '</div>';
for (const product of items) grid.appendChild(productCard(product));
if (!items.length) grid.innerHTML = '<div class="empty">' + WtoUi.escapeHtml(t('empty')) + '</div>';
for (const item of items) grid.appendChild(productCard(item));
}
function productImage(product, className) {
if (product.unitName) return WtoUi.vehicleImg(product.unitName, className);
return WtoUi.storeImg(product.guid, '', className ? className + ' cover' : 'cover');
}
function contentsOf(product) {
const lines = [];
for (const v of product.vehicles || []) lines.push({ text: WtoUi.text(v.title), vehicle: v });
if (product.premiumDays) lines.push({ text: t('containsPremium', { days: product.premiumDays }) });
if (product.goldIncome) lines.push({ text: t('containsEagles', { n: WtoUi.num(product.goldIncome) }) });
if (product.silverIncome) lines.push({ text: t('containsSilver', { n: WtoUi.num(product.silverIncome) }) });
return lines;
}
function buyButtons(product, mainClass, freeClass) {
const disabled = product.owned || !state.activeUserId;
const main = Object.assign(document.createElement('button'), { className: mainClass, disabled, textContent: product.owned ? t('owned') : (state.activeUserId ? t('buy') : t('pickAccount')) });
main.onclick = (e) => { e.stopPropagation(); buy(product.id, 'gjn'); };
const free = Object.assign(document.createElement('button'), { className: freeClass, disabled, textContent: t('freeBuy') });
free.onclick = (e) => { e.stopPropagation(); buy(product.id, 'free'); };
return [main, free];
}
function productCard(product) {
const card = document.createElement('div');
card.className = 'card';
card.appendChild(productCardHead(product));
const contents = productContents(product);
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);
card.className = 'pw';
card.innerHTML = '<div class="pw-poster"></div><div class="pw-desc"><div class="pw-game">War Thunder</div><div class="pw-title"></div><div class="pw-comment"></div><ul class="pw-contents"></ul></div><div class="pw-price"></div><div class="pw-buy"></div>';
card.querySelector('.pw-poster').appendChild(productImage(product, ''));
if (product.owned) card.querySelector('.pw-poster').appendChild(Object.assign(document.createElement('div'), { className: 'badge owned', textContent: t('badgeOwned') }));
card.querySelector('.pw-title').textContent = WtoUi.text(product.title);
if (product.country) {
const comment = card.querySelector('.pw-comment');
comment.appendChild(WtoUi.iconImg('flag', product.country, ''));
comment.appendChild(document.createTextNode(WtoUi.label(product.country) + ', ' + t('rank', { rank: ROMAN[product.rank] || product.rank })));
}
const foot = document.createElement('div');
foot.className = 'card-foot';
foot.innerHTML = '<span class="price"></span><div class="buy-actions"></div>';
foot.querySelector('.price').textContent = WtoUi.gjn(product.gjnCostCents) + ' GJN';
const actions = foot.querySelector('.buy-actions');
if (product.owned || !state.activeUserId) {
actions.appendChild(Object.assign(document.createElement('button'), {
className: 'buy', textContent: product.owned ? t('owned') : t('pickAccount'), disabled: true,
}));
} else {
const gjnBtn = Object.assign(document.createElement('button'), { className: 'buy', textContent: t('buy') });
gjnBtn.onclick = () => buy(product.id, 'gjn');
const freeBtn = Object.assign(document.createElement('button'), { className: 'buy free', textContent: t('freeBuy') });
freeBtn.onclick = () => buy(product.id, 'free');
actions.append(gjnBtn, freeBtn);
}
card.appendChild(foot);
const list = card.querySelector('.pw-contents');
for (const line of contentsOf(product).slice(1)) list.appendChild(Object.assign(document.createElement('li'), { textContent: line.text }));
card.querySelector('.pw-price').textContent = '$' + WtoUi.gjn(product.gjnCostCents);
card.querySelector('.pw-buy').append(...buyButtons(product, '', ''));
card.onclick = () => { detailId = product.id; render(); window.scrollTo(0, 0); };
return card;
}
function productCardHead(product) {
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 = '<h3 class="card-title"></h3><div class="card-sub"></div>';
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 })));
function renderDetail(product) {
const detail = $('detail');
detail.innerHTML = '<span class="back"></span><h1></h1><div class="detail-cols"><div class="detail-left"><div class="media"></div><div class="thumbs"></div>'
+ '<h2></h2><div class="bonus"><h3></h3><div></div></div></div><div class="detail-right"><div class="buy-panel"><div class="price"></div><div class="btns"></div>'
+ '<div class="includes"><h4></h4><ul></ul></div></div></div></div>';
detail.querySelector('.back').textContent = t('back');
detail.querySelector('.back').onclick = () => { detailId = ''; render(); };
detail.querySelector('h1').textContent = WtoUi.text(product.title);
const media = detail.querySelector('.media');
media.appendChild(productImage(product, ''));
const thumbs = detail.querySelector('.thumbs');
for (const v of product.vehicles || []) {
const thumb = WtoUi.vehicleImg(v.unitName, v.unitName === product.unitName ? 'active' : '');
thumb.onclick = () => { media.innerHTML = ''; media.appendChild(WtoUi.vehicleImg(v.unitName, '')); };
thumbs.appendChild(thumb);
}
head.appendChild(titles);
return head;
}
function productContents(product) {
const contents = [];
if (product.vehicles && product.vehicles.length > 1) {
for (const v of product.vehicles) contents.push(t('includesUnit', { name: WtoUi.text(v.title) }));
detail.querySelector('.detail-left h2').textContent = product.vehicles ? product.vehicles.map((v) => WtoUi.text(v.title)).join(' / ') : WtoUi.text(product.title);
detail.querySelector('.bonus h3').textContent = t('bonusTitle');
detail.querySelector('.bonus div').textContent = t('bonusText');
detail.querySelector('.bonus').hidden = product.kind !== 'vehicle-pack';
detail.querySelector('.price').textContent = '$' + WtoUi.gjn(product.gjnCostCents) + ' (GJN)';
detail.querySelector('.btns').append(...buyButtons(product, 'btn-main', 'btn-free'));
detail.querySelector('.includes h4').textContent = t('includes');
const list = detail.querySelector('.includes ul');
for (const line of contentsOf(product)) {
const li = document.createElement('li');
if (line.vehicle && product.country) {
li.appendChild(WtoUi.iconImg('flag', product.country, ''));
li.appendChild(Object.assign(document.createElement('span'), { className: 'rank-chip', textContent: ROMAN[line.vehicle.rank] || line.vehicle.rank }));
}
li.appendChild(document.createTextNode(line.text));
list.appendChild(li);
}
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 (product.kind === 'vehicle-pack' && product.silverIncome) contents.push(t('containsSilver', { n: WtoUi.num(product.silverIncome) }));
return contents;
}
function renderRedeemHelp() {
@@ -174,21 +197,17 @@ async function redeem() {
await loadState(state.activeUserId);
}
$('accountSelect').onchange = (event) => {
WtoUi.saveAccount(event.target.value);
loadState(event.target.value);
};
$('searchInput').oninput = renderContent;
$('countryFilter').onchange = renderContent;
$('rankFilter').onchange = renderContent;
$('accountSelect').onchange = (event) => { WtoUi.saveAccount(event.target.value); loadState(event.target.value); };
$('searchInput').oninput = render;
for (const id of ['countryFilter', 'rankFilter', 'sortSelect']) $(id).onchange = render;
$('redeemBtn').onclick = redeem;
$('tabs').onclick = (event) => {
const tab = event.target.closest('.tab');
const tab = event.target.closest('.snav-item');
if (!tab) return;
for (const other of document.querySelectorAll('.tab')) other.classList.toggle('active', other === tab);
currentTab = tab.dataset.tab;
renderContent();
detailId = '';
render();
};
applyStaticTexts();
WtoUi.loadLabels().then(() => loadState(WtoUi.savedAccount()));
Promise.all([WtoUi.loadLabels(), WtoUi.loadImageSources()]).then(() => loadState(WtoUi.savedAccount()));