2019-10-16 19:47:53 -03:00
|
|
|
const request = require('request')
|
2021-02-01 16:30:40 -03:00
|
|
|
const music = require('../misc/music.json')
|
2019-12-15 21:11:35 -03:00
|
|
|
const Level = require('../classes/Level.js')
|
2021-01-21 19:15:31 -03:00
|
|
|
let demonList = {}
|
|
|
|
// list: [], lastUpdated: 0
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2019-10-16 19:47:53 -03:00
|
|
|
module.exports = async (app, req, res) => {
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-18 23:54:18 -03:00
|
|
|
if (req.offline) return res.send("-1")
|
2020-09-10 10:02:40 -03:00
|
|
|
|
2021-01-04 12:21:58 -03:00
|
|
|
let demonMode = req.query.hasOwnProperty("demonlist") || req.query.hasOwnProperty("demonList") || req.query.type == "demonlist" || req.query.type == "demonList"
|
|
|
|
if (demonMode) {
|
2021-01-21 19:15:31 -03:00
|
|
|
if (!req.server.demonList) return res.send('-1')
|
|
|
|
let dList = demonList[req.id]
|
|
|
|
if (!dList || !dList.list.length || dList.lastUpdated + 600000 < Date.now()) { // 10 minute cache
|
|
|
|
return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100', function (err1, resp1, list1) {
|
2021-01-17 02:05:06 -03:00
|
|
|
if (err1) return res.send("-1")
|
2021-01-21 19:15:31 -03:00
|
|
|
else return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100&after=100', function (err2, resp2, list2) {
|
2021-01-17 02:05:06 -03:00
|
|
|
if (err2) return res.send("-1")
|
2021-01-21 19:15:31 -03:00
|
|
|
demonList[req.id] = {list: JSON.parse(list1).concat(JSON.parse(list2)).map(x => String(x.level_id)), lastUpdated: Date.now()}
|
2021-01-17 02:05:06 -03:00
|
|
|
return app.run.search(app, req, res)
|
|
|
|
})
|
2021-01-04 12:21:58 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:42:47 -03:00
|
|
|
let amount = 10;
|
2021-01-18 23:54:18 -03:00
|
|
|
let count = req.isGDPS ? 10 : +req.query.count
|
2019-10-15 23:42:47 -03:00
|
|
|
if (count && count > 0) {
|
2020-11-07 21:20:44 -03:00
|
|
|
if (count > 500) amount = 500
|
2019-10-15 23:42:47 -03:00
|
|
|
else amount = count;
|
|
|
|
}
|
|
|
|
|
2020-11-01 17:29:32 -03:00
|
|
|
let filters = {
|
2019-10-15 23:42:47 -03:00
|
|
|
str: req.params.text,
|
|
|
|
|
|
|
|
diff: req.query.diff,
|
|
|
|
demonFilter: req.query.demonFilter,
|
|
|
|
page: req.query.page || 0,
|
|
|
|
gauntlet: req.query.gauntlet || 0,
|
|
|
|
len: req.query.length,
|
|
|
|
song: req.query.songID,
|
2020-03-13 22:07:54 -03:00
|
|
|
followed: req.query.creators,
|
2019-10-15 23:42:47 -03:00
|
|
|
|
|
|
|
featured: req.query.hasOwnProperty("featured") ? 1 : 0,
|
|
|
|
originalOnly: req.query.hasOwnProperty("original") ? 1 : 0,
|
|
|
|
twoPlayer: req.query.hasOwnProperty("twoPlayer") ? 1 : 0,
|
|
|
|
coins: req.query.hasOwnProperty("coins") ? 1 : 0,
|
|
|
|
epic: req.query.hasOwnProperty("epic") ? 1 : 0,
|
|
|
|
star: req.query.hasOwnProperty("starred") ? 1 : 0,
|
|
|
|
noStar: req.query.hasOwnProperty("noStar") ? 1 : 0,
|
|
|
|
customSong: req.query.hasOwnProperty("customSong") ? 1 : 0,
|
|
|
|
|
|
|
|
type: req.query.type || 0,
|
2020-11-02 01:11:37 -03:00
|
|
|
count: amount
|
2020-11-01 17:29:32 -03:00
|
|
|
}
|
2019-10-15 23:42:47 -03:00
|
|
|
|
|
|
|
if (req.query.type) {
|
|
|
|
let filterCheck = req.query.type.toLowerCase()
|
2021-01-04 12:21:58 -03:00
|
|
|
switch(filterCheck) {
|
|
|
|
case 'mostdownloaded': filters.type = 1; break;
|
|
|
|
case 'mostliked': filters.type = 2; break;
|
|
|
|
case 'trending': filters.type = 3; break;
|
|
|
|
case 'recent': filters.type = 4; break;
|
|
|
|
case 'featured': filters.type = 6; break;
|
|
|
|
case 'magic': filters.type = 7; break;
|
|
|
|
case 'awarded': filters.type = 11; break;
|
|
|
|
case 'starred': filters.type = 11; break;
|
|
|
|
case 'halloffame': filters.type = 16; break;
|
|
|
|
case 'hof': filters.type = 16; break;
|
|
|
|
}
|
2019-10-15 23:42:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req.query.hasOwnProperty("user")) {
|
2021-01-21 19:15:31 -03:00
|
|
|
let accountCheck = app.userCache(req.id, filters.str)
|
2019-10-15 23:42:47 -03:00
|
|
|
filters.type = 5
|
2020-11-07 21:20:44 -03:00
|
|
|
if (accountCheck) filters.str = accountCheck[1]
|
|
|
|
else if (!filters.str.match(/^[0-9]*$/)) return app.run.profile(app, req, res, null, req.params.text)
|
2019-10-15 23:42:47 -03:00
|
|
|
}
|
|
|
|
|
2020-03-13 22:07:54 -03:00
|
|
|
if (req.query.hasOwnProperty("creators")) filters.type = 12
|
|
|
|
|
2021-01-04 12:21:58 -03:00
|
|
|
let listSize = 10
|
2021-01-18 23:54:18 -03:00
|
|
|
if (demonMode || req.query.gauntlet || req.query.type == "saved" || ["mappack", "list", "saved"].some(x => req.query.hasOwnProperty(x))) {
|
2021-01-04 12:21:58 -03:00
|
|
|
filters.type = 10
|
2021-01-21 19:15:31 -03:00
|
|
|
filters.str = demonMode ? demonList[req.id].list : filters.str.split(",")
|
2021-01-04 12:21:58 -03:00
|
|
|
listSize = filters.str.length
|
|
|
|
filters.str = filters.str.slice(filters.page*amount, filters.page*amount + amount).join()
|
|
|
|
filters.page = 0
|
|
|
|
}
|
2021-01-15 12:29:46 -03:00
|
|
|
|
2021-01-17 02:05:06 -03:00
|
|
|
if (filters.str == "*") delete filters.str
|
2021-01-18 23:54:18 -03:00
|
|
|
req.gdRequest('getGJLevels21', req.gdParams(filters), function(err, resp, body) {
|
2021-01-14 20:18:19 -03:00
|
|
|
|
2021-01-18 23:54:18 -03:00
|
|
|
if (err || !body || body == '-1' || body.startsWith("<")) return res.send("-1")
|
2021-01-15 12:29:46 -03:00
|
|
|
let splitBody = body.split('#')
|
|
|
|
let preRes = splitBody[0].split('|')
|
|
|
|
let authorList = {}
|
|
|
|
let songList = {}
|
|
|
|
let authors = splitBody[1].split('|')
|
2021-01-25 02:14:35 -03:00
|
|
|
let songs = splitBody[2]; songs = songs.split('~:~').map(x => app.parseResponse(`~${x}~`, '~|~'))
|
2021-01-15 12:29:46 -03:00
|
|
|
songs.forEach(x => {songList[x['~1']] = x['2']})
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
authors.forEach(x => {
|
|
|
|
if (x.startsWith('~')) return
|
|
|
|
let arr = x.split(':')
|
|
|
|
authorList[arr[0]] = [arr[1], arr[2]]})
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
let levelArray = preRes.map(x => app.parseResponse(x)).filter(x => x[1])
|
|
|
|
let parsedLevels = []
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-18 23:54:18 -03:00
|
|
|
levelArray.forEach((x, y) => {
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
let songSearch = songs.find(y => y['~1'] == x[35]) || []
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-02-01 16:30:40 -03:00
|
|
|
let level = new Level(x, req.server).getSongInfo(songSearch)
|
2021-01-15 12:29:46 -03:00
|
|
|
level.author = authorList[x[6]] ? authorList[x[6]][0] : "-";
|
|
|
|
level.accountID = authorList[x[6]] ? authorList[x[6]][1] : "0";
|
2019-12-15 21:11:35 -03:00
|
|
|
|
2021-01-21 19:15:31 -03:00
|
|
|
if (demonMode) {
|
|
|
|
if (!y) level.demonList = req.server.demonList
|
|
|
|
level.demonPosition = demonList[req.id].list.indexOf(level.id) + 1
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:28:26 -03:00
|
|
|
if (req.isGDPS) level.gdps = (req.onePointNine ? "1.9/" : "") + req.endpoint
|
2021-01-21 19:15:31 -03:00
|
|
|
if (level.author != "-" && app.config.cacheAccountIDs) app.userCache(req.id, level.accountID, level.playerID, level.author)
|
2021-01-18 23:54:18 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
//this is broken if you're not on page 0, blame robtop
|
|
|
|
if (filters.page == 0 && y == 0) {
|
|
|
|
let pages = splitBody[3].split(":");
|
2019-12-15 21:11:35 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
if (filters.gauntlet) { // gauntlet page stuff
|
|
|
|
level.results = levelArray.length
|
|
|
|
level.pages = 1
|
|
|
|
}
|
2020-11-02 01:11:37 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
else if (filters.type == 10) { // custom page stuff
|
|
|
|
level.results = listSize
|
|
|
|
level.pages = +Math.ceil(listSize / (amount || 10))
|
|
|
|
}
|
2020-11-02 01:11:37 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
else { // normal page stuff
|
|
|
|
level.results = +pages[0];
|
|
|
|
level.pages = +pages[0] == 9999 ? 1000 : +Math.ceil(pages[0] / amount);
|
|
|
|
}
|
2020-11-02 01:11:37 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
}
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
parsedLevels[y] = level
|
|
|
|
})
|
2019-10-15 23:42:47 -03:00
|
|
|
|
2021-01-15 12:29:46 -03:00
|
|
|
if (filters.type == 10) parsedLevels = parsedLevels.slice((+filters.page) * amount, (+filters.page + 1) * amount)
|
|
|
|
return res.send(parsedLevels)
|
2019-10-15 23:42:47 -03:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|