This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/src/utils/utils.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

import { default as LeaderboardType } from "../consts/LeaderboardType";
2022-10-21 16:12:32 +00:00
import { getBeatLeaderPP } from "../curve/BeatLeaderCurve";
import { getScoreSaberPP } from "../curve/ScoreSaberCurve";
export default class Utils {
2022-10-14 19:00:47 +00:00
/**
* Returns the information for the given website type.
*
2022-10-28 18:52:47 +00:00
* @param {string} 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");
}
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) {
2022-10-30 14:45:12 +00:00
if (stars <= 0) {
return undefined;
}
2022-10-20 14:04:49 +00:00
if (type === "BeatLeader") {
2022-10-21 16:12:32 +00:00
return getBeatLeaderPP(acc, stars);
}
if (type === "ScoreSaber") {
2022-10-21 16:12:32 +00:00
return getScoreSaberPP(acc, stars);
2022-10-20 14:04:49 +00:00
}
return undefined;
}
2022-10-20 17:00:27 +00:00
static base64ToArrayBuffer(base64) {
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
}
2022-10-28 18:52:47 +00:00
static stringToBoolean = (stringValue) => {
switch (stringValue?.toLowerCase()?.trim()) {
case "true":
case "yes":
case "1":
return true;
case "false":
case "no":
case "0":
case null:
case undefined:
return false;
default:
return JSON.parse(stringValue);
}
};
2022-10-14 19:00:47 +00:00
}