cumstorm/src/getGJLevels21.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-01-24 21:52:51 +01:00
import * as hash from './hash';
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);
if (req.body.featured != '0') searchQuery.featured = 1;
if (req.body.epic != '0') searchQuery.epic = 1;
if (req.body.coins != '0') searchQuery.coins = 1;
if (req.body.twoPlayer != '0') searchQuery.twoPlayer = 1;
2020-01-25 11:47:44 +01:00
if (req.body.star) searchQuery.stars = {$gt: 0};
2020-01-25 13:02:31 +01:00
if (req.body.original != '0') searchQuery.original = 1;
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 13:02:31 +01:00
searchQuery.auto = 0;
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
searchQuery.auto = 1;
} 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:00:33 +01:00
let offset = Math.max(Number(req.body.page) * 10 - 1, 0);
const foundLevels = levels
.slice(offset, (Number(req.body.page) + 1) * 10 - 1);
let results = foundLevels
2020-01-24 21:52:51 +01:00
.map(l =>
2020-01-25 11:47:44 +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:${l.auto ? '1' : '0'}: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('#') +
`:${offset}:10#` +
hash.hashLevels(foundLevels);
2020-01-24 21:52:51 +01:00
res.status(200).send(levelsString);
});
};