ddstats-server/api/graph.js
2021-11-01 22:49:52 +01:00

66 lines
1.7 KiB
JavaScript

import { Router } from 'express'
import { sqlite } from '../libs/database/init.js'
const graphApi = Router()
graphApi.get(
'/points',
(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 player = req.query.q
const finishes = sqlite.prepare(
`
SELECT DISTINCT(a.map), a.date, b.points
FROM race AS a
INNER JOIN maps AS b
ON a.map = b.map
WHERE a.player = ?
GROUP BY a.map
ORDER BY a.date;
`)
let currentPoints = 0
let array = []
for (const finish of finishes.iterate(player)) {
currentPoints += finish.points
array.push({ t: new Date(finish.date), y: currentPoints })
}
return res.json({
success: true,
response: array,
})
}
)
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!"
})
}
const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`)
let array = []
for (const record of finishes.iterate(req.query.q))
array.push({ t: new Date(record.date), y: record.time, player: record.player})
return res.json({
success: true,
response: array,
})
}
)
export default graphApi