Slight improvments

This commit is contained in:
furo 2021-10-31 20:46:43 +01:00
parent 08b0f6d36c
commit dfb54ba50e
5 changed files with 65 additions and 58 deletions

View File

@ -3,7 +3,6 @@ import { sqlite } from '../db/init.js'
const graphApi = Router() const graphApi = Router()
/* TODO: precalculate this */
graphApi.get( graphApi.get(
'/points', '/points',
(req, res) => { (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 export default graphApi

View File

@ -9,29 +9,22 @@ playerApi.get(
async (req, res) => { async (req, res) => {
let player = req.params.player 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) 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 */ /* Points */
const points = sqlite.prepare(`SELECT rank, points FROM points WHERE name = ?`).get(player) let points = {}
const pointsRank = sqlite.prepare(`SELECT rank, points FROM pointsRank WHERE name = ?`).get(player) const pointsData = sqlite.prepare(`SELECT type, rank, points FROM points WHERE name = ?`)
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)
for (const pointsType of pointsData.iterate(player)) {
points[pointsType.type] = pointsType
}
return res.json({ return res.json({
success: true, success: true,
response: { response: {
firstFinish, firstFinish,
points, points,
pointsRank,
pointsTeam,
pointsThisWeek,
pointsThisMonth,
} }
}) })
} }
@ -39,7 +32,7 @@ playerApi.get(
/* Searching allows you to attach sql LIKE % /* Searching allows you to attach sql LIKE %
* Example to search for players beginning with Test * Example to search for players beginning with Test
* You can do search for Test% * You can do search for %Test
*/ */
playerApi.get( playerApi.get(
'/search', '/search',
@ -51,7 +44,7 @@ playerApi.get(
response: "No query ('host/path?q=query') provided!" response: "No query ('host/path?q=query') provided!"
}) })
} }
1 1
let name = req.query.q let name = req.query.q
/* Set defaults */ /* Set defaults */
@ -70,7 +63,7 @@ playerApi.get(
let pages = Math.floor(amount.amount / limit) let pages = Math.floor(amount.amount / limit)
if (req.query.page) if (req.query.page)
offset = (req.query.page * limit) offset = (req.query.page * limit)
const players = sqlite.prepare( const players = sqlite.prepare(
`SELECT Rank, Name, Points FROM points `SELECT Rank, Name, Points FROM points

View File

@ -101,16 +101,15 @@ export function processAllPoints() {
} }
/* Generate tables */ /* Generate tables */
for(const type in types) { sqlite.exec(
sqlite.exec( `
` CREATE TABLE IF NOT EXISTS "points"
CREATE TABLE IF NOT EXISTS "${type}" (
( "type" varchar(16) NOT NULL,
"rank" INTEGER NOT NULL, "rank" INTEGER NOT NULL,
"name" varchar(16) NOT NULL, "name" varchar(16) NOT NULL,
"points" INTEGER NOT NULL); "points" INTEGER NOT NULL);
`) `)
}
/* Insert data */ /* Insert data */
for(const type in types) { for(const type in types) {
@ -119,21 +118,20 @@ export function processAllPoints() {
for (const entry of types[type]) { for (const entry of types[type]) {
sqlite.prepare( sqlite.prepare(
` `
INSERT INTO "${type}" INSERT INTO "points"
( (
rank, name, points type, rank, name, points
) VALUES (?, ?, ?)`).run( ) VALUES (?, ?, ?, ?)`).run(
rank, entry[0], entry[1]) type, rank, entry[0], entry[1])
++rank ++rank
} }
} }
/* Generate indexes */ /* Generate indexes */
for(const type in types) { sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_points_type" ON "points" ("type")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_${type}_player" ON "${type}" ("Name")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_points_rank" on "points" ("rank")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_${type}_rank" on "${type}" ("Rank")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "Idx_points_name" on "points" ("name")`)
}
} }
export default { export default {

View File

@ -18,8 +18,10 @@ export async function ddnssStart() {
await scrapeServer(`${server}`) await scrapeServer(`${server}`)
} }
else 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) { export function scrapeServer(server) {
@ -78,25 +80,6 @@ export function scrapeServer(server) {
) )
} }
done() 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);
*/

View File

@ -4,6 +4,7 @@ import api from './api/api.js'
import { generateDB } from "./db/generate.js" import { generateDB } from "./db/generate.js"
import { sqlite, dbInit } from "./db/init.js" import { sqlite, dbInit } from "./db/init.js"
import { ddnssStart, scrapeServer } from './ddnss/handler.js' import { ddnssStart, scrapeServer } from './ddnss/handler.js'
//import tasks from './db/tasks.js'
/* Read the .env file */ /* Read the .env file */
dotenv.config() dotenv.config()
@ -18,7 +19,6 @@ const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE typ
if(!exists.a) if(!exists.a)
generateDB() generateDB()
/* Init express */ /* Init express */
const Server = express() const Server = express()
Server.use('/api', api) Server.use('/api', api)