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()
/* 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

View File

@ -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

View File

@ -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 {

View File

@ -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);
*/
}

View File

@ -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)