Move components into a src folder. Added mock data endpoint

This commit is contained in:
Liam 2022-10-08 17:52:00 +01:00
parent e3bac7ffbe
commit 6eb8ba80c7
11 changed files with 61 additions and 26 deletions

@ -6,7 +6,6 @@ import Config from '../config.json';
import '../styles/globals.css' import '../styles/globals.css'
function MyApp({ Component, pageProps }) { function MyApp({ Component, pageProps }) {
return ( return (
// 2. Use at the root of your app // 2. Use at the root of your app

@ -6,7 +6,7 @@ import fetch from 'node-fetch';
const cacheDir = process.cwd() + path.sep + "cache"; const cacheDir = process.cwd() + path.sep + "cache";
if (!fs.existsSync(cacheDir)) { if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir); fs.mkdirSync(cacheDir);
console.log("Created cache directory") console.log("Created cache directory");
} }
export default async function handler(req, res) { export default async function handler(req, res) {
@ -18,14 +18,14 @@ export default async function handler(req, res) {
if (!exists) { if (!exists) {
const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`); const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`);
let buffer = await data.buffer(); let buffer = await data.buffer();
buffer = await sharp(buffer).resize(150, 150).toBuffer() buffer = await sharp(buffer).resize(150, 150).toBuffer();
fs.writeFileSync(imagePath, buffer); fs.writeFileSync(imagePath, buffer);
res.setHeader('Content-Type', 'image/' + ext) res.setHeader('Content-Type', 'image/' + ext);
res.send(buffer); res.send(buffer);
console.log("Song Cache - Added song \"" + mapHash + "\"") console.log("Song Cache - Added song \"" + mapHash + "\"");
return; return;
} }
const buffer = fs.readFileSync(imagePath); const buffer = fs.readFileSync(imagePath);
res.setHeader('Content-Type', 'image/jpg' + ext) res.setHeader('Content-Type', 'image/jpg' + ext);
res.send(buffer); res.send(buffer);
} }

@ -1,11 +1,11 @@
import Utils from '../../../utils/utils' import Utils from '../../../utils/utils';
export default async function handler(req, res) { export default async function handler(req, res) {
const mapHash = req.query.hash; const mapHash = req.query.hash;
const mapData = await Utils.getMapData(mapHash.replace("custom_level_", "")); const mapData = await Utils.getMapData(mapHash.replace("custom_level_", ""));
if (mapData === undefined) { // Check if a map hash was provided if (mapData === undefined) { // Check if a map hash was provided
return res.json({ error: true, message: "Unknown map" }) return res.json({ error: true, message: "Unknown map" });
} }
const data = { // The maps data from the provided map hash const data = { // The maps data from the provided map hash
bsr: mapData.id, bsr: mapData.id,

9
pages/api/mockdata.js Normal file

@ -0,0 +1,9 @@
export default async function handler(req, res) {
res.json({
"avatar": "https://avatars.akamai.steamstatic.com/4322d8d20cb6dbdd1d891b4efa9952a9679c9a76_full.jpg",
"country": "GB",
"pp": 0,
"rank": 0,
"countryRank": 0,
});
}

@ -1,14 +1,11 @@
import {Component} from 'react' import {Component} from 'react'
import Avatar from '../components/Avatar'; import Avatar from '../src/components/Avatar';
import PlayerStats from '../components/PlayerStats'; import PlayerStats from '../src/components/PlayerStats';
import ScoreStats from '../components/ScoreStats'; import ScoreStats from '../src/components/ScoreStats';
import SongInfo from "../src/components/SongInfo";
import Config from '../config.json'; import Config from '../config.json';
import SongInfo from "../components/SongInfo"; import Utils from '../src/utils/utils';
// Why do u have to proxy requests... it's so dumb LOL
const SCORESABER_API_URL = Config.proxy_url + "/https://scoresaber.com/api/player/%s/full";
const BEATLEADER_API_URL = Config.proxy_url + "/https://api.beatleader.xyz/player/%s";
export default class Home extends Component { export default class Home extends Component {
@ -142,8 +139,8 @@ export default class Home extends Component {
* @param {string} id The steam id of the player * @param {string} id The steam id of the player
* @returns * @returns
*/ */
async updateData(id) { async updateData(id) {
const data = await fetch(this.state.websiteType == "ScoreSaber" ? SCORESABER_API_URL.replace("%s", id) : BEATLEADER_API_URL.replace("%s", id), { const data = await fetch(new Utils().getWebsiteApi(id == "test" ? "Test" : this.state.websiteType).ApiUrl.replace("%s", id), {
mode: 'cors' mode: 'cors'
}); });
const json = await data.json(); const json = await data.json();
@ -195,15 +192,15 @@ export default class Home extends Component {
resetData(visible) { resetData(visible) {
console.log("Exiting level, resetting data.") console.log("Exiting level, resetting data.")
this.setState({ this.setState({
"leftHand": { leftHand: {
"averageCut": [15.00], averageCut: [15.00],
"averagePreSwing": [70.00], averagePreSwing: [70.00],
"averagePostSwing": [30.00], averagePostSwing: [30.00],
}, },
"rightHand": { rightHand: {
"averageCut": [15.00], averageCut: [15.00],
"averagePreSwing": [70.00], averagePreSwing: [70.00],
"averagePostSwing": [30.00], averagePostSwing: [30.00],
}, },
songInfo: undefined, songInfo: undefined,
beatSaverData: undefined, beatSaverData: undefined,

15
src/consts/WebsiteType.js Normal file

@ -0,0 +1,15 @@
import Config from '../../config.json';
const WebsiteTypes = {
ScoreSaber: {
ApiUrl: Config.proxy_url + "/https://scoresaber.com/api/player/%s/full"
},
BeatLeader: {
ApiUrl: Config.proxy_url + "/https://api.beatleader.xyz/player/%s"
},
Test: {
ApiUrl: "/api/mockdata"
}
}
export default WebsiteTypes

15
src/utils/utils.js Normal file

@ -0,0 +1,15 @@
import WebsiteTypes from "../consts/WebsiteType";
export default class Utils {
constructor() {};
/**
* Returns the information for the given website type.
*
* @param {String} website
* @returns The website type's information.
*/
getWebsiteApi(website) {
return WebsiteTypes[website]
}
}