From 8fb6047dfb7284dde368e3c9f30895a005066dcc Mon Sep 17 00:00:00 2001 From: furo Date: Fri, 5 Nov 2021 19:38:22 +0100 Subject: [PATCH] Add a simple searching function for maps --- libs/database/searcher.js | 2 +- libs/database/wrapper.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/libs/database/searcher.js b/libs/database/searcher.js index 1102150..8480df4 100644 --- a/libs/database/searcher.js +++ b/libs/database/searcher.js @@ -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, "") } diff --git a/libs/database/wrapper.js b/libs/database/wrapper.js index 8593407..d1e002f 100644 --- a/libs/database/wrapper.js +++ b/libs/database/wrapper.js @@ -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,