2022-10-21 15:25:30 +00:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import WebsiteTypes from "../../../src/consts/LeaderboardType";
|
2022-10-21 16:12:32 +00:00
|
|
|
import { getValue, setValue, valueExists } from "../../../src/utils/redisUtils";
|
2022-10-21 15:25:30 +00:00
|
|
|
import { diffToScoreSaberDiff } from "../../../src/utils/scoreSaberUtils";
|
|
|
|
|
|
|
|
const KEY = "SS_MAP_STAR_";
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Request} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
|
|
|
|
const difficulty = req.query.difficulty.replace(" ", "");
|
|
|
|
const characteristic = req.query.characteristic;
|
|
|
|
|
|
|
|
const key = `${KEY}${difficulty}-${characteristic}-${mapHash}`;
|
2022-10-21 16:12:32 +00:00
|
|
|
const exists = await valueExists(key);
|
2022-10-21 15:25:30 +00:00
|
|
|
if (exists) {
|
2022-10-21 16:12:32 +00:00
|
|
|
const data = await getValue(key);
|
2022-10-21 15:25:30 +00:00
|
|
|
res.setHeader("Cache-Status", "hit");
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
status: "OK",
|
|
|
|
stars: Number.parseFloat(data),
|
|
|
|
difficulty: difficulty,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const before = Date.now();
|
|
|
|
const data = await fetch(
|
|
|
|
WebsiteTypes.ScoreSaber.ApiUrl.MapData.replace("%h", mapHash).replace(
|
|
|
|
"%d",
|
|
|
|
diffToScoreSaberDiff(difficulty)
|
|
|
|
),
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"X-Requested-With": "BeatSaber Overlay",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (data.status === 404) {
|
|
|
|
return res.status(404).json({
|
|
|
|
status: 404,
|
|
|
|
message: "Unknown Map Hash",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const json = await data.json();
|
|
|
|
let starCount = json.stars;
|
|
|
|
if (starCount === undefined) {
|
|
|
|
return res.status(404).json({
|
|
|
|
status: 404,
|
|
|
|
message: "Unknown Map Hash",
|
|
|
|
});
|
|
|
|
}
|
2022-10-21 16:12:32 +00:00
|
|
|
await setValue(key, starCount);
|
2022-10-21 15:25:30 +00:00
|
|
|
console.log(
|
|
|
|
`[Cache]: Cached SS Star Count for hash ${mapHash} in ${
|
|
|
|
Date.now() - before
|
|
|
|
}ms`
|
|
|
|
);
|
|
|
|
res.setHeader("Cache-Status", "miss");
|
|
|
|
return res.status(200).json({
|
|
|
|
status: "OK",
|
|
|
|
stars: starCount,
|
|
|
|
});
|
|
|
|
}
|