Slight improvments
This commit is contained in:
parent
08b0f6d36c
commit
dfb54ba50e
35
api/graph.js
35
api/graph.js
|
@ -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
|
|
@ -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)
|
for (const pointsType of pointsData.iterate(player)) {
|
||||||
const pointsThisMonth = sqlite.prepare(`SELECT rank, points FROM pointsThisMonth WHERE name = ?`).get(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',
|
||||||
|
|
20
db/tasks.js
20
db/tasks.js
|
@ -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 "${type}"
|
CREATE TABLE IF NOT EXISTS "points"
|
||||||
(
|
(
|
||||||
|
"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 {
|
||||||
|
|
|
@ -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);
|
|
||||||
*/
|
|
||||||
|
|
2
index.js
2
index.js
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user