import { Router } from 'express' import { sqlite } from '../libs/database/init.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) => { 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 */ 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) return res.json({ success: true, response: { info, /* TODO Get median time*/ averageTime: avgTime.averageTime, finishes: { unique: unique.unique, total: total.total, teams: teams.teams, } } }) } ) mapApi.get( '/getAll', (req, res) => { const allMaps = sqlite.prepare(`SELECT * FROM maps`).all() return res.json({ success: true, response: allMaps, }) } ) mapApi.get( '/category/:category', (req, res) => { let category = req.params.category /* Check if category exists */ const check = sqlite.prepare(`SELECT category FROM maps WHERE category = ? LIMIT 1`).get(category) if (!check) { return res.json({ success: false, response: "Invalid category name!", }) } const allMaps = sqlite.prepare(`SELECT * FROM maps WHERE category = ?`).all(category) return res.json({ success: true, response: allMaps, }) } ) 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