2022-10-19 16:44:21 +00:00
|
|
|
import SteamIdCache from "../../src/caches/SteamIdCache";
|
2022-10-20 14:04:49 +00:00
|
|
|
import {
|
|
|
|
default as LeaderboardType,
|
|
|
|
default as WebsiteTypes,
|
|
|
|
} from "../consts/LeaderboardType";
|
2022-10-08 16:52:00 +00:00
|
|
|
|
2022-10-19 16:44:21 +00:00
|
|
|
const TO_CHECK = [
|
2022-10-20 14:04:49 +00:00
|
|
|
LeaderboardType.ScoreSaber.ApiUrl.PlayerData,
|
|
|
|
LeaderboardType.BeatLeader.ApiUrl.PlayerData,
|
2022-10-19 16:44:21 +00:00
|
|
|
];
|
|
|
|
|
2022-10-08 16:52:00 +00:00
|
|
|
export default class Utils {
|
2022-10-14 19:00:47 +00:00
|
|
|
constructor() {}
|
2022-10-08 16:52:00 +00:00
|
|
|
|
2022-10-14 19:00:47 +00:00
|
|
|
/**
|
|
|
|
* Returns the information for the given website type.
|
|
|
|
*
|
2022-10-19 16:56:07 +00:00
|
|
|
* @param {LeaderboardType} website
|
2022-10-14 19:00:47 +00:00
|
|
|
* @returns The website type's information.
|
|
|
|
*/
|
2022-10-17 11:58:07 +00:00
|
|
|
static getWebsiteApi(website) {
|
2022-10-19 16:56:07 +00:00
|
|
|
return LeaderboardType[website];
|
2022-10-14 19:00:47 +00:00
|
|
|
}
|
2022-10-19 15:52:50 +00:00
|
|
|
|
|
|
|
static openInNewTab(url) {
|
|
|
|
window.open(url, "_blank");
|
|
|
|
}
|
2022-10-19 16:44:21 +00:00
|
|
|
|
|
|
|
static async isValidSteamId(steamId) {
|
|
|
|
if (!steamId) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-19 16:47:54 +00:00
|
|
|
if (steamId.length !== 17) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-19 16:44:21 +00:00
|
|
|
|
|
|
|
if (SteamIdCache.has(steamId)) {
|
|
|
|
return SteamIdCache.get(steamId);
|
|
|
|
}
|
|
|
|
|
|
|
|
let invalid = false;
|
|
|
|
for (const url of TO_CHECK) {
|
|
|
|
const isValid = await Utils.checkLeaderboard(url, steamId);
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!isValid) {
|
|
|
|
invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SteamIdCache.set(steamId, invalid);
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async checkLeaderboard(url, steamId) {
|
|
|
|
const data = await fetch(url.replace("%s", steamId), {
|
|
|
|
headers: {
|
|
|
|
"X-Requested-With": "BeatSaber Overlay",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (data.status === 429) {
|
|
|
|
return true; // Just assume it's true is we are rate limited
|
|
|
|
}
|
|
|
|
const json = await data.json();
|
|
|
|
return !!json.pp;
|
|
|
|
}
|
2022-10-20 14:04:49 +00:00
|
|
|
|
|
|
|
static calculatePP(stars, acc, type) {
|
|
|
|
if (type === "BeatLeader") {
|
|
|
|
return WebsiteTypes.BeatLeader.ppFromAcc(acc, stars);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-10-14 19:00:47 +00:00
|
|
|
}
|