2022-02-15 19:29:59 +00:00
|
|
|
import {Component} from 'react'
|
2022-02-14 19:27:30 +00:00
|
|
|
import Avatar from '../components/Avatar';
|
|
|
|
import PlayerStats from '../components/PlayerStats';
|
2022-02-15 19:21:09 +00:00
|
|
|
import ScoreStats from '../components/ScoreStats';
|
|
|
|
|
|
|
|
import Config from '../config.json';
|
2022-02-17 02:25:43 +00:00
|
|
|
import SongInfo from "../components/SongInfo";
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
// Why do u have to proxy requests... it's so dumb LOL
|
2022-02-17 02:25:43 +00:00
|
|
|
const SCORESABER_API_URL = Config.proxy_url + "/https://scoresaber.com/api/player/%s/full";
|
2022-02-15 19:21:09 +00:00
|
|
|
const GITHUB_URL = "https://github.com/RealFascinated/beatsaber-overlay";
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
export default class Home extends Component {
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-20 23:52:57 +00:00
|
|
|
#_beatSaverURL = "";
|
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
id: undefined,
|
|
|
|
isValidScoresaber: true,
|
2022-02-15 19:21:09 +00:00
|
|
|
data: undefined,
|
|
|
|
showPlayerStats: true,
|
2022-02-17 02:25:43 +00:00
|
|
|
showScore: false,
|
|
|
|
showSongInfo: false,
|
|
|
|
|
|
|
|
socket: undefined,
|
|
|
|
isVisible: false,
|
|
|
|
songInfo: undefined,
|
|
|
|
beatSaverData: undefined,
|
|
|
|
currentSongTime: 0,
|
|
|
|
paused: true,
|
|
|
|
currentScore: 0,
|
|
|
|
percentage: "100.00%",
|
|
|
|
failed: false,
|
|
|
|
leftHand: {
|
|
|
|
averageCut: [15.00],
|
|
|
|
averagePreSwing: [70.00],
|
|
|
|
averagePostSwing: [30.00],
|
|
|
|
},
|
|
|
|
rightHand: {
|
|
|
|
averageCut: [15.00],
|
|
|
|
averagePreSwing: [70.00],
|
|
|
|
averagePostSwing: [30.00],
|
|
|
|
}
|
2022-02-14 19:27:30 +00:00
|
|
|
}
|
2022-02-17 02:25:43 +00:00
|
|
|
this.setupTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// I'd love if HTTP Status just gave this data lmao
|
2022-04-02 05:32:51 +00:00
|
|
|
// HttpSiraStatus(https://github.com/denpadokei/HttpSiraStatus) does give this data.
|
|
|
|
isCurrentSongTimeProvided = false;
|
|
|
|
// we don't need to reset this to false because it is highly unlikely for a player to swap mods within a browser session
|
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Setup the timer for the song time
|
|
|
|
*/
|
2022-02-17 02:25:43 +00:00
|
|
|
setupTimer() {
|
|
|
|
setInterval(() => {
|
2022-04-02 05:32:51 +00:00
|
|
|
if (this.isCurrentSongTimeProvided) {
|
|
|
|
return
|
|
|
|
}
|
2022-02-17 02:25:43 +00:00
|
|
|
if (!this.state.paused && this.state.beatSaverData !== undefined) {
|
|
|
|
this.setState({ currentSongTime: this.state.currentSongTime + 1 })
|
|
|
|
}
|
|
|
|
}, 1000);
|
2022-02-14 19:27:30 +00:00
|
|
|
}
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Update the current song time
|
|
|
|
*
|
|
|
|
* @param {[]} data The song data
|
|
|
|
*/
|
2022-04-02 05:32:51 +00:00
|
|
|
handleCurrentSongTime(data) {
|
|
|
|
try {
|
|
|
|
const time = data.status.performance.currentSongTime
|
2022-04-02 06:32:11 +00:00
|
|
|
if (time !== undefined && time != null) {
|
2022-04-02 05:32:51 +00:00
|
|
|
this.isCurrentSongTimeProvided = true
|
2022-04-02 06:32:11 +00:00
|
|
|
this.setState({ currentSongTime: time })
|
2022-04-02 05:32:51 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
async componentDidMount() {
|
2022-02-20 23:52:57 +00:00
|
|
|
this.#_beatSaverURL = document.location.origin + "/api/beatsaver/map?hash=%s";
|
2022-02-14 19:27:30 +00:00
|
|
|
const urlSearchParams = new URLSearchParams(window.location.search);
|
|
|
|
const params = Object.fromEntries(urlSearchParams.entries());
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
const id = params.id;
|
2022-02-15 19:21:09 +00:00
|
|
|
if (!id) { // Check if the id param is valid
|
2022-02-14 19:27:30 +00:00
|
|
|
this.setState({ loading: false, isValidScoresaber: false });
|
|
|
|
return;
|
|
|
|
}
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-02-15 19:21:09 +00:00
|
|
|
// Check if the player wants to disable their stats (pp, global pos, etc)
|
|
|
|
if (params.playerstats === 'false') {
|
|
|
|
this.setState({ showPlayerStats: false });
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:25:43 +00:00
|
|
|
// Check if the player wants to show their current score information
|
2022-02-15 19:21:09 +00:00
|
|
|
if (params.scoreinfo === 'true') {
|
|
|
|
this.setState({ showScore: true });
|
2022-02-17 02:25:43 +00:00
|
|
|
this.connectSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the player wants to show the current song
|
|
|
|
if (params.songinfo === 'true') {
|
|
|
|
this.setState({ showSongInfo: true });
|
2022-02-15 19:21:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
await this.updateData(id);
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (!this.state.isValidScoresaber) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.updateData(id);
|
|
|
|
}, 30_000); // Update the scoresaber data every 30 seconds.
|
|
|
|
}
|
2022-02-14 17:43:52 +00:00
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Fetch and update the data from ScoreSaber
|
|
|
|
*
|
|
|
|
* @param {string} id The scoresaber id of the player
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-02-14 19:27:30 +00:00
|
|
|
async updateData(id) {
|
2022-02-17 02:25:43 +00:00
|
|
|
const data = await fetch(SCORESABER_API_URL.replace("%s", id), {
|
2022-02-14 19:27:30 +00:00
|
|
|
mode: 'cors'
|
|
|
|
});
|
2022-02-17 02:25:43 +00:00
|
|
|
if (data.status === 422) { // Invalid scoresaber account (I think??)
|
2022-02-14 19:27:30 +00:00
|
|
|
this.setState({ loading: false, isValidScoresaber: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const json = await data.json();
|
2022-02-17 02:25:43 +00:00
|
|
|
if (json.errorMessage) { // Invalid scoresaber account
|
2022-02-14 19:27:30 +00:00
|
|
|
this.setState({ loading: false, isValidScoresaber: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ loading: false, id: id, data: json });
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Setup the HTTP Status connection
|
|
|
|
*/
|
2022-02-17 02:25:43 +00:00
|
|
|
connectSocket() {
|
|
|
|
const socket = new WebSocket('ws://localhost:6557/socket');
|
2022-02-20 23:52:57 +00:00
|
|
|
socket.addEventListener('close', () => {
|
2022-02-17 02:25:43 +00:00
|
|
|
console.log("Attempting to re-connect to the HTTP Status socket in 30 seconds.");
|
|
|
|
setTimeout(() => this.connectSocket(), 30_000);
|
|
|
|
});
|
|
|
|
socket.addEventListener('message', (message) => {
|
|
|
|
const json = JSON.parse(message.data);
|
2022-04-02 05:32:51 +00:00
|
|
|
this.handleCurrentSongTime(json)
|
2022-02-17 02:25:43 +00:00
|
|
|
if (!this.handlers[json.event]) {
|
|
|
|
console.log("Unhandled message from HTTP Status. (" + json.event + ")");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.handlers[json.event](json || []);
|
|
|
|
})
|
|
|
|
this.setState({ socket: socket });
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Set the current songs beat saver url in {@link #_beatSaverURL}
|
|
|
|
*
|
|
|
|
* @param {[]} songData
|
|
|
|
*/
|
2022-02-17 02:25:43 +00:00
|
|
|
async setBeatSaver(songData) {
|
|
|
|
console.log("Updating BeatSaver info")
|
2022-02-20 23:52:57 +00:00
|
|
|
const data = await fetch(this.#_beatSaverURL.replace("%s", songData.levelId));
|
2022-02-17 02:25:43 +00:00
|
|
|
const json = await data.json();
|
|
|
|
this.setState({ beatSaverData: json })
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
/**
|
|
|
|
* Cleanup the data and get ready for the next song
|
|
|
|
*
|
|
|
|
* @param {boolean} visible Whether to show info other than the player stats
|
|
|
|
*/
|
2022-02-17 02:25:43 +00:00
|
|
|
resetData(visible) {
|
|
|
|
console.log("Exiting level, resetting data.")
|
|
|
|
this.setState({
|
|
|
|
"leftHand": {
|
|
|
|
"averageCut": [15.00],
|
|
|
|
"averagePreSwing": [70.00],
|
|
|
|
"averagePostSwing": [30.00],
|
|
|
|
},
|
|
|
|
"rightHand": {
|
|
|
|
"averageCut": [15.00],
|
|
|
|
"averagePreSwing": [70.00],
|
|
|
|
"averagePostSwing": [30.00],
|
|
|
|
},
|
|
|
|
songInfo: undefined,
|
|
|
|
beatSaverData: undefined,
|
|
|
|
currentSongTime: 0,
|
|
|
|
currentScore: 0,
|
|
|
|
percentage: "100.00%",
|
|
|
|
isVisible: visible
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:32:11 +00:00
|
|
|
// The HTTP Status handlers
|
2022-02-17 02:25:43 +00:00
|
|
|
handlers = {
|
|
|
|
"hello": (data) => {
|
|
|
|
console.log("Hello from HTTP Status!");
|
|
|
|
if (data.status) {
|
|
|
|
this.setState({songData: data});
|
|
|
|
if (data.status.beatmap) {
|
|
|
|
this.setBeatSaver(data.status.beatmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"scoreChanged": (data) => {
|
|
|
|
const { status } = data;
|
|
|
|
const { score, currentMaxScore } = status.performance;
|
|
|
|
const percent = currentMaxScore > 0 ? ((score / currentMaxScore) * 1000 / 10).toFixed(2) : 0.00;
|
|
|
|
this.setState({
|
|
|
|
currentScore: score,
|
|
|
|
percentage: this.state.failed ? percent * 2 : percent + "%"
|
|
|
|
})
|
|
|
|
},
|
|
|
|
"noteFullyCut": (data) => {
|
|
|
|
const { noteCut } = data;
|
|
|
|
|
|
|
|
// Left Saber
|
|
|
|
if (noteCut.saberType === 'SaberA') {
|
|
|
|
const data = this.state.leftHand;
|
|
|
|
if (data.averageCut.includes(15) && data.averageCut.length === 1) {
|
|
|
|
data.averageCut = [];
|
|
|
|
}
|
|
|
|
if (data.averagePreSwing.includes(70) && data.averagePreSwing.length === 1) {
|
|
|
|
data.averagePreSwing = [];
|
|
|
|
}
|
|
|
|
if (data.averagePostSwing.includes(30) && data.averagePostSwing.length === 1) {
|
|
|
|
data.averagePostSwing = [];
|
|
|
|
}
|
|
|
|
data.averagePreSwing.push(noteCut.initialScore > 70 ? 70 : noteCut.initialScore);
|
|
|
|
data.averagePostSwing.push(noteCut.finalScore - noteCut.initialScore);
|
|
|
|
data.averageCut.push(noteCut.cutDistanceScore);
|
|
|
|
this.setState({ leftHand: data });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left Saber
|
|
|
|
if (noteCut.saberType === 'SaberB') {
|
|
|
|
const data = this.state.rightHand;
|
|
|
|
if (data.averageCut.includes(15) && data.averageCut.length === 1) {
|
|
|
|
data.averageCut = [];
|
|
|
|
}
|
|
|
|
if (data.averagePreSwing.includes(70) && data.averagePreSwing.length === 1) {
|
|
|
|
data.averagePreSwing = [];
|
|
|
|
}
|
|
|
|
if (data.averagePostSwing.includes(30) && data.averagePostSwing.length === 1) {
|
|
|
|
data.averagePostSwing = [];
|
|
|
|
}
|
|
|
|
data.averagePreSwing.push(noteCut.initialScore > 70 ? 70 : noteCut.initialScore);
|
|
|
|
data.averagePostSwing.push(noteCut.finalScore - noteCut.initialScore);
|
|
|
|
data.averageCut.push(noteCut.cutDistanceScore);
|
|
|
|
this.setState({ rightHand: data });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"songStart": (data) => {
|
|
|
|
console.log("Going into level, resetting data.")
|
|
|
|
this.resetData(true);
|
|
|
|
this.setState({ songData: data, paused: false })
|
|
|
|
this.setBeatSaver(data.status.beatmap);
|
|
|
|
},
|
|
|
|
"finished": () => {
|
|
|
|
console.log("Exiting level, resetting data.")
|
|
|
|
this.resetData(false);
|
|
|
|
},
|
|
|
|
"softFail": () => {
|
|
|
|
this.setState({ failed: true });
|
|
|
|
},
|
2022-04-02 06:32:11 +00:00
|
|
|
"pause": () => {
|
2022-02-17 02:25:43 +00:00
|
|
|
this.setState({ paused: true });
|
|
|
|
},
|
|
|
|
"resume": () => {
|
|
|
|
this.setState({ paused: false });
|
|
|
|
},
|
|
|
|
"menu": () => {
|
|
|
|
console.log("Exiting level, resetting data.")
|
|
|
|
this.resetData(false);
|
|
|
|
},
|
|
|
|
"noteCut": () => {},
|
|
|
|
"noteMissed": () => {},
|
|
|
|
"noteSpawned": () => {},
|
|
|
|
"bombMissed": () => {},
|
|
|
|
"beatmapEvent": () => {}
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
render() {
|
|
|
|
const { loading, isValidScoresaber, data } = this.state;
|
|
|
|
|
2022-02-14 19:40:28 +00:00
|
|
|
if (!isValidScoresaber && !loading) {
|
|
|
|
const body = document.body;
|
|
|
|
body.style.backgroundColor = "#181a1b";
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:27:30 +00:00
|
|
|
return <>
|
|
|
|
{ loading ?
|
2022-02-14 19:40:28 +00:00
|
|
|
<div className={'loading'}>
|
|
|
|
<h2>Loading...</h2>
|
|
|
|
</div>
|
2022-02-14 19:27:30 +00:00
|
|
|
: !isValidScoresaber ?
|
|
|
|
<div className={'invalid-player'}>
|
2022-02-15 19:21:09 +00:00
|
|
|
<div style={{ fontWeight: 'bold', marginBottom: '50px' }}>
|
|
|
|
<p>This is currently just a simple overlay for OBS displaying ScoreSaber stats.</p>
|
|
|
|
<p>If you have any suggestions you can message me on discord @ Fascinated#4719</p>
|
|
|
|
</div>
|
2022-02-14 19:27:30 +00:00
|
|
|
<p>Provide a valid scoresaber id</p>
|
|
|
|
<p>Example: {document.location.origin}?id=76561198449412074</p>
|
2022-02-15 19:21:09 +00:00
|
|
|
<p>Example with Score Info: {document.location.origin}?id=76561198449412074&scoreinfo=true</p>
|
2022-02-14 19:27:30 +00:00
|
|
|
<div className={'info'}>
|
2022-02-15 19:21:09 +00:00
|
|
|
<div>
|
|
|
|
<h3>Options</h3>
|
|
|
|
<p>scoreinfo - Can be "true" if you want to show your current score (needs HTTP Status)</p>
|
|
|
|
<p>playerstats - Can be "false" if you disable showing your stats (pp, global pos, etc)</p>
|
2022-02-17 02:25:43 +00:00
|
|
|
<p>songinfo - Can be "true" if want to see information about the song (song name, bsr, song art, etc)</p>
|
|
|
|
<br />
|
|
|
|
<p>To use a option just add &key=value (eg: &songinfo=true)</p>
|
2022-02-15 19:21:09 +00:00
|
|
|
</div>
|
2022-02-14 19:48:10 +00:00
|
|
|
<div className={'info'}>
|
|
|
|
<p>If you use this overlay and like it, don't forget to star the project :3</p>
|
2022-02-15 19:21:09 +00:00
|
|
|
<p>Github link: <span><a href={GITHUB_URL}>{GITHUB_URL}</a></span></p>
|
2022-02-14 19:48:10 +00:00
|
|
|
</div>
|
2022-02-14 19:27:30 +00:00
|
|
|
</div>
|
|
|
|
</div> :
|
|
|
|
<div className={'overlay'}>
|
2022-02-15 19:21:09 +00:00
|
|
|
{ this.state.showPlayerStats ?
|
|
|
|
<div className={'player-stats-container'}>
|
|
|
|
<Avatar url={data.profilePicture} />
|
|
|
|
<PlayerStats
|
|
|
|
pp={data.pp.toLocaleString()}
|
|
|
|
globalPos={data.rank.toLocaleString()}
|
|
|
|
country={data.country}
|
|
|
|
countryRank={data.countryRank.toLocaleString()}
|
|
|
|
/>
|
|
|
|
</div> :
|
|
|
|
""
|
|
|
|
}
|
|
|
|
{
|
2022-02-17 02:25:43 +00:00
|
|
|
this.state.showScore && this.state.isVisible ? <ScoreStats data={this.state} /> : ""
|
|
|
|
}
|
|
|
|
{
|
|
|
|
this.state.showSongInfo && this.state.beatSaverData !== undefined && this.state.isVisible ? <SongInfo data={this.state}/> : ""
|
2022-02-15 19:21:09 +00:00
|
|
|
}
|
2022-02-14 19:27:30 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
}
|