add server ids

This commit is contained in:
Lee 2024-01-01 04:11:22 +00:00
parent 3f3088f017
commit 3750a00017
4 changed files with 73 additions and 56 deletions

@ -2,61 +2,73 @@
{ {
"name": "WildPrison", "name": "WildPrison",
"ip": "wildprison.net", "ip": "wildprison.net",
"type": "PC" "type": "PC",
"id": 0
}, },
{ {
"name": "Hypixel", "name": "Hypixel",
"ip": "mc.hypixel.net", "ip": "mc.hypixel.net",
"type": "PC" "type": "PC",
"id": 1
}, },
{ {
"name": "CubeCraft", "name": "CubeCraft",
"ip": "play.cubecraft.net", "ip": "play.cubecraft.net",
"type": "PC" "type": "PC",
"id": 2
}, },
{ {
"name": "Mineplex", "name": "Mineplex",
"ip": "mineplex.com", "ip": "mineplex.com",
"type": "PC" "type": "PC",
"id": 3
}, },
{ {
"name": "2b2t", "name": "2b2t",
"ip": "2b2t.org", "ip": "2b2t.org",
"type": "PC" "type": "PC",
"id": 4
}, },
{ {
"name": "AkumaMC", "name": "AkumaMC",
"ip": "akumamc.net", "ip": "akumamc.net",
"type": "PC" "type": "PC",
"id": 5
}, },
{ {
"name": "Wynncraft", "name": "Wynncraft",
"ip": "play.wynncraft.com", "ip": "play.wynncraft.com",
"type": "PC" "type": "PC",
"id": 6
}, },
{ {
"name": "Minehut", "name": "Minehut",
"ip": "minehut.com", "ip": "minehut.com",
"type": "PC" "type": "PC",
"id": 7
}, },
{ {
"name": "Grand Theft Minecraft", "name": "Grand Theft Minecraft",
"ip": "gtm.network", "ip": "gtm.network",
"type": "PC" "type": "PC",
"id": 8
}, },
{ {
"name": "HiveMC", "name": "HiveMC",
"ip": "geo.hivebedrock.network", "ip": "geo.hivebedrock.network",
"type": "PE" "type": "PE",
"id": 9
}, },
{ {
"name": "Purple Prison", "name": "Purple Prison",
"ip": "MCSL.PURPLE.WTF", "ip": "MCSL.PURPLE.WTF",
"type": "PC" "type": "PC",
"id": 10
}, },
{ {
"name": "MinecraftOnline", "name": "MinecraftOnline",
"ip": "minecraftonline.com", "ip": "minecraftonline.com",
"type": "PC" "type": "PC",
"id": 11
} }
] ]

