ddstats-server/api/maps.js

116 lines
2.5 KiB
JavaScript
Raw Normal View History

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'
import wrapper, { map } from '../libs/database/wrapper.js'
2021-10-30 20:26:37 +02:00
const mapApi = Router()
mapApi.get(
'/count',
(req, res) => {
2021-11-01 21:52:48 +01:00
const totalMaps = sqlite.prepare(`SELECT COUNT(*) as count FROM maps`).get()
2021-10-30 20:26:37 +02:00
return res.json({
success: true,
2021-11-01 21:52:48 +01:00
response: totalMaps.count,
2021-10-30 20:26:37 +02:00
})
}
)
mapApi.get(
'/get/:map',
(req, res) => {
/* Check if map exists */
if(!wrapper.mapExists(req.params.map)) {
2021-10-30 20:26:37 +02:00
return res.json({
success: false,
response: "No such map!"
2021-10-30 20:26:37 +02:00
})
}
return res.json({
success: true,
response: map(req.params.map)
2021-10-30 20:26:37 +02:00
})
}
)
mapApi.get(
'/getAll',
(req, res) => {
return res.json({
success: true,
response: wrapper.allMaps()
2021-10-30 20:26:37 +02:00
})
}
)
mapApi.get(
'/category/:category',
(req, res) => {
/* Check if category exists */
if (!wrapper.categoryExists(req.params.category)) {
2021-10-30 20:26:37 +02:00
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!",
})
}
2021-10-30 20:26:37 +02:00
return res.json({
success: true,
response: wrapper.leaderboardTeamrace(req.params.map, 1, 20)
2021-10-30 20:26:37 +02:00
})
}
)
mapApi.get(
'/search',
async (req, res) => {
/* Check if a query was provided */
2021-11-01 21:52:48 +01:00
if (!req.query.q) {
2021-10-30 20:26:37 +02:00
return res.json({
success: false,
response: "No query ('host/path?q=query') provided!"
})
}
2021-11-01 21:52:48 +01:00
/* TODO: Use the searcher function */
2021-10-30 20:26:37 +02:00
}
)
export default mapApi