feat: compare mood presets in the UI
Add GET /api/compare with shared/unique games and Jaccard, plus a preset comparison panel on the recommendations tab.
This commit is contained in:
1 parent
9943081baa
commit
02af4bde09
10 files changed
+198
-3
No files matched your search
@@ -918,6 +918,7 @@ async function loadMoods() {
|
||||
if (chip.dataset.preset === state.preset) chip.classList.add("active");
|
||||
});
|
||||
updateMoodTools();
|
||||
fillCompareSelects();
|
||||
} catch (err) {
|
||||
toast(err.message);
|
||||
}
|
||||
@@ -1166,6 +1167,82 @@ async function runRecommend(scroll = true) {
|
||||
}
|
||||
}
|
||||
|
||||
function fillCompareSelects() {
|
||||
const a = $("#compareA");
|
||||
const b = $("#compareB");
|
||||
if (!a || !b) return;
|
||||
const prevA = a.value;
|
||||
const prevB = b.value;
|
||||
const options = state.moods.map((mood) =>
|
||||
`<option value="${esc(mood.name)}">${esc(mood.description || mood.name)}</option>`
|
||||
).join("");
|
||||
a.innerHTML = options;
|
||||
b.innerHTML = options;
|
||||
if (prevA && state.moods.some((mood) => mood.name === prevA)) a.value = prevA;
|
||||
if (prevB && state.moods.some((mood) => mood.name === prevB)) b.value = prevB;
|
||||
if (!a.value && state.preset && state.moods.some((mood) => mood.name === state.preset)) {
|
||||
a.value = state.preset;
|
||||
}
|
||||
if (!a.value && a.options.length) a.selectedIndex = 0;
|
||||
if (!b.value && b.options.length) b.selectedIndex = Math.min(1, b.options.length - 1);
|
||||
if (a.value === b.value && b.options.length > 1) {
|
||||
b.selectedIndex = (a.selectedIndex + 1) % b.options.length;
|
||||
}
|
||||
}
|
||||
|
||||
function compareListHtml(title, items, empty) {
|
||||
const body = items.length
|
||||
? `<ul>${items.map((game) =>
|
||||
`<li><button class="link" type="button" data-appid="${game.appid}">${esc(game.name)}</button>` +
|
||||
`<span class="compare-score">${Math.round(game.score)}</span></li>`
|
||||
).join("")}</ul>`
|
||||
: `<div class="muted">${empty}</div>`;
|
||||
return `<div class="compare-col"><h3>${title}</h3>${body}</div>`;
|
||||
}
|
||||
|
||||
function renderCompare(data) {
|
||||
const box = $("#compareResult");
|
||||
box.classList.remove("hidden");
|
||||
box.innerHTML =
|
||||
`<div class="compare-col"><h3>пересечение</h3>` +
|
||||
`<div class="compare-jaccard">Jaccard ${data.jaccard}</div>` +
|
||||
`<div class="muted">общих игр: ${data.common.length}</div></div>` +
|
||||
compareListHtml(`только ${esc(data.a)}`, data.only_a, "нет") +
|
||||
compareListHtml(`только ${esc(data.b)}`, data.only_b, "нет") +
|
||||
compareListHtml("общие", data.common, "нет");
|
||||
box.querySelectorAll("button[data-appid]").forEach((button) => {
|
||||
button.addEventListener("click", () => openGame(Number(button.dataset.appid)));
|
||||
});
|
||||
}
|
||||
|
||||
async function comparePresets() {
|
||||
const a = $("#compareA").value;
|
||||
const b = $("#compareB").value;
|
||||
if (!a || !b) return;
|
||||
if (a === b) {
|
||||
toast("Выбери два разных профиля");
|
||||
return;
|
||||
}
|
||||
const button = $("#compareBtn");
|
||||
button.disabled = true;
|
||||
try {
|
||||
const params = new URLSearchParams({ a, b, limit: "20" });
|
||||
const data = await api(`/api/compare?${params}`);
|
||||
renderCompare(data);
|
||||
} catch (err) {
|
||||
toast(err.message);
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
$("#compareToggle").addEventListener("click", () => {
|
||||
const form = $("#compareForm");
|
||||
form.classList.toggle("hidden");
|
||||
if (!form.classList.contains("hidden")) fillCompareSelects();
|
||||
});
|
||||
$("#compareBtn").addEventListener("click", comparePresets);
|
||||
|
||||
function downloadText(filename, text, content_type) {
|
||||
const blob = new Blob([text], { type: content_type });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
Reference in new issue
Block a user