- 一个端口按路径分发:/cnchess /cngo /cs 进程型,/alonelyroad /myword 静态目录 - 进程型首次访问以动态端口拉起子进程,就绪探测后转发;空闲 600s 且无 WS 连接才回收 - WebSocket 走原始 TCP 隧道(握手转发 + 101 透传 + 双向对拷,任一方向 EOF 即拆连) - 文本响应就地重写根绝对路径(href/src/fetch/url(/ 与 location.host}/ws),游戏源码零改动 - 入口页只显示状态与进入入口,不提供管理按钮;网关/运维接口为 /api/* - 配置 games.json 磁盘优先、内嵌兜底,改完刷新即生效
218 lines
6.0 KiB
JavaScript
218 lines
6.0 KiB
JavaScript
/* 游戏中心入口:拉状态 → 渲染卡片 → 点击唤醒后跳转。
|
|
这里只负责「显示 + 进入」,不做进程管理(拉起/释放由中心自己按访问和空闲决定)。 */
|
|
|
|
const cards = new Map();
|
|
let toastTimer = null;
|
|
|
|
const $ = (sel) => document.querySelector(sel);
|
|
|
|
/* ----------------------------- 工具 ----------------------------- */
|
|
|
|
function fmtDur(sec) {
|
|
sec = Math.max(0, Math.floor(sec || 0));
|
|
if (sec < 60) return `${sec}s`;
|
|
const m = Math.floor(sec / 60);
|
|
const s = sec % 60;
|
|
if (m < 60) return s ? `${m}m${String(s).padStart(2, "0")}s` : `${m}m`;
|
|
const h = Math.floor(m / 60);
|
|
return `${h}h${String(m % 60).padStart(2, "0")}m`;
|
|
}
|
|
|
|
function fmtIdle(sec) {
|
|
if (sec >= 60 && sec % 60 === 0) return `${sec / 60} 分钟`;
|
|
if (sec >= 60) return `${Math.round(sec / 60)} 分钟`;
|
|
return `${sec} 秒`;
|
|
}
|
|
|
|
function toast(msg, isErr) {
|
|
const el = $("#toast");
|
|
el.textContent = msg;
|
|
el.classList.toggle("err", !!isErr);
|
|
el.classList.add("show");
|
|
clearTimeout(toastTimer);
|
|
toastTimer = setTimeout(() => el.classList.remove("show"), isErr ? 5200 : 2200);
|
|
}
|
|
|
|
async function api(path, opts) {
|
|
const res = await fetch(path, {
|
|
cache: "no-store",
|
|
headers: { "content-type": "application/json" },
|
|
...opts,
|
|
});
|
|
let data = null;
|
|
try {
|
|
data = await res.json();
|
|
} catch (_) {
|
|
/* 非 JSON,交给调用方按状态码处理 */
|
|
}
|
|
return { res, data };
|
|
}
|
|
|
|
/* ----------------------------- 渲染 ----------------------------- */
|
|
|
|
function buildCard(g) {
|
|
const el = document.createElement("article");
|
|
el.className = "card";
|
|
el.dataset.id = g.id;
|
|
el.style.setProperty("--accent", g.accent || "#4a8dff");
|
|
el.innerHTML = `
|
|
<div class="card-head">
|
|
<span class="glyph"></span>
|
|
<span class="pill" data-role="pill">—</span>
|
|
</div>
|
|
<h2 data-role="name"></h2>
|
|
<p class="tag" data-role="tag"></p>
|
|
<ul class="meta" data-role="meta"></ul>
|
|
<div class="card-foot">
|
|
<span class="stat" data-role="stat">—</span>
|
|
<span class="go">进入 →</span>
|
|
</div>`;
|
|
|
|
el.querySelector(".glyph").textContent = g.icon || g.name.slice(0, 1);
|
|
el.querySelector('[data-role="name"]').textContent = g.name;
|
|
el.querySelector('[data-role="tag"]').textContent = g.tagline || "";
|
|
|
|
const meta = el.querySelector('[data-role="meta"]');
|
|
const chips = [...(g.meta || [])];
|
|
if (g.stack) chips.push(g.stack);
|
|
for (const c of chips) {
|
|
const li = document.createElement("li");
|
|
li.textContent = c;
|
|
meta.appendChild(li);
|
|
}
|
|
|
|
el.addEventListener("click", (ev) => {
|
|
ev.preventDefault();
|
|
if (el.classList.contains("loading")) return;
|
|
enterGame(g.id, el);
|
|
});
|
|
|
|
return el;
|
|
}
|
|
|
|
function paint(el, g) {
|
|
const pill = el.querySelector('[data-role="pill"]');
|
|
const stat = el.querySelector('[data-role="stat"]');
|
|
|
|
let cls = "asleep";
|
|
let label = "休眠";
|
|
let statText = "";
|
|
|
|
if (g.state === "static") {
|
|
if (!g.ready) {
|
|
cls = "error";
|
|
label = "缺目录";
|
|
statText = "目录不存在 · 检查 games.json 里的 dir";
|
|
} else {
|
|
cls = "static";
|
|
label = "静态";
|
|
statText = "直接托管 · 不占进程";
|
|
}
|
|
} else if (g.state === "running") {
|
|
cls = "running";
|
|
label = "运行中";
|
|
const ws = g.ws_conns > 0 ? ` · 联机中×${g.ws_conns}` : "";
|
|
const rel = g.release_in_secs != null ? ` · ${fmtDur(g.release_in_secs)}后释放` : "";
|
|
statText = `已运行 ${fmtDur(g.uptime_secs)}${ws}${rel}`;
|
|
} else if (g.state === "error") {
|
|
cls = "error";
|
|
label = "异常";
|
|
statText = g.error || "拉起失败";
|
|
} else if (!g.ready) {
|
|
cls = "error";
|
|
label = "未构建";
|
|
statText = "二进制缺失";
|
|
} else {
|
|
cls = "asleep";
|
|
label = "休眠";
|
|
statText = "点击即拉起";
|
|
}
|
|
|
|
if (el.dataset.state === "starting") {
|
|
cls = "starting";
|
|
label = "启动中";
|
|
statText = "正在拉起进程…";
|
|
}
|
|
|
|
pill.className = `pill ${cls}`;
|
|
pill.textContent = label;
|
|
stat.textContent = statText;
|
|
stat.title = statText;
|
|
}
|
|
|
|
function applyState(data) {
|
|
const list = data.games || [];
|
|
const seen = new Set();
|
|
|
|
for (const g of list) {
|
|
seen.add(g.id);
|
|
let el = cards.get(g.id);
|
|
if (!el) {
|
|
el = buildCard(g);
|
|
cards.set(g.id, el);
|
|
$("#cards").appendChild(el);
|
|
}
|
|
paint(el, g);
|
|
}
|
|
|
|
// 清单里被删掉的游戏,卡片也撤掉
|
|
for (const [id, el] of cards) {
|
|
if (!seen.has(id)) {
|
|
el.remove();
|
|
cards.delete(id);
|
|
}
|
|
}
|
|
|
|
const running = list.filter((g) => g.state === "running").length;
|
|
$("#runningCount").textContent = running;
|
|
$("#totalCount").textContent = list.length;
|
|
$("#runtimeDot").classList.toggle("on", running > 0);
|
|
|
|
const idle = data.idle_secs || 0;
|
|
$("#idleLabel").textContent = fmtIdle(idle);
|
|
$("#idleLabel2").textContent = fmtIdle(idle);
|
|
$("#portLabel").textContent = `:${data.port || 1818}`;
|
|
}
|
|
|
|
async function refresh() {
|
|
try {
|
|
const { res, data } = await api("/api/games");
|
|
if (!res.ok || !data || !data.ok) throw new Error(`状态接口 ${res.status}`);
|
|
applyState(data);
|
|
} catch (e) {
|
|
$("#runtimeDot").classList.remove("on");
|
|
toast(`拿不到游戏状态:${e.message}`, true);
|
|
}
|
|
}
|
|
|
|
/* ----------------------------- 进入 ----------------------------- */
|
|
|
|
async function enterGame(id, el) {
|
|
el.classList.add("loading");
|
|
el.dataset.state = "starting";
|
|
paint(el, { state: "asleep", ready: true });
|
|
try {
|
|
const { res, data } = await api(`/api/games/${id}/wake`, { method: "POST" });
|
|
if (!res.ok || !data || !data.ok) {
|
|
throw new Error((data && data.error) || `HTTP ${res.status}`);
|
|
}
|
|
location.href = data.path;
|
|
} catch (e) {
|
|
el.classList.remove("loading");
|
|
delete el.dataset.state;
|
|
toast(`没能拉起:${String(e.message).split("\n")[0]}`, true);
|
|
refresh();
|
|
}
|
|
}
|
|
|
|
/* ----------------------------- 启动 ----------------------------- */
|
|
|
|
refresh();
|
|
setInterval(() => {
|
|
if (!document.hidden) refresh();
|
|
}, 2000);
|
|
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (!document.hidden) refresh();
|
|
});
|