From 7fcec62869aaa41a5dc3e14e2783139387399c11 Mon Sep 17 00:00:00 2001 From: Liam <67254223+RealFascinated@users.noreply.github.com> Date: Thu, 20 Oct 2022 15:51:52 +0100 Subject: [PATCH] BL star cache --- pages/api/beatleader/stars.js | 41 +++++++++++++++++++++++++++++++++++ src/caches/BLMapStarCache.js | 3 +++ src/components/ScoreStats.js | 2 +- src/consts/LeaderboardType.js | 11 ++-------- 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 pages/api/beatleader/stars.js create mode 100644 src/caches/BLMapStarCache.js diff --git a/pages/api/beatleader/stars.js b/pages/api/beatleader/stars.js new file mode 100644 index 0000000..95e94ab --- /dev/null +++ b/pages/api/beatleader/stars.js @@ -0,0 +1,41 @@ +import fetch from "node-fetch"; +import BLMapStarCache from "../../../src/caches/BLMapStarCache"; +import WebsiteTypes from "../../../src/consts/LeaderboardType"; + +export default async function handler(req, res) { + const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase(); + const difficulty = req.query.difficulty; + const characteristic = req.query.characteristic; + + if (BLMapStarCache.has(mapHash)) { + return res.json({ + status: "OK", + message: "Cache hit for " + mapHash, + stars: BLMapStarCache.get(mapHash), + }); + } + + const data = await fetch( + WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash) + .replace("%d", difficulty.replace("+", "Plus")) + .replace("%m", characteristic), + { + headers: { + "X-Requested-With": "BeatSaber Overlay", + }, + } + ); + if (data.status === 404) { + return res.json({ + status: 404, + message: "Unknown hash", + }); + } + const json = await data.json(); + BLMapStarCache.set(mapHash, json.difficulty.stars); + return res.json({ + status: "OK", + message: "Cache miss for " + mapHash, + stars: json.difficulty.stars, + }); +} diff --git a/src/caches/BLMapStarCache.js b/src/caches/BLMapStarCache.js new file mode 100644 index 0000000..2934fa0 --- /dev/null +++ b/src/caches/BLMapStarCache.js @@ -0,0 +1,3 @@ +const CACHE = new Map(); + +module.exports = CACHE; diff --git a/src/components/ScoreStats.js b/src/components/ScoreStats.js index fdc10a9..63430da 100644 --- a/src/components/ScoreStats.js +++ b/src/components/ScoreStats.js @@ -20,7 +20,7 @@ export default class ScoreStats extends Component { render() { const data = this.props.data; - let currentPP = Utils.calculatePP( + const currentPP = Utils.calculatePP( data.mapStarCount, data.percentage.replace("%", ""), data.websiteType diff --git a/src/consts/LeaderboardType.js b/src/consts/LeaderboardType.js index 6437607..47d1f6c 100644 --- a/src/consts/LeaderboardType.js +++ b/src/consts/LeaderboardType.js @@ -16,17 +16,10 @@ const WebsiteTypes = { }, async getMapStarCount(mapHash, mapDiff, characteristic) { const data = await fetch( - this.ApiUrl.MapData.replace("%h", mapHash) - .replace("%d", mapDiff.replace("+", "Plus")) - .replace("%m", characteristic), - { - headers: { - "X-Requested-With": "BeatSaber Overlay", - }, - } + `/api/beatleader/stars?hash=${mapHash}&difficulty=${mapDiff}&characteristic=${characteristic}` ); const json = await data.json(); - return json.difficulty.stars || undefined; + return json.stars || undefined; }, curve(acc, stars) { var l = 1 - (0.03 * (stars - 3.0)) / 11.0;