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-11-04 15:40:12 +01:00
|
|
|
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 */
|
2021-11-04 15:40:12 +01:00
|
|
|
if(!wrapper.mapExists(req.params.map)) {
|
2021-10-30 20:26:37 +02:00
|
|
|
return res.json({
|
|
|
|
success: false,
|
2021-11-04 15:40:12 +01:00
|
|
|
response: "No such map!"
|
2021-10-30 20:26:37 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
2021-11-04 15:40:12 +01:00
|
|
|
response: map(req.params.map)
|
2021-10-30 20:26:37 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
mapApi.get(
|
|
|
|
'/getAll',
|
|
|
|
(req, res) => {
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
2021-11-04 15:40:12 +01:00
|
|
|
response: wrapper.allMaps()
|
2021-10-30 20:26:37 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
mapApi.get(
|
|
|
|
'/category/:category',
|
|
|
|
(req, res) => {
|
|
|
|
/* Check if category exists */
|
2021-11-04 15:40:12 +01:00
|
|
|
if (!wrapper.categoryExists(req.params.category)) {
|
2021-10-30 20:26:37 +02:00
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "Invalid category name!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-04 15:40:12 +01:00
|
|
|
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,
|
2021-11-04 15:40:12 +01:00
|
|
|
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
|