Split inventory definition generation by category and select chest contents deterministically. Move squadron queries, membership, and lifecycle into focused modules and enforce role permissions. Serve store and marketplace pages as static files with shared web helpers. Share profile field readers and keep response encoding in the protocol modules. Restrict cloud settings to registered blocks. Type checking and the save CLI ran successfully. In-game acceptance is pending.
139 lines
5.5 KiB
JavaScript
139 lines
5.5 KiB
JavaScript
const COUNTRY_LABELS = {
|
|
country_usa: '美国', country_germany: '德国', country_ussr: '苏联', country_britain: '英国',
|
|
country_japan: '日本', country_china: '中国', country_italy: '意大利', country_france: '法国',
|
|
country_sweden: '瑞典', country_israel: '以色列'
|
|
};
|
|
|
|
let state = { accounts: [], currentUserId: '', balance: { gjn: 0, goldenEagles: 0, silverLions: 0 }, listings: [] };
|
|
let activeCat = 'all';
|
|
const initialParams = new URLSearchParams(location.search);
|
|
const targetHash = initialParams.get('n') || '';
|
|
if (targetHash) {
|
|
document.getElementById('searchInput').value = targetHash;
|
|
}
|
|
|
|
function showToast(msg, ok) {
|
|
const el = document.getElementById('toast');
|
|
el.textContent = msg;
|
|
el.className = ok ? 'ok' : 'err';
|
|
}
|
|
|
|
async function loadState(userId) {
|
|
const q = userId ? '?userId=' + encodeURIComponent(userId) : '';
|
|
const res = await fetch('/trade.gaijin.net/api/state' + q);
|
|
state = await res.json();
|
|
renderHeader();
|
|
populateCountries();
|
|
renderGrid();
|
|
}
|
|
|
|
function renderHeader() {
|
|
const sel = document.getElementById('accountSelect');
|
|
sel.innerHTML = '';
|
|
for (const acc of state.accounts) {
|
|
const opt = document.createElement('option');
|
|
opt.value = acc.userId;
|
|
opt.textContent = acc.nick + ' (' + acc.login + ')';
|
|
if (acc.userId === state.currentUserId) opt.selected = true;
|
|
sel.appendChild(opt);
|
|
}
|
|
document.getElementById('balGjn').textContent = state.balance.gjn.toLocaleString();
|
|
document.getElementById('balGold').textContent = state.balance.goldenEagles.toLocaleString();
|
|
document.getElementById('balSilver').textContent = state.balance.silverLions.toLocaleString();
|
|
}
|
|
|
|
function populateCountries() {
|
|
const sel = document.getElementById('countryFilter');
|
|
const prev = sel.value;
|
|
const countries = [...new Set(state.listings.map(l => l.country).filter(Boolean))];
|
|
sel.innerHTML = '<option value="">全部国家</option>';
|
|
for (const c of countries) {
|
|
const opt = document.createElement('option');
|
|
opt.value = c;
|
|
opt.textContent = COUNTRY_LABELS[c] || c;
|
|
if (c === prev) opt.selected = true;
|
|
sel.appendChild(opt);
|
|
}
|
|
}
|
|
|
|
function renderGrid() {
|
|
const grid = document.getElementById('grid');
|
|
const country = document.getElementById('countryFilter').value;
|
|
const unitClass = document.getElementById('classFilter').value;
|
|
const q = document.getElementById('searchInput').value.trim().toLowerCase();
|
|
|
|
const filtered = state.listings.filter(item => {
|
|
if (activeCat !== 'all' && item.category !== activeCat) return false;
|
|
if (country && item.country !== country) return false;
|
|
if (unitClass && item.unitClass !== unitClass) return false;
|
|
if (q) {
|
|
const hay = (item.title.zh + ' ' + item.title.en + ' ' + item.marketHashName + ' ' + item.itemdefId).toLowerCase();
|
|
if (!hay.includes(q)) return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
grid.innerHTML = '';
|
|
for (const item of filtered) {
|
|
const card = document.createElement('div');
|
|
card.className = 'card' + (targetHash && item.marketHashName === targetHash ? ' highlight' : '');
|
|
const titleColor = item.nameColor ? '#' + item.nameColor : '#e6edf5';
|
|
const badges = [];
|
|
if (item.country) badges.push('<span class="badge">' + (COUNTRY_LABELS[item.country] || item.country) + '</span>');
|
|
if (item.rank) badges.push('<span class="badge">' + item.rank + ' 级</span>');
|
|
badges.push('<span class="badge">#' + item.itemdefId + '</span>');
|
|
if (item.couponCount > 0) badges.push('<span class="badge owned">库存持有 x' + item.couponCount + '</span>');
|
|
if (item.alreadyActivated) badges.push('<span class="badge owned">已激活拥有</span>');
|
|
|
|
card.innerHTML =
|
|
'<div>' +
|
|
'<h3 style="color:' + titleColor + '">' + escapeHtml(item.title.zh) + '</h3>' +
|
|
'<div class="meta">' + escapeHtml(item.title.en) + ' · ' + escapeHtml(item.subtitle.zh) + '</div>' +
|
|
'<div class="badges">' + badges.join('') + '</div>' +
|
|
'</div>' +
|
|
'<div class="card-foot">' +
|
|
'<span class="price">' + item.gjnCost + ' GJN</span>' +
|
|
'<button data-id="' + item.itemdefId + '"></button>' +
|
|
'</div>';
|
|
const btn = card.querySelector('button');
|
|
if (!state.currentUserId) {
|
|
btn.textContent = '请先登录游戏';
|
|
btn.disabled = true;
|
|
} else {
|
|
btn.textContent = '购买兑换券';
|
|
btn.addEventListener('click', () => buyCoupon(item.itemdefId));
|
|
}
|
|
grid.appendChild(card);
|
|
}
|
|
}
|
|
|
|
async function buyCoupon(itemdefId) {
|
|
const res = await fetch('/trade.gaijin.net/api/buy', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({ userId: state.currentUserId, itemdefId, quantity: 1 })
|
|
});
|
|
const data = await res.json();
|
|
showToast(data.message, data.ok);
|
|
if (data.ok) await loadState(state.currentUserId);
|
|
}
|
|
|
|
function escapeHtml(str) {
|
|
return String(str).replace(/[&<>"']/g, ch => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch]));
|
|
}
|
|
|
|
document.getElementById('tabs').addEventListener('click', e => {
|
|
const btn = e.target.closest('.tab');
|
|
if (!btn) return;
|
|
for (const el of document.querySelectorAll('.tab')) el.classList.remove('active');
|
|
btn.classList.add('active');
|
|
activeCat = btn.dataset.cat;
|
|
renderGrid();
|
|
});
|
|
document.getElementById('accountSelect').addEventListener('change', e => loadState(e.target.value));
|
|
document.getElementById('countryFilter').addEventListener('change', renderGrid);
|
|
document.getElementById('classFilter').addEventListener('change', renderGrid);
|
|
document.getElementById('searchInput').addEventListener('input', renderGrid);
|
|
|
|
loadState('');
|