Really cool changes (I promise)

This commit is contained in:
furo 2021-11-13 02:28:43 +01:00
parent 76a8658b6b
commit becbce9273
4 changed files with 47 additions and 20 deletions

View File

@ -15,6 +15,7 @@ import { dbInit } from './libs/database/init.js'
import { getStats, setStat } from './libs/serverStats.js' import { getStats, setStat } from './libs/serverStats.js'
import { downloadEssentialData } from './libs/download/dowload.js' import { downloadEssentialData } from './libs/download/dowload.js'
import { ddnssStart } from './libs/ddnss/handler.js' import { ddnssStart } from './libs/ddnss/handler.js'
import { searchMap } from './libs/database/wrapper.js'
const start = Date.now() const start = Date.now()
const log = initLog("[ MAIN ]") 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}.`)) Server.listen(process.env.PORT ?? 12345, () => log(`Server started and listening on port ${process.env.PORT}.`))
//ddnssStart() console.log(searchMap("", ["Solo"], [0,1,2,3,4,5], "points", "DESC", 1))

View File

@ -1,6 +1,8 @@
import { sqlite, skinDB } from './init.js' import { sqlite, skinDB } from './init.js'
import { simpleSanitize } from './searcher.js' import { simpleSanitize } from './searcher.js'
const entriesPerPage = process.env.ENTRIES_PER_PAGE ?? 50
/** /**
* This function returns all data pertaining a certain player * 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 * @returns {array} An array contaning all map objects
*/ */
export function allMaps() { export function allMaps() {
let output = [] // This is kinda dumb
const maps = sqlite.prepare(` return searchMap(
SELECT * FROM maps`).all() "",
["Novice", "Moderate", "Brutal" ,"Insane", "Dummy", "DDmaX", "Oldschool", "Solo", "Race", "Fun"],
for(const map of maps) { [0,1,2,3,4,5], "release", "desc", 1)
output.push(prettyifyMap(map))
}
return output
} }
/** /**
@ -183,7 +182,7 @@ export function leaderboardPoints(type, region, start, end) {
} }
let rank = 1 let rank = 1
for (const entry of leaderboard) { 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 }) output.push({ rank: rank, global: entry.rank, player: entry.player, points: entry.points, region: entry.region, flag: flag })
++rank ++rank
@ -291,16 +290,28 @@ export function finishedMaps(player) {
return unfinished return unfinished
} }
export function graphMap(map) { export function graphMap(map, player) {
const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`) 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) if(!finishes)
return undefined return undefined
let array = [] let array = []
for (const record of finishes.iterate(map)) let currentFinish
array.push({ t: record.date, y: record.time, player: record.player}) 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 return array
} }
/** /**
@ -314,23 +325,37 @@ export function graphMap(map) {
* *
* @returns {array} Returns an array of all maps found * @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 categoriesJoined = categories.join(',')
const starsJoined = stars.join(',') const starsJoined = stars.join(',')
let output = [] 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(` const maps = sqlite.prepare(`
SELECT * FROM maps WHERE map LIKE '%${query}%' SELECT * FROM maps WHERE map LIKE '%${query}%'
AND ',' || ? || ',' LIKE '%,' || category || ',%' AND ',' || ? || ',' LIKE '%,' || category || ',%'
AND ',' || ? || ',' LIKE '%,' || stars || ',%' AND ',' || ? || ',' LIKE '%,' || stars || ',%'
ORDER BY ? ${simpleSanitize(order)}`) ORDER BY ${simpleSanitize(sortBy)} ${simpleSanitize(order)} LIMIT ${entriesPerPage * (page - 1)}, ${entriesPerPage}`)
.all(categoriesJoined, starsJoined, sortBy) .all(categoriesJoined, starsJoined)
const totalPages = Math.ceil(mapCount/50)
const pageInfo = { page, mapCount: mapCount, totalPages }
for(const map of maps) { for(const map of maps) {
output.push(prettyifyMap(map)) 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) { export function mapToRegion(srv) {

View File

@ -20,8 +20,9 @@ routes.get(
const stars = req.query.stars ? req.query.stars : "1,2,3,4,5" const stars = req.query.stars ? req.query.stars : "1,2,3,4,5"
const sort = req.query.sortBy ? req.query.sortBy : "map" const sort = req.query.sortBy ? req.query.sortBy : "map"
const order = req.query.order === "desc" ? "desc" : "asc" 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" }) tx(req, res)('pages/maps.njk', { maps }, true, { currentSection: "maps" })
} }

View File

@ -28,7 +28,7 @@
<button type="submit">Search!</button> <button type="submit">Search!</button>
</form> </form>
{% for map in data.maps %} {% for map in data.maps.maps %}
{{ mapComponent.card(map) }} {{ mapComponent.card(map) }}
{% endfor %} {% endfor %}
</main> </main>