mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
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.
This commit is contained in:
1 parent
92e6511d91
commit
f03d6c758e
25 files changed
+3330
-70
No files matched your search
@@ -16,8 +16,8 @@
|
||||
.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 {
|
||||
width: 100%; margin: 0 0 12px; box-sizing: border-box; padding: 6px 8px;
|
||||
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>
|
||||
@@ -25,97 +25,93 @@
|
||||
<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;
|
||||
|
||||
let ws, seq = 0, pending = new Map(), pollTimer, backoff = 500;
|
||||
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;
|
||||
|
||||
function rpc(method, params) {
|
||||
const id = "r-" + (++seq);
|
||||
return new Promise((resolve, reject) => {
|
||||
pending.set(id, { resolve, reject });
|
||||
ws.send(JSON.stringify({ jsonrpc: "2.0", id, method, params: params || {} }));
|
||||
});
|
||||
}
|
||||
langInput.onchange = function () { text.setLanguage(langInput.value); };
|
||||
|
||||
function fmt(n) {
|
||||
try { return BigInt(n).toString(); } catch { return String(n ?? ""); }
|
||||
}
|
||||
|
||||
function pct(up, need) {
|
||||
try {
|
||||
const n = BigInt(need || "0");
|
||||
if (n === 0n) return 0;
|
||||
return Number(BigInt(up || "0") * 10000n / n) / 100;
|
||||
} catch { return 0; }
|
||||
}
|
||||
|
||||
async function tick() {
|
||||
const tech = await rpc("data.read", { root: "history", path: ["currentTech"] });
|
||||
if (tech.error) throw new Error(tech.error.data && tech.error.data.kind || tech.error.message);
|
||||
const id = tech.result.value;
|
||||
if (!id) {
|
||||
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 = "";
|
||||
statusEl.textContent = "session " + (tech.result.sessionId || "");
|
||||
return;
|
||||
}
|
||||
const state = await rpc("data.read", {
|
||||
root: "history",
|
||||
path: ["techStates", { key: id }],
|
||||
select: ["curLevel", "hashUploaded", "hashNeeded"]
|
||||
});
|
||||
if (state.error) throw new Error(state.error.data && state.error.data.kind || state.error.message);
|
||||
const v = state.result.value;
|
||||
const p = pct(v.hashUploaded, v.hashNeeded);
|
||||
titleEl.textContent = "Tech " + id + " Lv " + v.curLevel;
|
||||
fillEl.style.width = p.toFixed(2) + "%";
|
||||
metaEl.textContent = fmt(v.hashUploaded) + " / " + fmt(v.hashNeeded) + " (" + p.toFixed(1) + "%)";
|
||||
statusEl.textContent = "session " + (state.result.sessionId || "");
|
||||
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) + "%)";
|
||||
}
|
||||
|
||||
function connect() {
|
||||
statusEl.textContent = "Connecting";
|
||||
ws = new WebSocket(urlInput.value);
|
||||
ws.onopen = () => {
|
||||
backoff = 500;
|
||||
statusEl.textContent = "Connected";
|
||||
pollTimer = setInterval(() => { tick().catch(err => { statusEl.textContent = err.message; }); }, 1000);
|
||||
tick().catch(err => { statusEl.textContent = err.message; });
|
||||
};
|
||||
ws.onmessage = ev => {
|
||||
const msg = JSON.parse(ev.data);
|
||||
const wait = pending.get(msg.id);
|
||||
if (!wait) return;
|
||||
pending.delete(msg.id);
|
||||
wait.resolve(msg);
|
||||
};
|
||||
ws.onclose = () => {
|
||||
clearInterval(pollTimer);
|
||||
pending.forEach(p => p.reject(new Error("disconnected")));
|
||||
pending.clear();
|
||||
titleEl.textContent = "Disconnected";
|
||||
statusEl.textContent = "Retrying";
|
||||
setTimeout(connect, backoff);
|
||||
backoff = Math.min(backoff * 2, 8000);
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
urlInput.onchange = () => { if (ws) ws.close(); };
|
||||
connect();
|
||||
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>
|
||||
@@ -0,0 +1,134 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LiveStreamAssist stats</title>
|
||||
<style>
|
||||
:root { color-scheme: dark; }
|
||||
html, body { margin: 0; background: transparent; font: 16px/1.4 system-ui, sans-serif; color: #f2f5f8; }
|
||||
.card {
|
||||
min-width: 360px; max-width: 520px; padding: 16px 18px;
|
||||
background: rgba(12, 18, 28, 0.72); border-radius: 10px;
|
||||
}
|
||||
.row { display: flex; justify-content: space-between; gap: 12px; margin: 6px 0; font-variant-numeric: tabular-nums; }
|
||||
.label { opacity: 0.65; }
|
||||
h1 { margin: 0 0 8px; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; opacity: 0.7; font-weight: 600; }
|
||||
h2 { margin: 14px 0 6px; font-size: 18px; font-weight: 650; }
|
||||
input, select {
|
||||
width: 100%; margin: 0 0 8px; box-sizing: border-box; padding: 6px 8px;
|
||||
border: 0; border-radius: 6px; background: #1d2a3c; color: inherit;
|
||||
}
|
||||
.status { margin-top: 10px; font-size: 13px; opacity: 0.7; }
|
||||
</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>
|
||||
<input id="itemId" value="1101" title="Item ID">
|
||||
<h1>Item</h1>
|
||||
<h2 id="itemName">—</h2>
|
||||
<div class="row"><span class="label">Production / min</span><span id="prod">—</span></div>
|
||||
<div class="row"><span class="label">Theoretical / min</span><span id="prodRef">—</span></div>
|
||||
<div class="row"><span class="label">Consumption / min</span><span id="cons">—</span></div>
|
||||
<div class="row"><span class="label">Theoretical / min</span><span id="consRef">—</span></div>
|
||||
<h1>Research</h1>
|
||||
<h2 id="techName">—</h2>
|
||||
<div class="row"><span class="label">Progress</span><span id="techProg">—</span></div>
|
||||
<h1>Dyson sphere</h1>
|
||||
<div class="row"><span class="label">Generation</span><span id="dyson">—</span></div>
|
||||
<div class="status" id="status">Connecting</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 itemInput = document.getElementById("itemId");
|
||||
const statusEl = document.getElementById("status");
|
||||
urlInput.value = qs.get("ws") || urlInput.value;
|
||||
itemInput.value = qs.get("item") || itemInput.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 itemId = parseInt(itemInput.value, 10) || 1101;
|
||||
const results = await Promise.allSettled([
|
||||
query.getItemRates(itemId),
|
||||
query.getCurrentResearch(),
|
||||
query.getDysonPower()
|
||||
]);
|
||||
if (generation !== pollGeneration) return;
|
||||
const failed = results.find(function (result) { return result.status === "rejected"; });
|
||||
if (failed) throw failed.reason;
|
||||
const [rates, research, dyson] = results.map(function (result) { return result.value; });
|
||||
document.getElementById("itemName").textContent = text.itemName(itemId) || ("Item " + itemId);
|
||||
document.getElementById("prod").textContent = text.formatRate(rates.production);
|
||||
document.getElementById("prodRef").textContent = text.formatRate(rates.theoreticalProduction);
|
||||
document.getElementById("cons").textContent = text.formatRate(rates.consumption);
|
||||
document.getElementById("consRef").textContent = text.formatRate(rates.theoreticalConsumption);
|
||||
if (!research.active) {
|
||||
document.getElementById("techName").textContent = "No active research";
|
||||
document.getElementById("techProg").textContent = "";
|
||||
} else {
|
||||
document.getElementById("techName").textContent = text.techDisplayName(research.techId, research.curLevel) || ("Tech " + research.techId);
|
||||
document.getElementById("techProg").textContent = (research.progressPercent || 0).toFixed(1) + "%";
|
||||
}
|
||||
document.getElementById("dyson").textContent = text.formatPower(dyson.watts);
|
||||
}
|
||||
|
||||
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); }, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
Reference in new issue
Block a user