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/helpers/validateSteamId.js

41 lines
1017 B
JavaScript
Raw Normal View History

2022-10-20 18:00:27 +01:00
import { default as LeaderboardType } from "../consts/LeaderboardType";
2022-10-21 17:12:32 +01:00
import { getValue, setValue, valueExists } from "../utils/redisUtils";
2022-10-20 18:00:27 +01:00
import Utils from "../utils/utils";
const KEY = "VALID_STEAM_ID_";
const TO_CHECK = [
LeaderboardType.ScoreSaber.ApiUrl.PlayerData,
LeaderboardType.BeatLeader.ApiUrl.PlayerData,
];
2022-10-21 17:12:32 +01:00
export async function isValidSteamId(steamId) {
2022-10-20 18:00:27 +01:00
if (!steamId) {
return false;
}
if (steamId.length !== 17) {
return false;
}
2022-10-21 17:12:32 +01:00
const exists = await valueExists(`${KEY}${steamId}`);
2022-10-20 18:00:27 +01:00
if (exists) {
2022-10-21 17:12:32 +01:00
const data = await getValue(`${KEY}${steamId}`);
2022-10-20 18:00:27 +01:00
return Boolean(data);
}
2022-10-21 14:51:29 +01:00
const before = Date.now();
2022-10-20 18:00:27 +01:00
let valid = false;
for (const url of TO_CHECK) {
const isValid = await Utils.checkLeaderboard(url, steamId);
if (isValid) {
valid = true;
break;
}
}
await setValue(`${KEY}${steamId}`, valid, valid ? 86400 * 7 : 86400); // Expire in a week if is valid
2022-10-21 14:51:29 +01:00
console.log(
`[Cache]: Cached Steam ID for id ${steamId} in ${Date.now() - before}ms`
);
2022-10-20 18:00:27 +01:00
return valid;
}