Add a simple searching function for maps

This commit is contained in:
furo 2021-11-05 19:38:22 +01:00
parent 69313e86c7
commit 8fb6047dfb
2 changed files with 32 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import { sqlite } from './init.js'
const entriesPerPage = process.env.ENTRIES_PER_PAGE ?? 50
function simpleSanitize(str) {
export function simpleSanitize(str) {
return String(str).replace(/\s/g, "")
}

View File

@ -1,5 +1,5 @@
import { sqlite } from './init.js'
import { simpleSanitize } from './searcher.js'
/**
* This function checks if a player exists
*
@ -347,6 +347,35 @@ export function graphMap(map) {
return array
}
/**
* This searches the DB for maps
*
* @param {string} query The query to search for
* @param {array} categories Which categories to be searched ["Novice", "Insane"]
* @param {array} stars Only include maps with a specific amount of stars [1, 5]
* @param {string} sortBy What to sort after
* @param {string} order "ASC" | "DESC"
*
* @returns {array} Returns an array of all maps found
*/
export function searchMap(query, categories, stars, sortBy, order) {
const categoriesJoined = categories.join(',')
const starsJoined = stars.join(',')
let output = []
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)
for(const map of maps) {
output.push(prettyifyMap(map))
}
console.log(output)
}
export default {
playerExists,
finishedMaps,
@ -358,6 +387,7 @@ export default {
mapCategory,
allMaps,
mapExists,
searchMap,
leaderboardRace,
leaderboardTeamrace,
categoryExists,