From dfb54ba50ef6632c74147449000bf2e8a132ea65 Mon Sep 17 00:00:00 2001 From: furo Date: Sun, 31 Oct 2021 20:46:43 +0100 Subject: [PATCH] Slight improvments --- api/graph.js | 35 ++++++++++++++++++++++++++++++++++- api/players.js | 27 ++++++++++----------------- db/tasks.js | 34 ++++++++++++++++------------------ ddnss/handler.js | 25 ++++--------------------- index.js | 2 +- 5 files changed, 65 insertions(+), 58 deletions(-) diff --git a/api/graph.js b/api/graph.js index e5251c6..e19307a 100644 --- a/api/graph.js +++ b/api/graph.js @@ -3,7 +3,6 @@ import { sqlite } from '../db/init.js' const graphApi = Router() -/* TODO: precalculate this */ graphApi.get( '/points', (req, res) => { @@ -43,4 +42,38 @@ graphApi.get( } ) +graphApi.get( + '/map', + (req, res) => { + /* Check if a query was provided */ + if (!req.query.q) { + return res.json({ + success: false, + response: "No query ('host/path?q=query') provided!" + }) + } + let map = req.query.q + + const finishes = sqlite.prepare( + ` + SELECT * FROM race WHERE map = ? ORDER BY Timestamp; + `) + let currentFinish + let currentBest = 0; + let array = [] + for (const record of finishes.iterate(map)) { + currentFinish = record.Time + if(currentFinish <= currentBest || currentBest == 0) { + currentBest = currentFinish + array.push({ player: record.Name, Time: record.Time, Date: new Date(record.Timestamp) }) + } + } + + return res.json({ + success: true, + response: array, + }) + } +) + export default graphApi \ No newline at end of file diff --git a/api/players.js b/api/players.js index 723caaf..942f656 100644 --- a/api/players.js +++ b/api/players.js @@ -9,29 +9,22 @@ playerApi.get( async (req, res) => { let player = req.params.player - /* Misc */ + /* Misc, may be worth to cache this? */ const firstFinish = sqlite.prepare(`SELECT server as server, map as map, time as time, Timestamp as date FROM race WHERE name = ? ORDER BY Timestamp ASC LIMIT 1`).get(player) - /* TODO, make points a single table. Would be alot more efficent but longer cache creation time */ /* Points */ - const points = sqlite.prepare(`SELECT rank, points FROM points WHERE name = ?`).get(player) - const pointsRank = sqlite.prepare(`SELECT rank, points FROM pointsRank WHERE name = ?`).get(player) - const pointsTeam = sqlite.prepare(`SELECT rank, points FROM pointsTeam WHERE name = ?`).get(player) - - const pointsThisWeek = sqlite.prepare(`SELECT rank, points FROM pointsThisWeek WHERE name = ?`).get(player) - const pointsThisMonth = sqlite.prepare(`SELECT rank, points FROM pointsThisMonth WHERE name = ?`).get(player) + let points = {} + const pointsData = sqlite.prepare(`SELECT type, rank, points FROM points WHERE name = ?`) + for (const pointsType of pointsData.iterate(player)) { + points[pointsType.type] = pointsType + } + return res.json({ success: true, response: { firstFinish, - points, - pointsRank, - pointsTeam, - - pointsThisWeek, - pointsThisMonth, } }) } @@ -39,7 +32,7 @@ playerApi.get( /* Searching allows you to attach sql LIKE % * Example to search for players beginning with Test - * You can do search for Test% + * You can do search for %Test */ playerApi.get( '/search', @@ -51,7 +44,7 @@ playerApi.get( response: "No query ('host/path?q=query') provided!" }) } -1 + 1 let name = req.query.q /* Set defaults */ @@ -70,7 +63,7 @@ playerApi.get( let pages = Math.floor(amount.amount / limit) if (req.query.page) - offset = (req.query.page * limit) + offset = (req.query.page * limit) const players = sqlite.prepare( `SELECT Rank, Name, Points FROM points diff --git a/db/tasks.js b/db/tasks.js index 91c188e..ef3ca8e 100644 --- a/db/tasks.js +++ b/db/tasks.js @@ -101,16 +101,15 @@ export function processAllPoints() { } /* Generate tables */ - for(const type in types) { - sqlite.exec( - ` - CREATE TABLE IF NOT EXISTS "${type}" - ( - "rank" INTEGER NOT NULL, - "name" varchar(16) NOT NULL, - "points" INTEGER NOT NULL); - `) - } + sqlite.exec( + ` + CREATE TABLE IF NOT EXISTS "points" + ( + "type" varchar(16) NOT NULL, + "rank" INTEGER NOT NULL, + "name" varchar(16) NOT NULL, + "points" INTEGER NOT NULL); + `) /* Insert data */ for(const type in types) { @@ -119,21 +118,20 @@ export function processAllPoints() { for (const entry of types[type]) { sqlite.prepare( ` - INSERT INTO "${type}" + INSERT INTO "points" ( - rank, name, points - ) VALUES (?, ?, ?)`).run( - rank, entry[0], entry[1]) + type, rank, name, points + ) VALUES (?, ?, ?, ?)`).run( + type, rank, entry[0], entry[1]) ++rank } } /* Generate indexes */ - for(const type in types) { - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_${type}_player" ON "${type}" ("Name")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_${type}_rank" on "${type}" ("Rank")`) - } + sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_points_type" ON "points" ("type")`) + sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_points_rank" on "points" ("rank")`) + sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_points_name" on "points" ("name")`) } export default { diff --git a/ddnss/handler.js b/ddnss/handler.js index 5192653..3f30ab1 100644 --- a/ddnss/handler.js +++ b/ddnss/handler.js @@ -18,8 +18,10 @@ export async function ddnssStart() { await scrapeServer(`${server}`) } else - console.log(`Server full: ${servers.ip}:${servers.port}`) + console.log(`${servers.num_clients}/63 Server full: ${servers.ip}:${servers.port}`) } + /* A bit hacky way of killing ddnss */ + exec(`pkill -9 -f ddnss`) } export function scrapeServer(server) { @@ -78,25 +80,6 @@ export function scrapeServer(server) { ) } done() - - /* A bit hacky way of killing ddnss */ - //exec(`pkill -9 -f ddnss`) - }) }) -} - -/* -CREATE TABLE IF NOT EXISTS "skindata" -( - "timestamp" INTEGER NOT NULL, - "player" varchar(16) NOT NULL, - "clan" varchar(12) NOT NULL, - "flag" INTEGER NOT NULL, - "skin" varchar(16) NOT NULL, - "useColor" INTEGER NOT NULL, - "colorBodyRaw" INTEGER NOT NULL, - "colorBodyHex" varchar(8) NOT NULL, - "colorFeetRaw" INTEGER NOT NULL, - "colorFeetHex" varchar(8) NOT NULL); -*/ +} \ No newline at end of file diff --git a/index.js b/index.js index f857491..a5e3ed1 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ import api from './api/api.js' import { generateDB } from "./db/generate.js" import { sqlite, dbInit } from "./db/init.js" import { ddnssStart, scrapeServer } from './ddnss/handler.js' +//import tasks from './db/tasks.js' /* Read the .env file */ dotenv.config() @@ -18,7 +19,6 @@ const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE typ if(!exists.a) generateDB() - /* Init express */ const Server = express() Server.use('/api', api)