Private
Public Access
1
0

refacto: Replaced useless DB queries by websocket calls + patching WS auth-token leak

This commit is contained in:
gauvainboiche
2026-04-01 18:47:37 +02:00
parent e28a2d6e9c
commit f161ccb0f0
33 changed files with 6246 additions and 43 deletions

View File

@@ -1,11 +1,27 @@
import "dotenv/config";
import { createServer } from "http";
import { loadConfigFile, getConfig } from "./configLoader.js";
import { initGameSchema, ensureSeedEpoch } from "./db/gameDb.js";
import { initUsersSchema } from "./db/usersDb.js";
import app from "./app.js";
import { startEconTick } from "./econTick.js";
import { initWebSocketHub, broadcast } from "./ws/hub.js";
const PORT = Number(process.env.PORT ?? 8080);
let lastConfigSignature = "";
function makeConfigSignature(cfg) {
return JSON.stringify({
dailyActionQuota: cfg.dailyActionQuota,
teamActionQuota: cfg.teamActionQuota,
databaseWipeoutIntervalSeconds: cfg.databaseWipeoutIntervalSeconds,
debugModeForTeams: cfg.debugModeForTeams,
configReloadIntervalSeconds: cfg.configReloadIntervalSeconds,
elementWorth: cfg.elementWorth ?? {},
resourceWorth: cfg.resourceWorth ?? { common: {}, rare: {} },
militaryPower: cfg.militaryPower ?? {},
});
}
// ── Config-file poll ──────────────────────────────────────────────────────────
// Periodically re-reads game.settings.json and checks for a seed-epoch change.
@@ -15,8 +31,27 @@ function scheduleConfigPoll() {
const ms = Math.max(5_000, getConfig().configReloadIntervalSeconds * 1_000);
setTimeout(async () => {
try {
const beforeSig = lastConfigSignature;
loadConfigFile();
await ensureSeedEpoch();
const worldSeed = await ensureSeedEpoch();
const cfg = getConfig();
const nextSig = makeConfigSignature(cfg);
if (beforeSig && nextSig !== beforeSig) {
broadcast("config-updated", {
worldSeed,
config: {
dailyActionQuota: cfg.dailyActionQuota,
teamActionQuota: cfg.teamActionQuota,
databaseWipeoutIntervalSeconds: cfg.databaseWipeoutIntervalSeconds,
debugModeForTeams: cfg.debugModeForTeams,
configReloadIntervalSeconds: cfg.configReloadIntervalSeconds,
elementWorth: cfg.elementWorth ?? {},
resourceWorth: cfg.resourceWorth ?? { common: {}, rare: {} },
militaryPower: cfg.militaryPower ?? {},
},
});
}
lastConfigSignature = nextSig;
} catch (e) {
console.error("[config poll]", e.message);
}
@@ -31,8 +66,12 @@ async function main() {
await initGameSchema();
await initUsersSchema();
await ensureSeedEpoch();
lastConfigSignature = makeConfigSignature(getConfig());
app.listen(PORT, () => {
const httpServer = createServer(app);
initWebSocketHub(httpServer);
httpServer.listen(PORT, () => {
const cfg = getConfig();
console.log(
`[server] Listening on :${PORT} dailyQuota=${cfg.dailyActionQuota} wipe=${cfg.databaseWipeoutIntervalSeconds}s`