From becbce9273ff330fcaa703e44bfe4c5f5ccbd6dc Mon Sep 17 00:00:00 2001 From: furo Date: Sat, 13 Nov 2021 02:28:43 +0100 Subject: [PATCH] Really cool changes (I promise) --- index.js | 3 +- libs/database/wrapper.js | 59 ++++++++++++++++++++++++++++------------ routes/routes.js | 3 +- views/pages/maps.njk | 2 +- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 6757bd4..7efccb5 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ import { dbInit } from './libs/database/init.js' import { getStats, setStat } from './libs/serverStats.js' import { downloadEssentialData } from './libs/download/dowload.js' import { ddnssStart } from './libs/ddnss/handler.js' +import { searchMap } from './libs/database/wrapper.js' const start = Date.now() const log = initLog("[ MAIN ]") @@ -52,4 +53,4 @@ log(`Took ${elapsed/1000} seconds to start the server!`) Server.listen(process.env.PORT ?? 12345, () => log(`Server started and listening on port ${process.env.PORT}.`)) -//ddnssStart() \ No newline at end of file +console.log(searchMap("", ["Solo"], [0,1,2,3,4,5], "points", "DESC", 1)) \ No newline at end of file diff --git a/libs/database/wrapper.js b/libs/database/wrapper.js index d148613..01cef8c 100644 --- a/libs/database/wrapper.js +++ b/libs/database/wrapper.js @@ -1,6 +1,8 @@ import { sqlite, skinDB } from './init.js' import { simpleSanitize } from './searcher.js' +const entriesPerPage = process.env.ENTRIES_PER_PAGE ?? 50 + /** * This function returns all data pertaining a certain player * @@ -57,14 +59,11 @@ export function map(map) { * @returns {array} An array contaning all map objects */ export function allMaps() { - let output = [] - const maps = sqlite.prepare(` - SELECT * FROM maps`).all() - - for(const map of maps) { - output.push(prettyifyMap(map)) - } - return output + // This is kinda dumb + return searchMap( + "", + ["Novice", "Moderate", "Brutal" ,"Insane", "Dummy", "DDmaX", "Oldschool", "Solo", "Race", "Fun"], + [0,1,2,3,4,5], "release", "desc", 1) } /** @@ -183,7 +182,7 @@ export function leaderboardPoints(type, region, start, end) { } let rank = 1 for (const entry of leaderboard) { - let flag = skinDB.prepare(`SELECT flag FROM skindata WHERE player = ?`).get(entry.player)?.flag ?? "default" + let flag = skinDB.prepare(`SELECT COUNT(flag) as a, flag FROM skindata WHERE player = ? GROUP BY flag ORDER BY a DESC`).get(entry.player)?.flag ?? "default" output.push({ rank: rank, global: entry.rank, player: entry.player, points: entry.points, region: entry.region, flag: flag }) ++rank @@ -291,16 +290,28 @@ export function finishedMaps(player) { return unfinished } -export function graphMap(map) { - const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`) +export function graphMap(map, player) { + let finishes + + if(player) + finishes = sqlite.prepare(`SELECT * FROM race WHERE map = ? AND player = ? ORDER BY date`).all(map, player) + else + finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`).all(map) if(!finishes) return undefined let array = [] - for (const record of finishes.iterate(map)) - array.push({ t: record.date, y: record.time, player: record.player}) + let currentFinish + let currentBest = 0 + for (const record of finishes) { + currentFinish = record.time + if (currentFinish <= currentBest || currentBest == 0) { + currentBest = currentFinish + array.push({ t: record.date, y: record.time, player: record.player}) + } + } return array } /** @@ -314,23 +325,37 @@ export function graphMap(map) { * * @returns {array} Returns an array of all maps found */ -export function searchMap(query, categories, stars, sortBy, order) { +export function searchMap(query, categories, stars, sortBy, order, page) { const categoriesJoined = categories.join(',') const starsJoined = stars.join(',') let output = [] + // TODO: Would it be possible to NOT duplicate this? + const mapCount = sqlite.prepare(` + SELECT COUNT(*) as a FROM maps WHERE map LIKE '%${query}%' + AND ',' || ? || ',' LIKE '%,' || category || ',%' + AND ',' || ? || ',' LIKE '%,' || stars || ',%'`) + .get(categoriesJoined, starsJoined).a + const maps = sqlite.prepare(` SELECT * FROM maps WHERE map LIKE '%${query}%' AND ',' || ? || ',' LIKE '%,' || category || ',%' AND ',' || ? || ',' LIKE '%,' || stars || ',%' - ORDER BY ? ${simpleSanitize(order)}`) - .all(categoriesJoined, starsJoined, sortBy) + ORDER BY ${simpleSanitize(sortBy)} ${simpleSanitize(order)} LIMIT ${entriesPerPage * (page - 1)}, ${entriesPerPage}`) + .all(categoriesJoined, starsJoined) + + const totalPages = Math.ceil(mapCount/50) + const pageInfo = { page, mapCount: mapCount, totalPages } for(const map of maps) { output.push(prettyifyMap(map)) } - return output + console.log(entriesPerPage * (page - 1)) + console.log(entriesPerPage * (page)) + console.log(maps.length) + + return { "pageInfo": pageInfo, "maps": output } } export function mapToRegion(srv) { diff --git a/routes/routes.js b/routes/routes.js index 440aa5a..2c80b36 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -20,8 +20,9 @@ routes.get( const stars = req.query.stars ? req.query.stars : "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 ? req.query.page : "1" - const maps = wrapper.searchMap(map, categories.split(","), stars.split(","), sort, order) + const maps = wrapper.searchMap(map, categories.split(","), stars.split(","), sort, order, page) tx(req, res)('pages/maps.njk', { maps }, true, { currentSection: "maps" }) } diff --git a/views/pages/maps.njk b/views/pages/maps.njk index e29fcf7..1cc7be2 100644 --- a/views/pages/maps.njk +++ b/views/pages/maps.njk @@ -28,7 +28,7 @@ - {% for map in data.maps %} + {% for map in data.maps.maps %} {{ mapComponent.card(map) }} {% endfor %}