cumstorm/src/getGJLevels21.ts

123 lines
3.5 KiB
TypeScript

import * as hash from './hash';
const levelsPerPage = 10; // should be kept at 10, increasing/decreasing may lead to unforseeable consequences
module.exports = (app, db) : void => {
app.post('/aa/database/getGJLevels21.php', 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 = true;
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.audio_track = Number(req.body.song); // official song
} else {
searchQuery.song_id = 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.diff = 0;
} else if(req.body.diff == '-2') { // demons
if (req.body.demonFilter) {
searchQuery.diff = 60 + req.body.demonFilter * 10;
} else {
searchQuery.diff = { $gte: 60 };
}
} else if(req.body.diff == '-3') { // auto
searchQuery.auto = true;
} else {
searchQuery.$or = req.body.diff
.split(',') // multiple diffs
.map(d => {
return {diff: Number(d) * 10};
});
}
}
const levels = await 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 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);
}
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:0:6:16:8:10:9:${l.diff}:10:${l.downloads}:12:1:13:21:14:${l.likes}:17:0:43:0:25:${Number(l.auto)}:18:${l.stars}:19:0:42:0:45:10:3:${l.description}:15:1:30:0:31:0:37:0:38:0:39:0:46:1:47:2:40:0:35:0`
)
.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);
});
};