61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
function readSaveSummary(storage, prefix, defaultGameId, gameLabels) {
|
|
try {
|
|
if (!storage) return null;
|
|
const rawMeta = storage.getItem(`${prefix}app-meta`);
|
|
const meta = rawMeta ? JSON.parse(rawMeta) : null;
|
|
const gameId = meta?.activeGameId || defaultGameId;
|
|
const gameMeta = meta?.gameProfiles?.[gameId];
|
|
const profileId = gameMeta?.activeProfileId || 'default';
|
|
const profileName =
|
|
gameMeta?.profiles?.find((p) => p.id === profileId)?.name || 'Default';
|
|
const profileCount = gameMeta?.profiles?.length || 1;
|
|
const saveKey = profileId === 'default' ? `${prefix}${gameId}` : `${prefix}${gameId}:${profileId}`;
|
|
const rawSave = storage.getItem(saveKey);
|
|
if (!rawSave) return null;
|
|
const parsed = JSON.parse(rawSave);
|
|
const caughtCount = Array.isArray(parsed?.caught) ? parsed.caught.length : 0;
|
|
const gameLabel = gameLabels[gameId] || gameId;
|
|
return {
|
|
gameLabel,
|
|
profileName,
|
|
profileCount,
|
|
caughtCount,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function renderSavePills(doc = globalThis.document, storage = globalThis.localStorage) {
|
|
const orasEl = doc?.getElementById('oras-save-badge');
|
|
if (orasEl) {
|
|
const info = readSaveSummary(storage, 'poc-tracker:v1:', 'alpha-sapphire', {
|
|
'alpha-sapphire': 'Alpha Sapphire',
|
|
'omega-ruby': 'Omega Ruby',
|
|
});
|
|
if (info) {
|
|
orasEl.textContent = `Saved (${info.gameLabel} · ${info.profileName}): ${info.caughtCount} caught`;
|
|
orasEl.classList.add('has-save');
|
|
} else {
|
|
orasEl.textContent = 'Ready for a new run';
|
|
}
|
|
}
|
|
|
|
const lumiEl = doc?.getElementById('lumi-save-badge');
|
|
if (lumiEl) {
|
|
const info = readSaveSummary(storage, 'lumi-poc-tracker:v1:', 'luminescent-platinum', {
|
|
'luminescent-platinum': 'Luminescent Platinum',
|
|
});
|
|
if (info) {
|
|
lumiEl.textContent = `Saved (${info.profileName}): ${info.caughtCount} caught`;
|
|
lumiEl.classList.add('has-save');
|
|
} else {
|
|
lumiEl.textContent = 'Ready for a new run';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (typeof globalThis.document !== 'undefined') {
|
|
renderSavePills();
|
|
}
|