Files
soarqin f03d6c758e feat(livestreamassist): add resilient JavaScript overlay client
Add a JSON-RPC client, session-aware game queries, and generated offline locale data. Update research and stats overlays with cancellable single-flight polling and reconnect handling.

Add 41 JavaScript regression tests, Python locale and formatting checks, and generation and validation documentation.
2026-09-18 01:59:53 +08:00

118 lines
4.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>LiveStreamAssist research</title>
<style>
:root { color-scheme: dark; }
html, body { margin: 0; background: transparent; font: 18px/1.4 system-ui, sans-serif; color: #f2f5f8; }
.card {
min-width: 320px; max-width: 420px; padding: 16px 18px;
background: rgba(12, 18, 28, 0.72); border-radius: 10px;
}
.label { opacity: 0.65; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; }
.title { margin: 4px 0 10px; font-size: 22px; font-weight: 650; }
.bar { height: 10px; background: #1d2a3c; border-radius: 999px; overflow: hidden; }
.fill { height: 100%; width: 0; background: #3d9cff; }
.meta { margin-top: 8px; font-variant-numeric: tabular-nums; font-size: 14px; opacity: 0.9; }
.status { margin-top: 8px; font-size: 13px; opacity: 0.7; }
input, select {
width: 100%; margin: 0 0 8px; box-sizing: border-box; padding: 6px 8px;
border: 0; border-radius: 6px; background: #1d2a3c; color: inherit;
}
</style>
</head>
<body>
<div class="card">
<input id="url" value="ws://127.0.0.1:18080/api/v1">
<select id="lang">
<option value="1033">English</option>
<option value="2052">简体中文</option>
<option value="1036">Français</option>
<option value="1031">Deutsch</option>
<option value="1041">日本語</option>
</select>
<div class="label">Current research</div>
<div class="title" id="title">Connecting…</div>
<div class="bar"><div class="fill" id="fill"></div></div>
<div class="meta" id="meta"></div>
<div class="status" id="status"></div>
</div>
<script src="../js/generated/dsp-text.js"></script>
<script src="../js/livestream-assist.js"></script>
<script>
const qs = new URLSearchParams(location.search);
const urlInput = document.getElementById("url");
const langInput = document.getElementById("lang");
const titleEl = document.getElementById("title");
const fillEl = document.getElementById("fill");
const metaEl = document.getElementById("meta");
const statusEl = document.getElementById("status");
urlInput.value = qs.get("ws") || urlInput.value;
const text = LiveStreamAssist.GameText.fromGenerated({ language: qs.get("lang") || langInput.value });
langInput.value = String(text.lcid);
if (!langInput.value) { langInput.value = "1033"; text.setLanguage(1033); }
const client = new LiveStreamAssist.Client({
url: urlInput.value,
onStatus: function (status) { statusEl.textContent = status; }
});
const query = new LiveStreamAssist.GameQuery(client);
let pollTimer;
let pollGeneration = 0;
let validatedInfo = null;
langInput.onchange = function () { text.setLanguage(langInput.value); };
async function tick(generation) {
const research = await query.getCurrentResearch();
if (generation !== pollGeneration) return;
if (!research.active) {
titleEl.textContent = "No active research";
fillEl.style.width = "0%";
metaEl.textContent = "";
return;
}
const percentage = research.progressPercent || 0;
titleEl.textContent = text.techDisplayName(research.techId, research.curLevel) || ("Tech " + research.techId);
fillEl.style.width = Math.max(0, Math.min(100, percentage)).toFixed(2) + "%";
metaEl.textContent = text.formatKmg(research.hashUploaded) + " / " + text.formatKmg(research.hashNeeded) + " (" + percentage.toFixed(1) + "%)";
}
async function poll(generation) {
if (generation !== pollGeneration) return;
try {
await client.connect(urlInput.value);
if (generation !== pollGeneration) return;
if (!client.infoSnapshot || validatedInfo !== client.infoSnapshot) {
const info = await client.systemInfo();
if (generation !== pollGeneration) return;
await client.validate({ apiMajor: 1, requiredCapabilities: ["reflection.read"] });
if (generation !== pollGeneration) return;
validatedInfo = info;
}
await tick(generation);
if (generation === pollGeneration) statusEl.textContent = "connected";
} catch (error) {
if (generation === pollGeneration) statusEl.textContent = error.kind || error.message;
} finally {
if (generation === pollGeneration) pollTimer = setTimeout(function () { poll(generation); }, 1000);
}
}
function stop() {
pollGeneration += 1;
clearTimeout(pollTimer);
client.close();
validatedInfo = null;
}
function start() { stop(); poll(pollGeneration); }
urlInput.onchange = start;
addEventListener("pagehide", stop);
start();
</script>
</body>
</html>