MVP version
This commit is contained in:
commit
3f96943858
25 changed files with 3962 additions and 0 deletions
62
src/services/status.js
Normal file
62
src/services/status.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import fs from "node:fs";
|
||||
import { config } from "../config.js";
|
||||
|
||||
function readStatusFile() {
|
||||
try {
|
||||
const raw = fs.readFileSync(config.statusFile, "utf8");
|
||||
const parsed = JSON.parse(raw);
|
||||
return Array.isArray(parsed?.servers) ? parsed.servers : [];
|
||||
} catch (error) {
|
||||
console.error(`[status] Failed to read status file ${config.statusFile}:`, error.message);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function mergeServersWithStatus(servers) {
|
||||
const statuses = readStatusFile();
|
||||
return statuses.map((status) => {
|
||||
const server = findConfiguredServer(status, servers);
|
||||
const id = status.id || server?.id || createServerId(status);
|
||||
|
||||
return {
|
||||
...(server || {}),
|
||||
id,
|
||||
game: status.game || server?.game || id,
|
||||
address: status.address || server?.address || "",
|
||||
port: status.port ?? server?.port ?? null,
|
||||
active: Boolean(status.active),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function findConfiguredServer(status, servers) {
|
||||
return (
|
||||
servers.find((server) => status.id && server.id === status.id) ||
|
||||
servers.find((server) => sameEndpoint(server, status)) ||
|
||||
servers.find((server) => server.game === status.game) ||
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
function sameEndpoint(server, status) {
|
||||
if (!server.address || !status.address) return false;
|
||||
return normalizeAddress(server.address) === normalizeAddress(status.address);
|
||||
}
|
||||
|
||||
function normalizeAddress(address) {
|
||||
return String(address).trim().toLowerCase();
|
||||
}
|
||||
|
||||
function createServerId(status) {
|
||||
const base = `${status.game || "server"}-${status.address || ""}-${status.port || ""}`;
|
||||
return (
|
||||
base
|
||||
.toLowerCase()
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "") || "server"
|
||||
);
|
||||
}
|
||||
|
||||
export { createServerId, mergeServersWithStatus };
|
||||
Loading…
Add table
Add a link
Reference in a new issue