import { Router } from 'express' import wrapper from '../libs/database/wrapper.js' import tx from '../libs/routex.js' import { getStats } from '../libs/serverStats.js' const routes = Router() routes.get( '/', (req, res) => { tx(req, res)('pages/landing.njk', null, { currentSection: null }) } ) routes.get( '/maps', (req, res) => { const map = req.query.map ?? "" const categories = req.query.categories ? req.query.categories : "Novice,Moderate,Brutal,Insane,Dummy,DDmaX,Oldschool,Solo,Race,Fun" const stars = req.query.stars ? req.query.stars : "0,1,2,3,4,5" const sort = req.query.sortBy ? req.query.sortBy : "map" const order = req.query.order === "desc" ? "desc" : "asc" const page = req.query.page ? parseInt(req.query.page) : 1 const maps = wrapper.searchMap(map, categories.split(","), stars.split(","), sort, order, page) tx(req, res)('pages/maps.njk', { maps }, true, { currentSection: "maps", query: req.query }) } ) routes.get( '/maps/:map', (req, res) => { const map = wrapper.map(req.params.map) const graphMap = wrapper.graphMap(req.params.map) const raceLeaderboard = wrapper.leaderboardRace(req.params.map, 1, 10) const teamLeaderboard = wrapper.leaderboardTeamrace(req.params.map, 1, 10) const [success, error] = map ? [true, "No error!"] : [false, "Map not found!"] tx(req, res)('pages/mapSingle.njk', { map, graphMap, raceLeaderboard, teamLeaderboard, error }, success, { currentSection: "maps" }) } ) routes.get( '/leaderboards', (req, res) => { const region = "Global" const leaderboards = { points: wrapper.leaderboardPoints("points", region, 1, 10), pointsRank: wrapper.leaderboardPoints("pointsRank", region, 1, 10), pointsTeam: wrapper.leaderboardPoints("pointsTeam", region, 1, 10), pointsThisWeek: wrapper.leaderboardPoints("pointsThisWeek", region, 1, 10), pointsThisMonth: wrapper.leaderboardPoints("pointsThisMonth", region, 1, 10) } tx(req, res)('pages/leaderboards.njk', { leaderboards }, true, { currentSection: "leaderboards" }) } ) routes.get( '/leaderboards/:region', (req, res) => { const region = req.params.region.match(/^(Global|Europe|Asia|SA|NA|Africa|ME|OLD|Other)$/) ? req.params.region : "Global" const leaderboards = { points: wrapper.leaderboardPoints("points", region, 1, 10), pointsRank: wrapper.leaderboardPoints("pointsRank", region, 1, 10), pointsTeam: wrapper.leaderboardPoints("pointsTeam", region, 1, 10), pointsThisWeek: wrapper.leaderboardPoints("pointsThisWeek", region, 1, 10), pointsThisMonth: wrapper.leaderboardPoints("pointsThisMonth", region, 1, 10) } tx(req, res)('pages/leaderboards.njk', { leaderboards, region }, true, { currentSection: "leaderboards" }) } ) routes.get( '/players/:player', (req, res) => { const player = wrapper.player(req.params.player) const finishedMaps = wrapper.finishedMaps(req.params.player) const unfinishedMaps = wrapper.unfinishedMaps(req.params.player) const [success, error] = player ? [true, null] : [false, `Player ${req.params.player} does not exist!`] tx(req, res)('pages/playerProfile.njk', { player, finishedMaps, unfinishedMaps, error }, success, { currentSection: "players" }) } ) routes.get( '/status', (req, res) => { const stats = getStats() tx(req, res)('pages/stats.njk', { stats }, true, { currentSection: null }) } ) routes.get( '/credits', (req, res) => { tx(req, res)('pages/credits.njk', {}, true, { currentSection: null }) } ) routes.get( '/api-reference', (req, res) => { tx(req, res)('pages/reference.njk', {}, true, { currentSection: null }) } ) routes.get( '/changelog', (req, res) => { tx(req, res)('pages/changelogs.njk', {}, true, { currentSection: null }) } ) export default routes