@ -13,30 +13,37 @@ const RECORD_TABLE = "record";
/** /**
* SQL Queries * SQL Queries
*/ */
const CREATE_TABLE = ` const CREATE_PINGS_TABLE = `
CREATE TABLE IF NOT EXISTS {} ( CREATE TABLE IF NOT EXISTS pings (
id INTEGER NOT NULL,
timestamp BIGINT NOT NULL,
ip TINYTEXT NOT NULL,
player_count MEDIUMINT NOT NULL
);
`;
const CREATE_RECORD_TABLE = `
CREATE TABLE IF NOT EXISTS record (
id INTEGER PRIMARY KEY,
timestamp BIGINT NOT NULL, timestamp BIGINT NOT NULL,
ip TINYTEXT NOT NULL, ip TINYTEXT NOT NULL,
player_count MEDIUMINT NOT NULL player_count MEDIUMINT NOT NULL
); );
`; `;
const CREATE_PINGS_TABLE = CREATE_TABLE.replace("{}", PINGS_TABLE);
const CREATE_RECORD_TABLE = CREATE_TABLE.replace("{}", RECORD_TABLE);
const CREATE_PINGS_INDEX = `CREATE INDEX IF NOT EXISTS ip_index ON pings (ip, player_count)`; const CREATE_PINGS_INDEX = `CREATE INDEX IF NOT EXISTS ip_index ON pings (id, ip, player_count)`;
const CREATE_TIMESTAMP_INDEX = `CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)`; const CREATE_TIMESTAMP_INDEX = `CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (id, timestamp)`;
const INSERT_PING = ` const INSERT_PING = `
INSERT INTO ${PINGS_TABLE} (timestamp, ip, player_count) INSERT INTO ${PINGS_TABLE} (id, timestamp, ip, player_count)
VALUES (?, ?, ?) VALUES (?, ?, ?, ?)
`; `;
const INSERT_RECORD = ` const INSERT_RECORD = `
INSERT INTO ${RECORD_TABLE} (timestamp, ip, player_count) INSERT INTO ${RECORD_TABLE} (id, timestamp, ip, player_count)
VALUES (?, ?, ?) VALUES (?, ?, ?, ?)
`; ON CONFLICT(id) DO UPDATE SET
const DELETE_OLD_RECORD = ` timestamp = excluded.timestamp,
DELETE FROM ${RECORD_TABLE} player_count = excluded.player_count,
WHERE ip = ? ip = excluded.ip
`; `;
export default class Scanner { export default class Scanner {
@ -100,32 +107,7 @@ export default class Scanner {
return; // Server is offline return; // Server is offline
} }
const { timestamp, players } = response; this.insertPing(server, response);
this.insertPing(timestamp, server.getIP(), players.online);
this.updateRecord(server, response);
}
/**
* Updates the record for a server.
*
* @param server the server to update
* @param response the response to update with
*/
private updateRecord(server: Server, response: PingResponse): void {
const ip = server.getIP();
// select record from database for this server
const statement = this.db.prepare(
`SELECT * FROM ${RECORD_TABLE} WHERE ip = ?`
);
const record = statement.get(ip);
// delete old record
if (record) {
this.db.prepare(DELETE_OLD_RECORD).run(ip);
}
this.insertRecord(server, response); this.insertRecord(server, response);
} }
@ -136,9 +118,14 @@ export default class Scanner {
* @param ip the IP address of the server * @param ip the IP address of the server
* @param playerCount the number of players online * @param playerCount the number of players online
*/ */
private insertPing(timestamp: number, ip: string, playerCount: number): void { private insertPing(server: Server, response: PingResponse): void {
const { timestamp, players } = response;
const id = server.getID();
const ip = server.getIP();
const onlineCount = players.online;
const statement = this.db.prepare(INSERT_PING); const statement = this.db.prepare(INSERT_PING);
statement.run(timestamp, ip, playerCount); // Insert the ping into the database statement.run(id, timestamp, ip, onlineCount); // Insert the ping into the database
} }
/** /**
@ -149,10 +136,11 @@ export default class Scanner {
*/ */
private insertRecord(server: Server, response: PingResponse): void { private insertRecord(server: Server, response: PingResponse): void {
const { timestamp, players } = response; const { timestamp, players } = response;
const id = server.getID();
const ip = server.getIP(); const ip = server.getIP();
const onlineCount = players.online; const onlineCount = players.online;
const statement = this.db.prepare(INSERT_RECORD); const statement = this.db.prepare(INSERT_RECORD);
statement.run(timestamp, ip, onlineCount); // Insert the record into the database statement.run(id, timestamp, ip, onlineCount); // Insert the record into the database
} }
} }

@ -24,6 +24,7 @@ export type PingResponse = {
}; };
type ServerOptions = { type ServerOptions = {
id: number;
name: string; name: string;
ip: string; ip: string;
type: ServerType; type: ServerType;
@ -35,6 +36,11 @@ type DnsInfo = {
}; };
export default class Server { export default class Server {
/**
* The ID of the server.
*/
private id: number;
/** /**
* The name of the server. * The name of the server.
*/ */
@ -58,7 +64,8 @@ export default class Server {
hasResolved: false, hasResolved: false,
}; };
constructor({ name, ip, type }: ServerOptions) { constructor({ id, name, ip, type }: ServerOptions) {
this.id = id;
this.name = name; this.name = name;
this.ip = ip; this.ip = ip;
this.type = type; this.type = type;
@ -152,6 +159,15 @@ export default class Server {
return undefined; return undefined;
} }
/**
* Returns the ID of the server.
*
* @returns the ID
*/
public getID(): number {
return this.id;
}
/** /**
* Returns the name of the server. * Returns the name of the server.
* *

@ -9,6 +9,7 @@ export default class ServerManager {
for (const server of Servers) { for (const server of Servers) {
this.servers.push( this.servers.push(
new Server({ new Server({
id: server.id,
ip: server.ip, ip: server.ip,
name: server.name, name: server.name,
type: server.type as ServerType, type: server.type as ServerType,