cumstorm/src/getGJLevels21.ts

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-01-24 21:52:51 +01:00
import * as hash from './hash';
2020-01-25 15:25:38 +01:00
const levelsPerPage = 10; // should be kept at 10, increasing/decreasing may lead to unforseeable consequences
2020-01-24 22:57:59 +01:00
module.exports = (app, db) : void => {
app.post('/aa/database/getGJLevels21.php', async (req, res) => {
2020-01-24 21:52:51 +01:00
let levelsString = '';
2020-01-25 11:47:44 +01:00
let searchQuery: any = {}; // fuck you typescript, i have to use any or else it screams
2020-01-25 13:02:31 +01:00
console.log(req.body);
2020-01-25 15:23:12 +01:00
if (isNaN(Number(req.body.str)) || req.body.str == '') searchQuery.unlisted = false;
2020-01-25 14:30:53 +01:00
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;
2020-01-25 11:47:44 +01:00
if (req.body.star) searchQuery.stars = {$gt: 0};
2020-01-25 14:30:53 +01:00
if (req.body.original != '0') searchQuery.original = true;
2020-01-25 15:37:29 +01:00
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
}
}
2020-01-25 11:47:44 +01:00
if (req.body.diff != '-') {
// if the client requests -1, -2 or -3 then it cant request any other difficulties
2020-01-25 14:30:53 +01:00
searchQuery.auto = false;
2020-01-25 11:47:44 +01:00
if (req.body.diff == '-1') { // NA levels
searchQuery.diff = 0;
} else if(req.body.diff == '-2') { // demons
2020-01-25 13:02:31 +01:00
if (req.body.demonFilter) {
searchQuery.diff = 60 + req.body.demonFilter * 10;
} else {
searchQuery.diff = { $gte: 60 };
}
2020-01-25 11:47:44 +01:00
} else if(req.body.diff == '-3') { // auto
2020-01-25 14:30:53 +01:00
searchQuery.auto = true;
2020-01-25 11:47:44 +01:00
} 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
]
})
2020-01-24 22:57:59 +01:00
.toArray();
2020-01-25 14:14:08 +01:00
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);
}
2020-01-25 15:25:38 +01:00
let offset = Math.max(Number(req.body.page) * levelsPerPage - 1, 0);
2020-01-25 14:00:33 +01:00
const foundLevels = levels
2020-01-25 15:25:38 +01:00
.slice(offset, (Number(req.body.page) + 1) * levelsPerPage - 1);
2020-01-25 14:00:33 +01:00
let results = foundLevels
2020-01-24 21:52:51 +01:00
.map(l =>
2020-01-25 14:30:53 +01:00
`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`
2020-01-24 21:52:51 +01:00
)
.join('|');
2020-01-25 14:00:33 +01:00
let users = new Array(10) // placeholder
2020-01-24 21:52:51 +01:00
.fill('16:mat:0')
.join('|');
let songs = ''; // placeholder
2020-01-25 14:00:33 +01:00
levelsString = [results, users, songs, levels.length].join('#') +
2020-01-25 15:25:38 +01:00
`:${offset}:${levelsPerPage}#` +
2020-01-25 14:00:33 +01:00
hash.hashLevels(foundLevels);
2020-01-24 21:52:51 +01:00
res.status(200).send(levelsString);
});
};