diff --git a/data/servers.json b/data/servers.json index 2ff32bc..a2e84c0 100644 --- a/data/servers.json +++ b/data/servers.json @@ -2,61 +2,73 @@ { "name": "WildPrison", "ip": "wildprison.net", - "type": "PC" + "type": "PC", + "id": 0 }, { "name": "Hypixel", "ip": "mc.hypixel.net", - "type": "PC" + "type": "PC", + "id": 1 }, { "name": "CubeCraft", "ip": "play.cubecraft.net", - "type": "PC" + "type": "PC", + "id": 2 }, { "name": "Mineplex", "ip": "mineplex.com", - "type": "PC" + "type": "PC", + "id": 3 }, { "name": "2b2t", "ip": "2b2t.org", - "type": "PC" + "type": "PC", + "id": 4 }, { "name": "AkumaMC", "ip": "akumamc.net", - "type": "PC" + "type": "PC", + "id": 5 }, { "name": "Wynncraft", "ip": "play.wynncraft.com", - "type": "PC" + "type": "PC", + "id": 6 }, { "name": "Minehut", "ip": "minehut.com", - "type": "PC" + "type": "PC", + "id": 7 }, { "name": "Grand Theft Minecraft", "ip": "gtm.network", - "type": "PC" + "type": "PC", + "id": 8 }, { "name": "HiveMC", "ip": "geo.hivebedrock.network", - "type": "PE" + "type": "PE", + "id": 9 }, { "name": "Purple Prison", "ip": "MCSL.PURPLE.WTF", - "type": "PC" + "type": "PC", + "id": 10 }, { "name": "MinecraftOnline", "ip": "minecraftonline.com", - "type": "PC" + "type": "PC", + "id": 11 } ] diff --git a/src/scanner/scanner.ts b/src/scanner/scanner.ts index c190f7f..0f429f0 100644 --- a/src/scanner/scanner.ts +++ b/src/scanner/scanner.ts @@ -13,30 +13,37 @@ const RECORD_TABLE = "record"; /** * SQL Queries */ -const CREATE_TABLE = ` - CREATE TABLE IF NOT EXISTS {} ( +const CREATE_PINGS_TABLE = ` + 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, ip TINYTEXT 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_TIMESTAMP_INDEX = `CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)`; +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 (id, timestamp)`; const INSERT_PING = ` - INSERT INTO ${PINGS_TABLE} (timestamp, ip, player_count) - VALUES (?, ?, ?) + INSERT INTO ${PINGS_TABLE} (id, timestamp, ip, player_count) + VALUES (?, ?, ?, ?) `; const INSERT_RECORD = ` - INSERT INTO ${RECORD_TABLE} (timestamp, ip, player_count) - VALUES (?, ?, ?) -`; -const DELETE_OLD_RECORD = ` - DELETE FROM ${RECORD_TABLE} - WHERE ip = ? + INSERT INTO ${RECORD_TABLE} (id, timestamp, ip, player_count) + VALUES (?, ?, ?, ?) + ON CONFLICT(id) DO UPDATE SET + timestamp = excluded.timestamp, + player_count = excluded.player_count, + ip = excluded.ip `; export default class Scanner { @@ -100,32 +107,7 @@ export default class Scanner { return; // Server is offline } - const { timestamp, players } = 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.insertPing(server, response); this.insertRecord(server, response); } @@ -136,9 +118,14 @@ export default class Scanner { * @param ip the IP address of the server * @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); - 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 { const { timestamp, players } = response; + const id = server.getID(); const ip = server.getIP(); const onlineCount = players.online; 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 } } diff --git a/src/server/server.ts b/src/server/server.ts index 1bcd189..0bf58b5 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -24,6 +24,7 @@ export type PingResponse = { }; type ServerOptions = { + id: number; name: string; ip: string; type: ServerType; @@ -35,6 +36,11 @@ type DnsInfo = { }; export default class Server { + /** + * The ID of the server. + */ + private id: number; + /** * The name of the server. */ @@ -58,7 +64,8 @@ export default class Server { hasResolved: false, }; - constructor({ name, ip, type }: ServerOptions) { + constructor({ id, name, ip, type }: ServerOptions) { + this.id = id; this.name = name; this.ip = ip; this.type = type; @@ -152,6 +159,15 @@ export default class Server { return undefined; } + /** + * Returns the ID of the server. + * + * @returns the ID + */ + public getID(): number { + return this.id; + } + /** * Returns the name of the server. * diff --git a/src/server/serverManager.ts b/src/server/serverManager.ts index 8c434b1..6a07202 100644 --- a/src/server/serverManager.ts +++ b/src/server/serverManager.ts @@ -9,6 +9,7 @@ export default class ServerManager { for (const server of Servers) { this.servers.push( new Server({ + id: server.id, ip: server.ip, name: server.name, type: server.type as ServerType,