cleanup + server list on websocket connect

This commit is contained in:
Lee
2024-01-01 22:16:13 +00:00
parent 09f7b700ca
commit 9e17262804
6 changed files with 64 additions and 32 deletions

View File

@ -80,19 +80,20 @@ export default class Server {
* Pings a server and gets the response.
*
* @param server the server to ping
* @param insertPing whether to insert the ping into the database
* @returns the ping response or undefined if the server is offline
*/
public pingServer(server: Server): Promise<Ping | undefined> {
switch (server.getType()) {
public pingServer(): Promise<Ping | undefined> {
switch (this.getType()) {
case "PC": {
return this.pingPCServer(server);
return this.pingPCServer();
}
case "PE": {
return this.pingPEServer(server);
return this.pingPEServer();
}
default: {
throw new Error(
`Unknown server type ${server.getType()} for ${server.getName()}`
`Unknown server type ${this.getType()} for ${this.getName()}`
);
}
}
@ -104,10 +105,10 @@ export default class Server {
* @param server the server to ping
* @returns the ping response or undefined if the server is offline
*/
private async pingPCServer(server: Server): Promise<Ping | undefined> {
private async pingPCServer(): Promise<Ping | undefined> {
if (this.dnsInfo.resolvedServer == undefined && !this.dnsInfo.hasResolved) {
try {
const resolvedServer = await resolveDns(server.getIP());
const resolvedServer = await resolveDns(this.getIP());
this.dnsInfo = {
hasResolved: true,
@ -125,7 +126,7 @@ export default class Server {
ip = resolvedServer.ip;
port = resolvedServer.port;
} else {
ip = server.getIP();
ip = this.getIP();
port = 25565; // The default port
}
@ -139,7 +140,7 @@ export default class Server {
this.favicon = res.favicon; // Set the favicon
resolve({
id: server.getID(),
id: this.getID(),
timestamp: Date.now(),
ip: ip,
playerCount: res.players.online,
@ -154,20 +155,20 @@ export default class Server {
* @param server the server to ping
* @returns the ping response or undefined if the server is offline
*/
private async pingPEServer(server: Server): Promise<Ping | undefined> {
private async pingPEServer(): Promise<Ping | undefined> {
return new Promise((resolve, reject) => {
bedrockPing(
server.getIP(),
server.getPort() || 19132,
this.getIP(),
this.getPort() || 19132,
(err: any, res: any) => {
if (err || res == undefined) {
return reject(err);
}
resolve({
id: server.getID(),
id: this.getID(),
timestamp: Date.now(),
ip: server.getIP(),
ip: this.getIP(),
playerCount: res.currentPlayers,
});
}

View File

@ -5,16 +5,23 @@ import Servers from "../../data/servers.json";
export default class ServerManager {
private servers: Server[] = [];
constructor() {
for (const server of Servers) {
this.servers.push(
new Server({
id: server.id,
ip: server.ip,
name: server.name,
type: server.type as ServerType,
})
);
constructor() {}
/**
* Loads the servers from the config file.
*/
async init() {
for (const configServer of Servers) {
const server = new Server({
id: configServer.id,
ip: configServer.ip,
name: configServer.name,
type: configServer.type as ServerType,
});
try {
await server.pingServer();
} catch (err) {}
this.servers.push(server);
}
}