import * as hash from './hash'; const levelsPerPage = 10; // should be kept at 10, increasing/decreasing may lead to unforseeable consequences const handleRequestsAt = ['getGJLevels21', 'getGJLevels20', 'getGJLevels19', 'getGJLevels']; module.exports = (app) : void => { let getLevels = async (req, res) => { let levelsString = ''; let searchQuery: any = {}; // fuck you typescript, i have to use any or else it screams console.log(req.body); if (isNaN(Number(req.body.str)) || req.body.str == '') searchQuery.unlisted = false; if (req.body.featured != '0') searchQuery.featured = true; if (req.body.epic != '0') searchQuery.epic = true; if (req.body.coins != '0') searchQuery.coins = true; if (req.body.twoPlayer != '0') searchQuery.twoPlayer = true; if (req.body.star) searchQuery.stars = { $gt: 0 }; if (req.body.original != '0') searchQuery.original = 0; if (req.body.noStar) searchQuery.stars = 0; if (req.body.song) { if (Number(req.body.song) <= 50) { // lets assume a limit of 50 official songs searchQuery.audioTrack = Number(req.body.song); // official song } else { searchQuery.songID = Number(req.body.song); // newgrounds/custom songs } } if (req.body.diff != '-') { // if the client requests -1, -2 or -3 then it cant request any other difficulties searchQuery.auto = false; if (req.body.diff == '-1') { // NA levels searchQuery.difficulty = 0; } else if (req.body.diff == '-2') { // demons if (req.body.demonFilter) { searchQuery.difficulty = 60 + req.body.demonFilter * 10; } else { searchQuery.difficulty = { $gte: 60 }; } } else if (req.body.diff == '-3') { // auto searchQuery.auto = true; } else { searchQuery.$or = req.body.diff .split(',') // multiple diffs .map(d => { return { difficulty: Number(d) * 10 }; }); } } const levels = await app.get('db').collection('levels').find( { $and: [ { $or: [ { id: Number(req.body.str) }, { name: { $regex: `${req.body.str}` } } ] }, searchQuery ] }) .toArray(); switch (Number(req.body.type)) { case 1: // most downloaded levels.sort((a, b) => b.downloads - a.downloads); break; case 2: // most liked levels.sort((a, b) => b.likes - a.likes); break; case 3: // trending levels.filter(l => { l.timestamp || 0 > Date.now() - 604800000; }); levels.sort((a, b) => b.downloads - a.downloads); break; case 4: // recent levels.sort((a, b) => b.timestamp || 0 - a.timestamp || 0); break; case 6: // featured levels.filter(l => l.featured); levels.sort((a, b) => b.timestamp || 0 - a.timestamp || 0); break; case 7: // ~ M a G i C ~ levels.filter(l => { l.objects || 0 > 1000; }); levels.sort((a, b) => b.timestamp || 0 - a.timestamp || 0); break; case 11: // awarded levels.filter(l => { l.stars > 0; }); levels.sort((a, b) => b.timestamp || 0 - a.timestamp || 0); break; case 12: // TODO: followed case 13: // TODO: friends levels.filter(() => false); break; case 16: // epic levels.filter(l => l.epic); levels.sort((a, b) => b.timestamp || 0 - a.timestamp || 0); } let offset = Math.max(Number(req.body.page) * levelsPerPage - 1, 0); const foundLevels = levels .slice(offset, (Number(req.body.page) + 1) * levelsPerPage - 1); let results = foundLevels .map(l => `1:${l.id}:2:${l.name}:5:${l.version}:6:${l.userID}:8:10:9:${l.difficulty}:10:${l.downloads}:12:${l.audioTrack}:13:${l.gameVersion}:14:${l.likes}:17:${Number(l.difficulty >= 60)}:43:${Math.max(l.difficulty - 60, 0)}:25:${Number(l.auto)}:18:${l.stars}:19:${Number(l.featured)}:42:${Number(l.epic)}:45:${l.objects}:3:${l.description}:15:${l.length}:30:${l.original}:31:0:37:${l.coins}:38:${Number(l.ratedCoins)}:39:${l.requestedStars}:46:1:47:2:40:${Number(l.hasLDM)}:35:${l.songID}` ) .join('|'); let users = new Array(10) // placeholder .fill('16:mat:0') .join('|'); let songs = ''; // placeholder levelsString = [results, users, songs, levels.length].join('#') + `:${offset}:${levelsPerPage}#` + hash.hashLevels(foundLevels); res.status(200).send(levelsString); }; for (let i in handleRequestsAt) app.post('/' + app.get('config').addtopath + 'database/' + handleRequestsAt[i] + '.php', getLevels); };