GDBrowser/api/leaderboards/accurate.js

47 lines
2.6 KiB
JavaScript
Raw Normal View History

const {GoogleSpreadsheet} = require('google-spreadsheet');
const sheet = new GoogleSpreadsheet('1ADIJvAkL0XHGBDhO7PP9aQOuK3mPIKB2cVPbshuBBHc'); // accurate leaderboard spreadsheet
2021-06-24 19:24:08 -04:00
let indexes = ["stars", "coins", "demons", "diamonds"]
2020-11-14 16:14:29 -05:00
let forms = ['cube', 'ship', 'ball', 'ufo', 'wave', 'robot', 'spider']
2021-06-24 19:24:08 -04:00
let lastIndex = [{"stars": 0, "coins": 0, "demons": 0}, {"stars": 0, "coins": 0, "demons": 0, "diamonds": 0}]
let caches = [{"stars": null, "coins": null, "demons": null, "diamonds": null}, {"stars": null, "coins": null, "demons": null, "diamonds": null}, {"stars": null, "coins": null, "demons": null, "diamonds": null}] // 0 for JSON, 1 for mods, 2 for GD
2020-09-16 09:25:15 -04:00
2020-10-02 14:33:24 -04:00
module.exports = async (app, req, res, post) => {
// Accurate leaderboard returns 418 because private servers do not use.
if (req.isGDPS) return res.status(418).send("-2")
if (!app.sheetsKey) return res.status(500).send([])
2020-10-02 14:33:24 -04:00
let gdMode = post || req.query.hasOwnProperty("gd")
2020-12-01 00:07:10 -05:00
let modMode = !gdMode && req.query.hasOwnProperty("mod")
let cache = caches[gdMode ? 2 : modMode ? 1 : 0]
2020-09-16 09:25:15 -04:00
let type = req.query.type ? req.query.type.toLowerCase() : 'stars'
if (type == "usercoins") type = "coins"
2021-06-24 19:24:08 -04:00
if (!indexes.includes(type)) type = "stars"
2022-02-06 19:18:45 -05:00
if (lastIndex[modMode ? 1 : 0][type] + 600000 > Date.now() && cache[type]) return res.send(gdMode ? cache[type] : JSON.parse(cache[type])) // 10 min cache
sheet.useApiKey(app.sheetsKey)
sheet.loadInfo().then(async () => {
let tab = sheet.sheetsById[1555821000]
2021-06-24 19:24:08 -04:00
await tab.loadCells('A2:H2')
2020-12-01 00:07:10 -05:00
2021-06-24 19:24:08 -04:00
let cellIndex = indexes.findIndex(x => type == x)
if (modMode) cellIndex += indexes.length
2020-12-01 00:07:10 -05:00
let cell = tab.getCell(1, cellIndex).value
if (!cell || typeof cell != "string" || cell.startsWith("GoogleSpreadsheetFormulaError")) { console.log("Spreadsheet Error:"); console.log(cell); return res.sendError() }
2021-02-28 11:27:27 -05:00
let leaderboard = JSON.parse(cell.replace(/~( |$)/g, ""))
2020-09-16 09:25:15 -04:00
let gdFormatting = ""
leaderboard.forEach(x => {
app.userCache(req.id, x.accountID, x.playerID, x.username)
gdFormatting += `1:${x.username}:2:${x.playerID}:13:${x.coins}:17:${x.usercoins}:6:${x.rank}:9:${x.icon.icon}:10:${x.icon.col1}:11:${x.icon.col2}:14:${forms.indexOf(x.icon.form)}:15:${x.icon.glow ? 2 : 0}:16:${x.accountID}:3:${x.stars}:8:${x.cp}:46:${x.diamonds}:4:${x.demons}|`
})
2020-12-01 00:07:10 -05:00
caches[modMode ? 1 : 0][type] = JSON.stringify(leaderboard)
caches[2][type] = gdFormatting
lastIndex[modMode ? 1 : 0][type] = Date.now()
2022-02-06 19:18:45 -05:00
return res.send(gdMode ? gdFormatting : leaderboard)
})
}