2023-01-06 22:44:24 +00:00
|
|
|
import axios from "axios";
|
2022-10-21 15:25:30 +00:00
|
|
|
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";
|
2022-12-11 09:42:24 +00:00
|
|
|
import { useSongDataStore } from "../store/songDataStore";
|
2022-10-08 16:52:00 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2022-10-19 16:44:21 +00:00
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
static async isLeaderboardValid(url, steamId) {
|
2023-01-06 22:44:24 +00:00
|
|
|
const response = await axios.get(url.replace("%s", steamId), {
|
2022-10-19 16:44:21 +00:00
|
|
|
headers: {
|
|
|
|
"X-Requested-With": "BeatSaber Overlay",
|
|
|
|
},
|
|
|
|
});
|
2023-01-06 22:44:24 +00:00
|
|
|
if (response.status === 429) {
|
2022-10-19 16:44:21 +00:00
|
|
|
return true; // Just assume it's true is we are rate limited
|
|
|
|
}
|
2023-01-06 22:44:24 +00:00
|
|
|
const json = response.data;
|
2022-10-19 16:44:21 +00:00
|
|
|
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") {
|
2023-03-26 19:39:55 +00:00
|
|
|
const leaderboardData =
|
|
|
|
useSongDataStore.getState().mapLeaderboardData.beatleader;
|
|
|
|
|
|
|
|
return getBeatLeaderPP(
|
|
|
|
acc,
|
|
|
|
leaderboardData.accRating,
|
|
|
|
leaderboardData.passRating,
|
|
|
|
leaderboardData.techRating
|
|
|
|
);
|
2022-10-21 15:25:30 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-12-11 09:42:24 +00:00
|
|
|
static calculateModifierBonus() {
|
|
|
|
const songMods = useSongDataStore.getState().songModifiers;
|
|
|
|
const modifierMulipliers =
|
2023-03-26 19:39:55 +00:00
|
|
|
useSongDataStore.getState().mapLeaderboardData.beatleader.modifiers;
|
2023-03-21 16:07:12 +00:00
|
|
|
let bonus = 1;
|
2022-12-11 09:42:24 +00:00
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// No Fail
|
2022-12-11 09:42:24 +00:00
|
|
|
if (
|
|
|
|
songMods.noFail == true &&
|
|
|
|
modifierMulipliers.nf < 0 &&
|
|
|
|
useSongDataStore.getState().failed
|
|
|
|
) {
|
|
|
|
bonus -= modifierMulipliers.nf;
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// Speed Modifiers
|
2022-12-11 09:42:24 +00:00
|
|
|
if (songMods.songSpeed != "Normal") {
|
2022-12-11 10:03:39 +00:00
|
|
|
if (songMods.songSpeed == "SuperSlow" && modifierMulipliers.ss > 0) {
|
|
|
|
bonus -= modifierMulipliers.ss;
|
|
|
|
}
|
|
|
|
if (songMods.songSpeed == "Faster" && modifierMulipliers.fs > 0) {
|
2022-12-11 09:42:24 +00:00
|
|
|
bonus += modifierMulipliers.fs;
|
|
|
|
}
|
|
|
|
if (songMods.songSpeed == "SuperFast" && modifierMulipliers.sf > 0) {
|
|
|
|
bonus += modifierMulipliers.sf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// Disappearing Arrows
|
2022-12-11 09:42:24 +00:00
|
|
|
if (songMods.disappearingArrows == true && modifierMulipliers.da > 0) {
|
|
|
|
bonus += modifierMulipliers.da;
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// Ghost Notes
|
2022-12-11 09:42:24 +00:00
|
|
|
if (songMods.ghostNotes == true && modifierMulipliers.gn > 0) {
|
|
|
|
toAdd += modifierMulipliers.gn;
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// No Arrows
|
2022-12-11 09:42:24 +00:00
|
|
|
if (songMods.noArrows == true && modifierMulipliers.na < 0) {
|
|
|
|
bonus -= modifierMulipliers.na;
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:03:39 +00:00
|
|
|
// No Bombs
|
2022-12-11 09:42:24 +00:00
|
|
|
if (songMods.noBombs == true && modifierMulipliers.nb < 0) {
|
|
|
|
bonus -= modifierMulipliers.nb;
|
|
|
|
}
|
2022-12-11 10:03:39 +00:00
|
|
|
|
|
|
|
// No Obstacles
|
|
|
|
if (songMods.obstacles == false && modifierMulipliers.no < 0) {
|
|
|
|
bonus -= modifierMulipliers.no;
|
|
|
|
}
|
|
|
|
|
2022-12-11 09:42:24 +00:00
|
|
|
return bonus;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|