Fix vehicle icon lookup and default web account selection
Match archive paths without regard to case and include Golden Eagle vehicles in icon extraction. Default read-only page state to the current in-game account. Keep web mutations gated on an explicit account ID. Type checking and local icon and account selection checks passed. In-game acceptance is pending.
This commit is contained in:
1 parent
26fd6b51b7
commit
ec2d3ffbc4
6 files changed
+63
-24
No files matched your search
@@ -35,6 +35,7 @@ const NICK_MAX_LENGTH = 16;
|
|||||||
export class AccountStore {
|
export class AccountStore {
|
||||||
readonly #path: string;
|
readonly #path: string;
|
||||||
#file: AccountFile;
|
#file: AccountFile;
|
||||||
|
#lastSignedInUserId: string | undefined;
|
||||||
|
|
||||||
constructor(dataFolder: string) {
|
constructor(dataFolder: string) {
|
||||||
this.#path = join(dataFolder, 'accounts.json');
|
this.#path = join(dataFolder, 'accounts.json');
|
||||||
@@ -45,6 +46,7 @@ export class AccountStore {
|
|||||||
const existing = this.#find(login);
|
const existing = this.#find(login);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (!verifyPassword(password, existing.passwordHash)) return { kind: 'wrong-password' };
|
if (!verifyPassword(password, existing.passwordHash)) return { kind: 'wrong-password' };
|
||||||
|
this.#lastSignedInUserId = existing.userId;
|
||||||
return { kind: 'signed-in', account: existing, registered: false };
|
return { kind: 'signed-in', account: existing, registered: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,9 +54,18 @@ export class AccountStore {
|
|||||||
const account: Account = { login, userId, nick: nickFromLogin(login, userId), passwordHash: hashPassword(password), created: new Date().toISOString() };
|
const account: Account = { login, userId, nick: nickFromLogin(login, userId), passwordHash: hashPassword(password), created: new Date().toISOString() };
|
||||||
this.#file = { ...this.#file, nextUserId: this.#file.nextUserId + 1, accounts: [...this.#file.accounts, account] };
|
this.#file = { ...this.#file, nextUserId: this.#file.nextUserId + 1, accounts: [...this.#file.accounts, account] };
|
||||||
writeFileAtomic(this.#path, JSON.stringify(this.#file, null, 2));
|
writeFileAtomic(this.#path, JSON.stringify(this.#file, null, 2));
|
||||||
|
this.#lastSignedInUserId = userId;
|
||||||
return { kind: 'signed-in', account, registered: true };
|
return { kind: 'signed-in', account, registered: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The account that most recently signed in during this service run (for defaulting the initial
|
||||||
|
* read-only account dropdown in the built-in browser; never used as a fallback on mutations).
|
||||||
|
*/
|
||||||
|
lastSignedIn(): Account | undefined {
|
||||||
|
return this.#lastSignedInUserId ? this.byUserId(this.#lastSignedInUserId) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
all(): readonly Account[] {
|
all(): readonly Account[] {
|
||||||
return this.#file.accounts;
|
return this.#file.accounts;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
* gameuiskin/<vehicle>_ico.svg the tech-tree silhouette of a vehicle
|
* gameuiskin/<vehicle>_ico.svg the tech-tree silhouette of a vehicle
|
||||||
* gameuiskin/<country>.svg a country flag (country_usa, ...)
|
* gameuiskin/<country>.svg a country flag (country_usa, ...)
|
||||||
*
|
*
|
||||||
* All 3349 vehicle icons together are about 26 MB, so only the vehicles the stock game sells
|
* All 3349 vehicle icons together are about 26 MB, so only premium and store/market vehicles
|
||||||
* for real money are kept: gift vehicles (wpcost.blk gift:t, the store's packs) and
|
* are kept: gift vehicles (wpcost.blk gift:t), Golden Eagle premium vehicles (wpcost.blk
|
||||||
* marketplace vehicles (shop.blk marketplaceItemdefId).
|
* costGold > 0, used by starter packs and custom multi-vehicle packs), and marketplace
|
||||||
|
* vehicles (shop.blk marketplaceItemdefId).
|
||||||
*/
|
*/
|
||||||
import { numberField, stringField } from './blk-fields.ts';
|
import { numberField, stringField } from './blk-fields.ts';
|
||||||
import type { Dataset } from './dataset.ts';
|
import type { Dataset } from './dataset.ts';
|
||||||
@@ -26,20 +27,23 @@ export interface WebIcons {
|
|||||||
|
|
||||||
export const webIcons: Dataset<WebIcons> = {
|
export const webIcons: Dataset<WebIcons> = {
|
||||||
id: 'web-icons',
|
id: 'web-icons',
|
||||||
version: 1,
|
version: 3,
|
||||||
extract: (source) => {
|
extract: (source) => {
|
||||||
const available = new Set(source.listFiles(ATLASES));
|
// Archive paths in ui/atlases.vromfs.bin are lowercase, whereas unit IDs in wpcost.blk
|
||||||
|
// mix uppercase and lowercase (e.g. germ_pzkpfw_VI_ausf_h1_tiger_east, ussr_t_26E).
|
||||||
|
const byLowerPath = new Map(source.listFiles(ATLASES).map((path) => [path.toLowerCase(), path]));
|
||||||
const readIcon = (name: string): string | undefined => {
|
const readIcon = (name: string): string | undefined => {
|
||||||
const path = `${ICON_FOLDER}${name}.svg`;
|
const actualPath = byLowerPath.get(`${ICON_FOLDER}${name}.svg`.toLowerCase());
|
||||||
return available.has(path) ? source.readText(ATLASES, path) : undefined;
|
return actualPath ? source.readText(ATLASES, actualPath) : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
const shopEntries = shopEntriesByName(source);
|
const shopEntries = shopEntriesByName(source);
|
||||||
const vehicles: Record<string, string> = {};
|
const vehicles: Record<string, string> = {};
|
||||||
for (const vehicle of source.readBlk('char.vromfs.bin', VEHICLE_ECONOMY).blocks) {
|
for (const vehicle of source.readBlk('char.vromfs.bin', VEHICLE_ECONOMY).blocks) {
|
||||||
const soldForMoney = stringField(vehicle, 'gift') !== undefined
|
const needIcon = stringField(vehicle, 'gift') !== undefined
|
||||||
|
|| (numberField(vehicle, 'costGold') ?? 0) > 0
|
||||||
|| numberField(shopEntries.get(vehicle.name), 'marketplaceItemdefId') !== undefined;
|
|| numberField(shopEntries.get(vehicle.name), 'marketplaceItemdefId') !== undefined;
|
||||||
const icon = soldForMoney ? readIcon(`${vehicle.name}_ico`) : undefined;
|
const icon = needIcon ? readIcon(`${vehicle.name}_ico`) : undefined;
|
||||||
if (icon) vehicles[vehicle.name] = icon;
|
if (icon) vehicles[vehicle.name] = icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,10 @@ function applyStaticTexts() {
|
|||||||
|
|
||||||
async function loadState(userId) {
|
async function loadState(userId) {
|
||||||
state = await WtoUi.getJson('/trade.gaijin.net/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
|
state = await WtoUi.getJson('/trade.gaijin.net/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
|
||||||
if (userId && !state.currentUserId) return loadState('');
|
if (userId && !state.currentUserId) {
|
||||||
if (state.currentUserId) WtoUi.saveAccount(state.currentUserId);
|
WtoUi.clearSavedAccount();
|
||||||
|
return loadState('');
|
||||||
|
}
|
||||||
WtoUi.fillAccountSelect($('accountSelect'), state.accounts, state.currentUserId);
|
WtoUi.fillAccountSelect($('accountSelect'), state.accounts, state.currentUserId);
|
||||||
$('gjnVal').textContent = WtoUi.gjn(state.balance.gjnCents);
|
$('gjnVal').textContent = WtoUi.gjn(state.balance.gjnCents);
|
||||||
$('goldVal').textContent = WtoUi.num(state.balance.goldenEagles);
|
$('goldVal').textContent = WtoUi.num(state.balance.goldenEagles);
|
||||||
@@ -112,12 +114,15 @@ function listingCard(listing) {
|
|||||||
|
|
||||||
const foot = document.createElement('div');
|
const foot = document.createElement('div');
|
||||||
foot.className = 'card-foot';
|
foot.className = 'card-foot';
|
||||||
foot.innerHTML = '<span class="price"></span><button class="buy"></button>';
|
foot.innerHTML = '<span class="price"></span>';
|
||||||
foot.querySelector('.price').textContent = WtoUi.gjn(listing.gjnCostCents) + ' GJN';
|
foot.querySelector('.price').textContent = WtoUi.gjn(listing.gjnCostCents) + ' GJN';
|
||||||
const button = foot.querySelector('button');
|
const btn = Object.assign(document.createElement('button'), {
|
||||||
button.textContent = state.currentUserId ? t('buy') : t('pickAccount');
|
className: 'buy',
|
||||||
button.disabled = !state.currentUserId;
|
textContent: state.currentUserId ? t('buy') : t('pickAccount'),
|
||||||
button.onclick = () => buy(listing.itemdefId);
|
disabled: !state.currentUserId,
|
||||||
|
});
|
||||||
|
if (!btn.disabled) btn.onclick = () => buy(listing.itemdefId);
|
||||||
|
foot.appendChild(btn);
|
||||||
card.appendChild(foot);
|
card.appendChild(foot);
|
||||||
return card;
|
return card;
|
||||||
}
|
}
|
||||||
@@ -135,7 +140,10 @@ $('tabs').onclick = (event) => {
|
|||||||
activeCat = tab.dataset.cat;
|
activeCat = tab.dataset.cat;
|
||||||
renderGrid();
|
renderGrid();
|
||||||
};
|
};
|
||||||
$('accountSelect').onchange = (event) => loadState(event.target.value);
|
$('accountSelect').onchange = (event) => {
|
||||||
|
WtoUi.saveAccount(event.target.value);
|
||||||
|
loadState(event.target.value);
|
||||||
|
};
|
||||||
$('countryFilter').onchange = renderGrid;
|
$('countryFilter').onchange = renderGrid;
|
||||||
$('classFilter').onchange = renderGrid;
|
$('classFilter').onchange = renderGrid;
|
||||||
$('searchInput').oninput = renderGrid;
|
$('searchInput').oninput = renderGrid;
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ function applyStaticTexts() {
|
|||||||
|
|
||||||
async function loadState(userId) {
|
async function loadState(userId) {
|
||||||
state = await WtoUi.getJson('/store/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
|
state = await WtoUi.getJson('/store/api/state' + (userId ? '?userId=' + encodeURIComponent(userId) : ''));
|
||||||
|
if (userId && !state.activeUserId) {
|
||||||
|
WtoUi.clearSavedAccount();
|
||||||
|
return loadState('');
|
||||||
|
}
|
||||||
renderHeader();
|
renderHeader();
|
||||||
renderFilters();
|
renderFilters();
|
||||||
renderContent();
|
renderContent();
|
||||||
@@ -165,7 +169,10 @@ async function redeem() {
|
|||||||
await loadState(state.activeUserId);
|
await loadState(state.activeUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$('accountSelect').onchange = (event) => loadState(event.target.value);
|
$('accountSelect').onchange = (event) => {
|
||||||
|
WtoUi.saveAccount(event.target.value);
|
||||||
|
loadState(event.target.value);
|
||||||
|
};
|
||||||
$('searchInput').oninput = renderContent;
|
$('searchInput').oninput = renderContent;
|
||||||
$('countryFilter').onchange = renderContent;
|
$('countryFilter').onchange = renderContent;
|
||||||
$('rankFilter').onchange = renderContent;
|
$('rankFilter').onchange = renderContent;
|
||||||
|
|||||||
@@ -70,11 +70,12 @@ export function jsonResponse(payload: unknown, summary: string): ServiceResponse
|
|||||||
/**
|
/**
|
||||||
* Select the account displayed in a read-only web state query:
|
* Select the account displayed in a read-only web state query:
|
||||||
* - when `requestedUserId` is provided, look up that exact account (never falling back to another);
|
* - when `requestedUserId` is provided, look up that exact account (never falling back to another);
|
||||||
* - when `requestedUserId` is omitted on initial page load, default the UI selector to the first registered account.
|
* - when `requestedUserId` is omitted on initial page load, default the UI selector to the account
|
||||||
|
* currently signed into the game in this session, or the first registered account otherwise.
|
||||||
*/
|
*/
|
||||||
export function selectWebViewAccount(accounts: AccountStore, requestedUserId: string | null): Account | undefined {
|
export function selectWebViewAccount(accounts: AccountStore, requestedUserId: string | null): Account | undefined {
|
||||||
const trimmed = requestedUserId?.trim() ?? '';
|
const trimmed = requestedUserId?.trim() ?? '';
|
||||||
return trimmed ? accounts.byUserId(trimmed) : accounts.all()[0];
|
return trimmed ? accounts.byUserId(trimmed) : (accounts.lastSignedIn() ?? accounts.all()[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -64,13 +64,21 @@ const WtoUi = (() => {
|
|||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The account the player picked last time on either page (kept in the built-in browser). */
|
/**
|
||||||
|
* Manual account override chosen in the page dropdown during this browser session.
|
||||||
|
* Kept in sessionStorage (not auto-written on initial load) so opening the store or
|
||||||
|
* marketplace defaults to the account currently signed into the game.
|
||||||
|
*/
|
||||||
function savedAccount() {
|
function savedAccount() {
|
||||||
try { return localStorage.getItem(ACCOUNT_KEY) || ''; } catch (error) { return ''; }
|
try { return sessionStorage.getItem(ACCOUNT_KEY) || ''; } catch (error) { return ''; }
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveAccount(userId) {
|
function saveAccount(userId) {
|
||||||
try { localStorage.setItem(ACCOUNT_KEY, userId); } catch (error) { /* storage unavailable: the choice is not kept */ }
|
try { sessionStorage.setItem(ACCOUNT_KEY, userId); } catch (error) { /* storage unavailable */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearSavedAccount() {
|
||||||
|
try { sessionStorage.removeItem(ACCOUNT_KEY); } catch (error) { /* storage unavailable */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
function fillAccountSelect(select, accounts, currentId) {
|
function fillAccountSelect(select, accounts, currentId) {
|
||||||
@@ -94,5 +102,5 @@ const WtoUi = (() => {
|
|||||||
element.className = 'notice ' + (ok ? 'ok' : 'err');
|
element.className = 'notice ' + (ok ? 'ok' : 'err');
|
||||||
}
|
}
|
||||||
|
|
||||||
return { lang, query, text, tr, num, gjn, escapeHtml, label, getJson, postJson, loadLabels, iconImg, savedAccount, saveAccount, fillAccountSelect, pageLink, showNotice };
|
return { lang, query, text, tr, num, gjn, escapeHtml, label, getJson, postJson, loadLabels, iconImg, savedAccount, saveAccount, clearSavedAccount, fillAccountSelect, pageLink, showNotice };
|
||||||
})();
|
})();
|
||||||
Reference in new issue
Block a user