-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (54 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require("./utils/console");
require("./utils/process");
try{
global.config = require("./config.json");
}catch(e) {
console.warn("Creating config.json");
fs.writeFileSync("./config.json", "{}");
process.exit();
};
const validator = require("./utils/validator");
const child = require("child_process");
const scpsl = require("./utils/scpsl");
const chalk = require("chalk");
const fs = require("fs");
/*
LOGO
*/
console.raw(chalk.cyan(fs.readFileSync("./logo.md", "utf-8")));
validator(config);
global.clients = {};
/*
Starting client workers.
*/
config["BOTS"]["servers"].forEach(s => {
s.PORT = `${s.PORT}`;
clients[s.PORT] = child.fork("./child/client.js", [s.TOKEN]);
clients[s.PORT].on("message", (message) => {
if(typeof message === "string") {
console.log(`${chalk.hex(tools.getRandomHex())(s.PORT)} | ${message}`)
};
});
});
/*
SETUPPING INTERVAL
*/
setInterval(async () => {
let online = await scpsl.getServers();
if(Array.isArray(online) && online.length > 0) {
console.log("Updating online for clients...");
online.forEach(s => {
sendClientData(s);
});
} else {
console.warn("Hmmm, servers not found in SCPSL API. Retrying in 15seconds.");
};
}, 16500)
/*
OTHER FUNCTIONS
*/
function sendClientData(s) {
if(!clients[s.Port]) return console.warn(`Worker with PORT \'${chalk.bold(s.Port)}\' not found. Skipping...`);
clients[s.Port].send(s.Players);
clients[s.Port].send({ status: "cache", data: s });
}