Add GET /api/steam/friends and /api/steam/friends/{steamid}, a Friends tab with manual SteamID entry for private profiles, and one-hour caching for friend lists and owned games.
This commit is contained in:
1 parent
5b1b23d392
commit
376a2ee7a0
15 files changed
+456
-15
No files matched your search
+109
-2
@@ -1375,6 +1375,106 @@ recoGrid.addEventListener("click", async (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
/* ---------- friends ---------- */
|
||||
|
||||
const FRIEND_STATE = ["офлайн", "онлайн", "занят", "не беспокоить", "отошёл",
|
||||
"не активен", "хочет играть"];
|
||||
|
||||
function friendCardHtml(friend) {
|
||||
const label = FRIEND_STATE[friend.state] || "";
|
||||
return `<button class="friend-card" type="button" data-steamid="${esc(friend.steamid)}">
|
||||
${friend.avatar
|
||||
? `<img src="${esc(friend.avatar)}" alt="" width="48" height="48" loading="lazy">`
|
||||
: `<span class="friend-avatar"></span>`}
|
||||
<span class="friend-info"><b>${esc(friend.name)}</b><span class="muted">${esc(label)}</span></span>
|
||||
</button>`;
|
||||
}
|
||||
|
||||
function friendGameList(items, local) {
|
||||
if (!items.length) return '<div class="muted">нет</div>';
|
||||
return `<ul>${items.slice(0, 200).map((game) => {
|
||||
const hours = game.friend_hours ? `${game.friend_hours} ч` : "";
|
||||
const score = local
|
||||
? `${esc(STATUS_LABEL[game.status] || game.status || "")}${hours ? ` · ${hours}` : ""}`
|
||||
: hours;
|
||||
const name = local
|
||||
? `<button class="link" type="button" data-appid="${game.appid}">${esc(game.name)}</button>`
|
||||
: `<a class="link" href="https://store.steampowered.com/app/${game.appid}/" target="_blank" rel="noopener">${esc(game.name)}</a>`;
|
||||
return `<li>${name}<span class="compare-score">${score}</span></li>`;
|
||||
}).join("")}</ul>`;
|
||||
}
|
||||
|
||||
async function loadFriends() {
|
||||
const box = $("#friendsList");
|
||||
if (!box) return;
|
||||
if (!state.meta || !state.meta.steam_api) {
|
||||
box.innerHTML = '<div class="empty">Steam API не настроен: добавь ключ и перезапусти сервер.</div>';
|
||||
$("#friendsSummary").textContent = "";
|
||||
return;
|
||||
}
|
||||
box.innerHTML = '<div class="muted">загрузка…</div>';
|
||||
try {
|
||||
const data = await api("/api/steam/friends");
|
||||
$("#friendsSummary").textContent = `Всего: ${data.total}`;
|
||||
if (data.private) {
|
||||
$("#friendsSummary").textContent = "Список закрыт";
|
||||
box.innerHTML = '<div class="empty">Список друзей недоступен (профиль Steam закрыт). Введи SteamID64 друга вручную.</div>';
|
||||
return;
|
||||
}
|
||||
if (!data.friends.length) {
|
||||
box.innerHTML = '<div class="empty">друзей не найдено</div>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = data.friends.map(friendCardHtml).join("");
|
||||
box.querySelectorAll(".friend-card").forEach((card) => {
|
||||
card.addEventListener("click", () => openFriendCompare(card.dataset.steamid));
|
||||
});
|
||||
} catch (err) {
|
||||
box.innerHTML = `<div class="empty">${esc(err.message)}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function openFriendCompare(steamid) {
|
||||
const box = $("#friendCompare");
|
||||
box.classList.remove("hidden");
|
||||
box.innerHTML = '<div class="muted">загрузка…</div>';
|
||||
box.scrollIntoView({ behavior: "smooth", block: "start" });
|
||||
try {
|
||||
const data = await api(`/api/steam/friends/${encodeURIComponent(steamid)}`);
|
||||
box.innerHTML = `
|
||||
<div class="friend-head">
|
||||
${data.avatar ? `<img src="${esc(data.avatar)}" alt="" width="56" height="56">` : ""}
|
||||
<div class="friend-title">
|
||||
<h2>${esc(data.name)}</h2>
|
||||
<div class="muted">общих: ${data.common_count} · всего у друга: ${data.total_games} · вне библиотеки: ${data.friend_only_count}</div>
|
||||
</div>
|
||||
<button id="friendClose" class="link" type="button">закрыть</button>
|
||||
</div>
|
||||
<div class="friend-columns">
|
||||
<div class="compare-col"><h3>есть у обоих</h3>${friendGameList(data.common || [], true)}</div>
|
||||
<div class="compare-col"><h3>только у друга</h3>${friendGameList(data.friend_only || [], false)}</div>
|
||||
</div>`;
|
||||
$("#friendClose").addEventListener("click", () => box.classList.add("hidden"));
|
||||
box.querySelectorAll("button[data-appid]").forEach((button) => {
|
||||
button.addEventListener("click", () => openGame(Number(button.dataset.appid)));
|
||||
});
|
||||
} catch (err) {
|
||||
box.innerHTML = `<div class="empty">${esc(err.message)}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
$("#friendCompareBtn").addEventListener("click", () => {
|
||||
const value = $("#friendSteamId").value.trim();
|
||||
if (!/^\d{17}$/.test(value)) {
|
||||
toast("SteamID64 — 17 цифр");
|
||||
return;
|
||||
}
|
||||
openFriendCompare(value);
|
||||
});
|
||||
$("#friendSteamId").addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter") $("#friendCompareBtn").click();
|
||||
});
|
||||
|
||||
/* ---------- game modal ---------- */
|
||||
|
||||
function modalSkeleton() {
|
||||
@@ -1568,12 +1668,17 @@ function switchView(view) {
|
||||
$("#libraryView").classList.toggle("hidden", view !== "library");
|
||||
$("#recommendView").classList.toggle("hidden", view !== "recommend");
|
||||
$("#hltbView").classList.toggle("hidden", view !== "hltb");
|
||||
$("#friendsView").classList.toggle("hidden", view !== "friends");
|
||||
$("#servicesView").classList.toggle("hidden", view !== "services");
|
||||
if (view === "hltb") loadHltb(true);
|
||||
if (view === "friends") loadFriends();
|
||||
try {
|
||||
const url = new URL(location.href);
|
||||
if (view === "recommend") url.searchParams.set("view", "recommend");
|
||||
else url.searchParams.delete("view");
|
||||
if (view === "recommend" || view === "friends") {
|
||||
url.searchParams.set("view", view);
|
||||
} else {
|
||||
url.searchParams.delete("view");
|
||||
}
|
||||
history.replaceState(null, "", url);
|
||||
} catch (_) {}
|
||||
}
|
||||
@@ -2080,6 +2185,8 @@ if ("serviceWorker" in navigator) {
|
||||
if (preset || mood) await runRecommend(false);
|
||||
} else if (params.get("view") === "hltb") {
|
||||
switchView("hltb");
|
||||
} else if (params.get("view") === "friends") {
|
||||
switchView("friends");
|
||||
} else if (params.get("view") === "services") {
|
||||
switchView("services");
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ try {
|
||||
aria-selected="false" aria-controls="recommendView" tabindex="-1">Рекомендации</button>
|
||||
<button class="tab" id="tabHltb" role="tab" data-view="hltb"
|
||||
aria-selected="false" aria-controls="hltbView" tabindex="-1">HLTB</button>
|
||||
<button class="tab" id="tabFriends" role="tab" data-view="friends"
|
||||
aria-selected="false" aria-controls="friendsView" tabindex="-1">Друзья</button>
|
||||
<button class="tab" id="tabServices" role="tab" data-view="services"
|
||||
aria-selected="false" aria-controls="servicesView" tabindex="-1">Сервисы</button>
|
||||
</nav>
|
||||
@@ -390,6 +392,22 @@ try {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section id="friendsView" class="hidden" role="tabpanel" aria-labelledby="tabFriends">
|
||||
<div class="friends-page">
|
||||
<div class="hltb-head">
|
||||
<h1>Друзья</h1>
|
||||
<div class="muted" id="friendsSummary"></div>
|
||||
</div>
|
||||
<div class="friends-manual">
|
||||
<input id="friendSteamId" type="text" inputmode="numeric" autocomplete="off"
|
||||
placeholder="SteamID64 друга" aria-label="SteamID64 друга">
|
||||
<button id="friendCompareBtn" class="ghost" type="button">сравнить</button>
|
||||
</div>
|
||||
<div id="friendsList" class="friends-list"></div>
|
||||
<div id="friendCompare" class="friend-compare hidden" aria-live="polite"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section id="servicesView" class="hidden" role="tabpanel" aria-labelledby="tabServices">
|
||||
<div class="services-page">
|
||||
<h1>Сервисы</h1>
|
||||
|
||||
+48
-2
@@ -919,6 +919,52 @@ html[data-theme="light"] .game-table tbody tr:hover { background: rgba(23, 32, 5
|
||||
.compare-score { margin-left: 6px; font-size: 12px; color: var(--accent-2); }
|
||||
.compare-jaccard { margin-bottom: 4px; font-size: 13px; color: var(--text); }
|
||||
|
||||
.friends-page { display: flex; flex-direction: column; gap: 18px; }
|
||||
.friends-manual { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
||||
.friends-manual input { flex: 1 1 220px; max-width: 320px; }
|
||||
.friends-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
|
||||
.friend-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 14px;
|
||||
background: var(--panel);
|
||||
text-align: left;
|
||||
color: inherit;
|
||||
transition: .15s;
|
||||
}
|
||||
.friend-card:hover { border-color: var(--accent); }
|
||||
.friend-card img, .friend-avatar {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 10px;
|
||||
background: var(--bg-2);
|
||||
object-fit: cover;
|
||||
}
|
||||
.friend-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
||||
.friend-info b { font-size: 14px; }
|
||||
.friend-compare {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
padding: 16px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 16px;
|
||||
background: var(--panel);
|
||||
}
|
||||
.friend-compare[hidden] { display: none; }
|
||||
.friend-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
|
||||
.friend-head img { width: 56px; height: 56px; border-radius: 12px; background: var(--bg-2); }
|
||||
.friend-title { flex: 1 1 200px; }
|
||||
.friend-title h2 { margin: 0 0 2px; font-size: 20px; }
|
||||
.friend-head #friendClose { margin-left: auto; }
|
||||
.friend-columns { display: flex; flex-wrap: wrap; gap: 18px; }
|
||||
.friend-columns .compare-col { flex: 1 1 260px; min-width: 220px; }
|
||||
.friend-columns ul { max-height: 360px; overflow-y: auto; }
|
||||
.friend-columns li { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
|
||||
|
||||
.reco-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 16px; }
|
||||
.reco-actions[hidden] { display: none; }
|
||||
.reco-actions .ghost { flex: 0 0 auto; padding: 7px 12px; }
|
||||
@@ -1275,8 +1321,8 @@ html[data-theme="light"] ::-webkit-scrollbar-thumb { background: #c9c0ad; }
|
||||
@media (max-width: 720px) {
|
||||
.topbar { position: relative; flex-wrap: wrap; gap: 12px; padding: 12px 68px 12px 16px; }
|
||||
.theme-toggle { position: absolute; top: 12px; right: 16px; }
|
||||
.tabs { gap: 2px; padding: 3px; }
|
||||
.tab { padding: 6px 9px; font-size: 12px; }
|
||||
.tabs { gap: 2px; padding: 3px; max-width: 100%; overflow-x: auto; }
|
||||
.tab { padding: 6px 9px; font-size: 12px; white-space: nowrap; }
|
||||
.stats { width: 100%; justify-content: flex-start; }
|
||||
main { padding: 18px 16px 60px; }
|
||||
.grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
|
||||
|
||||
Reference in new issue
Block a user