Implemented a way to easily respond render/json.

This commit is contained in:
BurnyLlama 2021-11-04 21:33:42 +01:00
parent 355c0ab670
commit 76d9ad836d
9 changed files with 18 additions and 329 deletions

View File

@ -1,26 +0,0 @@
import { Router } from 'express'
import playerApi from './players.js'
import mapApi from './maps.js'
import finishesApi from './finishes.js'
import graphApi from './graph.js'
const api = Router()
api.get(
'/',
(req, res) => res.json({
success: true,
response: "You connected to DDStats API! :D"
})
)
api.use('/players', playerApi)
api.use('/maps', mapApi)
api.use('/finishes', finishesApi)
api.use('/graph', graphApi)
/**
* This module is the entrypoint for the API.
* @module api/api
*/
export default api

View File

@ -1,59 +0,0 @@
import { Router } from 'express'
import { sqlite } from '../libs/database/init.js'
import wrapper from '../libs/database/wrapper.js'
const finishApi = Router()
/* TODO: precalculate this */
finishApi.get(
'/count',
(req, res) => {
const finishes = sqlite.prepare(`SELECT COUNT(*) as count FROM race`).get()
return res.json({
success: true,
response: finishes.count,
})
}
)
finishApi.get(
'/finishedMaps/:player',
(req, res) => {
/* Check if player exists */
if(!wrapper.playerExists(req.params.player)) {
return res.json({
success: false,
response: "No such player!"
})
}
const finishes = wrapper.finishedMaps(req.params.player)
return res.json({
success: true,
response: finishes,
})
}
)
finishApi.get(
'/unfinishedMaps/:player',
(req, res) => {
/* Check if player exists */
if(!wrapper.playerExists(req.params.player)) {
return res.json({
success: false,
response: "No such player!"
})
}
const finishes = wrapper.unfinishedMaps(req.params.player)
return res.json({
success: true,
response: finishes,
})
}
)
export default finishApi

View File

@ -1,66 +0,0 @@
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

View File

@ -1,115 +0,0 @@
import { Router } from 'express'
import { sqlite } from '../libs/database/init.js'
import wrapper, { map } from '../libs/database/wrapper.js'
const mapApi = Router()
mapApi.get(
'/count',
(req, res) => {
const totalMaps = sqlite.prepare(`SELECT COUNT(*) as count FROM maps`).get()
return res.json({
success: true,
response: totalMaps.count,
})
}
)
mapApi.get(
'/get/:map',
(req, res) => {
/* Check if map exists */
if(!wrapper.mapExists(req.params.map)) {
return res.json({
success: false,
response: "No such map!"
})
}
return res.json({
success: true,
response: map(req.params.map)
})
}
)
mapApi.get(
'/getAll',
(req, res) => {
return res.json({
success: true,
response: wrapper.allMaps()
})
}
)
mapApi.get(
'/category/:category',
(req, res) => {
/* Check if category exists */
if (!wrapper.categoryExists(req.params.category)) {
return res.json({
success: false,
response: "Invalid category name!",
})
}
return res.json({
success: true,
response: wrapper.mapCategory(req.params.category)
})
}
)
mapApi.get(
'/leaderboard/race/:map',
(req, res) => {
/* Check if map exists */
if (!wrapper.mapExists(req.params.map)) {
return res.json({
success: false,
response: "No such map!",
})
}
return res.json({
success: true,
response: wrapper.leaderboardRace(req.params.map, 1, 20)
})
}
)
mapApi.get(
'/leaderboard/teamrace/:map',
(req, res) => {
/* Check if map exists */
if (!wrapper.mapExists(req.params.map)) {
return res.json({
success: false,
response: "No such map!",
})
}
return res.json({
success: true,
response: wrapper.leaderboardTeamrace(req.params.map, 1, 20)
})
}
)
mapApi.get(
'/search',
async (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!"
})
}
/* TODO: Use the searcher function */
}
)
export default mapApi

View File

@ -1,57 +0,0 @@
import { Router } from 'express'
import wrapper from '../libs/database/wrapper.js'
const playerApi = Router()
playerApi.get(
'/get/:player',
async (req, res) => {
/* Check if player exists */
if(!wrapper.playerExists(req.params.player)) {
return res.json({
success: false,
response: "No such player!"
})
}
const data = wrapper.player(req.params.player)
return res.json({
success: true,
response: data
})
}
)
playerApi.get(
'/search',
async (req, res) => {
if (!req.query.q) {
return res.json({
success: false,
response: "No query ('?q=query') provided!"
})
}
searcher(
'points',
'player',
`%${req.query.q}%`,
req.query.sort ?? undefined,
req.query.order === "asc",
"all",
req.query.page
).then(
player => res.json({
success: true,
response: player
})
).catch(
error => res.json({
success: false,
response: error
})
)
}
)
export default playerApi

View File

@ -10,7 +10,6 @@ import njk from 'nunjucks'
import initLog from './libs/utils/log.js'
import routes from './routes/routes.js'
import api from './api/api.js'
import { generateDB } from './libs/database/generate.js'
import { dbInit } from './libs/database/init.js'
@ -40,7 +39,7 @@ njk.configure(
)
Server.use('/', routes)
Server.use('/api', api)
Server.use('/api', routes)
Server.use('/assets', express.static('static'))
Server.listen(process.env.PORT ?? 12345, () => log(`Server started and listening on port ${process.env.PORT}.`))

10
libs/routex.js Normal file
View File

@ -0,0 +1,10 @@
export default function routex(req, res) {
if (req.baseUrl === "/api")
return function(_, data, _2) {
res.json(data)
}
return function(template, data, options) {
res.render(template, { data, options })
}
}

View File

@ -1,10 +1,13 @@
import { Router } from 'express'
import routex from '../libs/routex.js'
const routes = Router()
routes.get(
'/',
(req, res) => res.render('pages/landing.njk', {currentSection: "maps"})
(req, res) => {
routex(req, res)('pages/landing.njk', null, { currentSection: null })
}
)
export default routes

View File

@ -1,7 +1,7 @@
<nav>
<div class="logo"></div>
<a href="/maps" class="{{ "current" if currentSection == "maps" }}">Maps</a>
<a href="/leaderboards" class="{{ "current" if currentSection == "leaderboards" }}">Leaderboards</a>
<a href="/tournaments" class="{{ "current" if currentSection == "tournaments" }}">Tournaments</a>
<a href="/maps" class="{{ "current" if options.currentSection == "maps" }}">Maps</a>
<a href="/leaderboards" class="{{ "current" if options.currentSection == "leaderboards" }}">Leaderboards</a>
<a href="/tournaments" class="{{ "current" if options.currentSection == "tournaments" }}">Tournaments</a>
</nav>