2021-10-30 20:26:37 +02:00
|
|
|
import { Router } from 'express'
|
2021-10-31 23:43:36 +01:00
|
|
|
import { sqlite } from '../libs/database/init.js'
|
2021-10-30 20:26:37 +02:00
|
|
|
|
|
|
|
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(
|
|
|
|
`
|
2021-11-01 21:52:48 +01:00
|
|
|
SELECT DISTINCT(a.map), a.date, b.points
|
2021-10-30 20:26:37 +02:00
|
|
|
FROM race AS a
|
|
|
|
INNER JOIN maps AS b
|
|
|
|
ON a.map = b.map
|
2021-11-01 21:52:48 +01:00
|
|
|
WHERE a.player = ?
|
2021-10-30 20:26:37 +02:00
|
|
|
GROUP BY a.map
|
2021-11-01 21:52:48 +01:00
|
|
|
ORDER BY a.date;
|
2021-10-30 20:26:37 +02:00
|
|
|
`)
|
2021-11-01 22:49:52 +01:00
|
|
|
let currentPoints = 0
|
2021-10-30 20:26:37 +02:00
|
|
|
let array = []
|
2021-11-01 22:49:52 +01:00
|
|
|
|
2021-10-30 20:26:37 +02:00
|
|
|
for (const finish of finishes.iterate(player)) {
|
2021-11-01 21:52:48 +01:00
|
|
|
currentPoints += finish.points
|
|
|
|
array.push({ t: new Date(finish.date), y: currentPoints })
|
2021-10-30 20:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
response: array,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-31 20:46:43 +01:00
|
|
|
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!"
|
|
|
|
})
|
|
|
|
}
|
2021-11-01 21:52:48 +01:00
|
|
|
const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`)
|
2021-10-31 20:46:43 +01:00
|
|
|
|
|
|
|
let array = []
|
2021-11-01 15:09:03 +01:00
|
|
|
for (const record of finishes.iterate(req.query.q))
|
2021-11-01 21:52:48 +01:00
|
|
|
array.push({ t: new Date(record.date), y: record.time, player: record.player})
|
2021-10-31 20:46:43 +01:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
response: array,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-30 20:26:37 +02:00
|
|
|
export default graphApi
|