2019-10-16 18:47:53 -04:00
|
|
|
const request = require('request')
|
|
|
|
const fs = require('fs')
|
2019-12-15 19:11:35 -05:00
|
|
|
const Level = require('../classes/Level.js')
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2019-10-16 18:47:53 -04:00
|
|
|
module.exports = async (app, req, res, api, analyze) => {
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2021-07-04 15:43:58 -04:00
|
|
|
function rejectLevel() {
|
2020-09-10 09:02:40 -04:00
|
|
|
if (!api) return res.redirect('search/' + req.params.id)
|
2021-12-07 14:14:56 -05:00
|
|
|
else return res.sendError()
|
2020-09-10 09:02:40 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 15:43:58 -04:00
|
|
|
if (req.offline) return rejectLevel()
|
|
|
|
|
2019-10-15 22:42:47 -04:00
|
|
|
let levelID = req.params.id
|
2019-12-25 16:20:32 -05:00
|
|
|
if (levelID == "daily") return app.run.download(app, req, res, api, 'daily', analyze)
|
|
|
|
else if (levelID == "weekly") return app.run.download(app, req, res, api, 'weekly', analyze)
|
2021-07-04 15:43:58 -04:00
|
|
|
else if (levelID.match(/[^0-9]/)) return rejectLevel()
|
2019-10-15 22:42:47 -04:00
|
|
|
else levelID = levelID.replace(/[^0-9]/g, "")
|
|
|
|
|
2019-12-25 16:20:32 -05:00
|
|
|
if (analyze || req.query.hasOwnProperty("download")) return app.run.download(app, req, res, api, levelID, analyze)
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2021-01-18 21:54:18 -05:00
|
|
|
req.gdRequest('getGJLevels21', { str: levelID, type: 0 }, function (err, resp, body) {
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2021-08-18 20:10:35 -04:00
|
|
|
if (err || body.startsWith("##")) return rejectLevel()
|
2019-10-15 22:42:47 -04:00
|
|
|
|
|
|
|
let preRes = body.split('#')[0].split('|', 10)
|
|
|
|
let author = body.split('#')[1].split('|')[0].split(':')
|
2019-12-15 19:11:35 -05:00
|
|
|
let song = '~' + body.split('#')[2];
|
|
|
|
song = app.parseResponse(song, '~|~')
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2021-03-09 23:14:06 -05:00
|
|
|
let levelInfo = app.parseResponse(preRes.find(x => x.startsWith(`1:${levelID}`)) || preRes[0])
|
2021-02-01 14:30:40 -05:00
|
|
|
let level = new Level(levelInfo, req.server, false, author).getSongInfo(song)
|
2021-07-04 15:43:58 -04:00
|
|
|
if (!level.id) return rejectLevel()
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2021-08-03 11:51:09 -04:00
|
|
|
if (req.isGDPS) level.gdps = (req.onePointNine ? "1.9/" : "") + req.server.id
|
2021-01-21 17:15:31 -05:00
|
|
|
if (level.author != "-") app.userCache(req.id, level.accountID, level.playerID, level.author)
|
2021-01-18 21:54:18 -05:00
|
|
|
|
2019-10-17 00:17:55 -04:00
|
|
|
function sendLevel() {
|
|
|
|
|
2022-02-06 19:18:45 -05:00
|
|
|
if (api) return res.send(level)
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2019-12-15 19:11:35 -05:00
|
|
|
else return fs.readFile('./html/level.html', 'utf8', function (err, data) {
|
|
|
|
let html = data;
|
2021-01-14 18:18:19 -05:00
|
|
|
let filteredSong = level.songName.replace(/[^ -~]/g, "") // strip off unsupported characters
|
|
|
|
level.songName = filteredSong || level.songName
|
2019-12-15 19:11:35 -05:00
|
|
|
let variables = Object.keys(level)
|
|
|
|
variables.forEach(x => {
|
|
|
|
let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g")
|
|
|
|
html = html.replace(regex, app.clean(level[x]))
|
|
|
|
})
|
2021-02-03 09:38:50 -05:00
|
|
|
if (req.server.downloadsDisabled) html = html.replace('id="additional" class="', 'id="additional" class="downloadDisabled ')
|
2021-01-18 21:54:18 -05:00
|
|
|
.replace('analyzeBtn"', 'analyzeBtn" style="filter: opacity(30%)"')
|
2022-02-06 19:18:45 -05:00
|
|
|
return res.send(html)
|
2019-10-15 22:42:47 -04:00
|
|
|
})
|
2019-12-15 19:11:35 -05:00
|
|
|
}
|
2019-10-17 00:17:55 -04:00
|
|
|
|
2021-01-21 17:15:31 -05:00
|
|
|
if (req.server.demonList && level.difficulty == "Extreme Demon") {
|
|
|
|
request.get(req.server.demonList + 'api/v2/demons/?name=' + level.name.trim(), function (err, resp, demonList) {
|
2020-04-23 00:56:19 -04:00
|
|
|
if (err) return sendLevel()
|
2019-10-18 16:49:10 -04:00
|
|
|
let demon = JSON.parse(demonList)
|
2021-01-04 10:21:58 -05:00
|
|
|
if (demon[0] && demon[0].position) level.demonList = demon[0].position
|
2019-10-17 00:17:55 -04:00
|
|
|
return sendLevel()
|
|
|
|
})
|
2019-12-15 19:11:35 -05:00
|
|
|
}
|
2019-10-17 00:17:55 -04:00
|
|
|
|
|
|
|
else return sendLevel()
|
2019-10-15 22:42:47 -04:00
|
|
|
|
2019-12-15 19:11:35 -05:00
|
|
|
})
|
2019-10-15 22:42:47 -04:00
|
|
|
}
|