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 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) => {
|
|
|
|
let map = req.params.map
|
|
|
|
|
|
|
|
/* Check if map exists */
|
|
|
|
const check = sqlite.prepare(`SELECT map FROM maps WHERE map = ?`).get(map)
|
|
|
|
if (!check) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "No map found!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const info = sqlite.prepare(`SELECT * FROM maps WHERE map = ?`).get(map)
|
|
|
|
|
|
|
|
/* TODO: Generate a table with this as a cache */
|
2021-11-01 21:52:48 +01:00
|
|
|
const avgTime = sqlite.prepare(`SELECT avg(time) AS 'averageTime' FROM race WHERE map = ?`).get(map)
|
|
|
|
const total = sqlite.prepare(`SELECT COUNT(*) AS 'total' FROM race WHERE map = ?`).get(map)
|
|
|
|
const unique = sqlite.prepare(`SELECT COUNT(distinct(player)) AS 'unique' FROM race WHERE map = ?`).get(map)
|
|
|
|
const teams = sqlite.prepare(`SELECT COUNT(distinct(id)) AS 'teams' FROM teamrace WHERE map = ?`).get(map)
|
2021-10-30 20:26:37 +02:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
response: {
|
2021-11-01 21:52:48 +01:00
|
|
|
info,
|
|
|
|
|
2021-10-30 20:26:37 +02:00
|
|
|
/* TODO Get median time*/
|
|
|
|
averageTime: avgTime.averageTime,
|
|
|
|
finishes: {
|
|
|
|
unique: unique.unique,
|
|
|
|
total: total.total,
|
|
|
|
teams: teams.teams,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
mapApi.get(
|
|
|
|
'/getAll',
|
|
|
|
(req, res) => {
|
2021-11-01 21:52:48 +01:00
|
|
|
const allMaps = sqlite.prepare(`SELECT * FROM maps`).all()
|
2021-10-30 20:26:37 +02:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
response: allMaps,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
mapApi.get(
|
|
|
|
'/category/:category',
|
|
|
|
(req, res) => {
|
|
|
|
let category = req.params.category
|
|
|
|
|
|
|
|
/* Check if category exists */
|
2021-11-01 22:49:52 +01:00
|
|
|
const check = sqlite.prepare(`SELECT category FROM maps WHERE category = ? LIMIT 1`).get(category)
|
2021-10-30 20:26:37 +02:00
|
|
|
if (!check) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "Invalid category name!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:52:48 +01:00
|
|
|
const allMaps = sqlite.prepare(`SELECT * FROM maps WHERE category = ?`).all(category)
|
2021-10-30 20:26:37 +02:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
response: allMaps,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|