66 lines
No EOL
1.9 KiB
TypeScript
66 lines
No EOL
1.9 KiB
TypeScript
import { Rcon } from "rconts";
|
|
import { EventEmitter } from "tseep"
|
|
import config from "./config.json" assert { type: "json" };
|
|
export const apiEvents = new EventEmitter<{
|
|
state: (currentUser: string, amount: number, done: number) => void
|
|
}>();
|
|
|
|
let lastUpdated = 0;
|
|
let authenicated = false;
|
|
const advancements = new Map<string, number>();
|
|
|
|
const client = new Rcon({
|
|
host: config.rcon.host,
|
|
port: config.rcon.port,
|
|
password: config.rcon.password
|
|
}
|
|
);
|
|
|
|
client.on("connected", () => {
|
|
console.log("RCON connected.");
|
|
});
|
|
|
|
client.on("authenicated", () => {
|
|
authenicated = true;
|
|
});
|
|
|
|
export function getAdvancements(): Promise<Map<string, number>> {
|
|
if (Date.now() - lastUpdated < 60000) {
|
|
return Promise.resolve(advancements);
|
|
}
|
|
|
|
lastUpdated = Date.now();
|
|
advancements.clear();
|
|
return new Promise(async (res, rej) => {
|
|
if (!authenicated) {
|
|
rej("Not authenicated.");
|
|
return;
|
|
}
|
|
|
|
const list = await client.exec("whitelist list");
|
|
const whitelistedPlayers = list.replace(/There are \d* whitelisted player\(s\): /gm, "").split(", ");
|
|
let amount = 0;
|
|
apiEvents.emit("state", "-", whitelistedPlayers.length, amount)
|
|
|
|
for await(const name of whitelistedPlayers) {
|
|
const data = await client.exec(`scoreboard players get ${name} bac_advancements`);
|
|
const advancementRegex = /^([^ ]*) has (\d*) \[Advancements\]$/gm;
|
|
const noAdvancements =
|
|
/^Can't get value of bac_advancements for ([^;]*); none is set$/gm;
|
|
|
|
if (advancementRegex.test(data)) {
|
|
advancements.set(
|
|
data.replace(advancementRegex, "$1"),
|
|
+data.replace(advancementRegex, "$2")
|
|
);
|
|
} else if (noAdvancements.test(data)) {
|
|
advancements.set(data.replace(noAdvancements, "$1"), 0);
|
|
}
|
|
amount++;
|
|
apiEvents.emit("state", name, whitelistedPlayers.length, amount)
|
|
}
|
|
res(advancements);
|
|
});
|
|
}
|
|
|
|
client.connect(); |