add basic player view
All checks were successful
deploy / deploy (push) Successful in 1m37s

This commit is contained in:
Lee 2023-10-19 15:48:02 +01:00
parent 1e52ac3d93
commit ce7eb17242
9 changed files with 238 additions and 2 deletions

12
package-lock.json generated

@ -19,6 +19,7 @@
"next": "13.5.5",
"node-fetch-cache": "^3.1.3",
"react": "^18",
"react-country-flag": "^3.1.0",
"react-dom": "^18",
"sharp": "^0.32.6",
"winston": "^3.11.0"
@ -4602,6 +4603,17 @@
"node": ">=0.10.0"
}
},
"node_modules/react-country-flag": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/react-country-flag/-/react-country-flag-3.1.0.tgz",
"integrity": "sha512-JWQFw1efdv9sTC+TGQvTKXQg1NKbDU2mBiAiRWcKM9F1sK+/zjhP2yGmm8YDddWyZdXVkR8Md47rPMJmo4YO5g==",
"engines": {
"node": ">=12"
},
"peerDependencies": {
"react": ">=16"
}
},
"node_modules/react-dom": {
"version": "18.2.0",
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz",

@ -20,6 +20,7 @@
"next": "13.5.5",
"node-fetch-cache": "^3.1.3",
"react": "^18",
"react-country-flag": "^3.1.0",
"react-dom": "^18",
"sharp": "^0.32.6",
"winston": "^3.11.0"

@ -0,0 +1,19 @@
import { getPlayerInfo } from "@/utils/scoresaber/api";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) {
return Response.json({ error: true, message: "No player provided" });
}
const player = await getPlayerInfo(id);
if (player == undefined) {
return Response.json({
error: true,
message: "No players with that ID were found",
});
}
return Response.json({ error: false, data: player });
}

@ -0,0 +1,144 @@
"use client";
import Avatar from "@/components/Avatar";
import Container from "@/components/Container";
import Label from "@/components/Label";
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
import { formatNumber } from "@/utils/number";
import { GlobeAsiaAustraliaIcon } from "@heroicons/react/20/solid";
import Image from "next/image";
import { useEffect, useState } from "react";
import ReactCountryFlag from "react-country-flag";
// export const metadata: Metadata = {
// title: "todo",
// };
export default function Player({ params }: { params: { id: string } }) {
const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const [loading, setLoading] = useState(true);
const [playerData, setPlayerData] = useState<ScoresaberPlayer | undefined>(
undefined,
);
useEffect(() => {
if (!params.id) {
setError(true);
setLoading(false);
return;
}
if (error || !loading) {
return;
}
fetch("/api/player/get?id=" + params.id).then(async (response) => {
const json = await response.json();
if (json.error == true) {
setError(true);
setErrorMessage(json.message);
setLoading(false);
return;
}
console.log(json);
setPlayerData(json.data);
setLoading(false);
});
}, [error, loading, params.id, playerData]);
if (loading || error || !playerData) {
return (
<main>
<Container>
<div className="mt-2 flex w-full flex-col justify-center rounded-sm bg-neutral-800">
<div className="p-3 text-center">
<div role="status">
{loading && (
<>
<svg
aria-hidden="true"
className="mr-2 inline h-8 w-8 animate-spin fill-blue-600 text-gray-200 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</>
)}
{error && (
<div className="flex flex-col items-center justify-center gap-2">
<p className="text-xl text-red-500">{errorMessage}</p>
<Image
alt="Sad cat"
src={"https://cdn.fascinated.cc/BxI9iJI9.jpg"}
width={200}
height={200}
/>
</div>
)}
</div>
</div>
</div>
</Container>
</main>
);
}
return (
<main>
<Container>
<div className="mt-2 flex w-full flex-row justify-center rounded-sm bg-neutral-800 md:flex-col">
<div className="flex flex-col items-center gap-3 p-3 md:flex-row md:items-start">
<Avatar url={playerData.profilePicture} label="Avatar" />
<div className="flex flex-col items-center gap-2 md:items-start">
<p className="text-2xl">{playerData.name}</p>
<div className="flex gap-3 text-xl">
{/* Global Rank */}
<div className="flex items-center gap-1 text-gray-300">
<GlobeAsiaAustraliaIcon width={32} height={32} />
<p>#{playerData.rank}</p>
</div>
{/* Country Rank */}
<div className="flex items-center gap-1 text-gray-300">
<ReactCountryFlag
countryCode={playerData.country}
svg
className="!h-7 !w-7"
/>
<p>#{playerData.countryRank}</p>
</div>
{/* PP */}
<div className="flex items-center text-gray-300">
<p>{formatNumber(playerData.pp)}pp</p>
</div>
</div>
{/* Labels */}
<div>
<Label
title="Total play count"
value={formatNumber(playerData.scoreStats.totalPlayCount)}
/>
</div>
</div>
</div>
</div>
</Container>
</main>
);
}

16
src/components/Label.tsx Normal file

@ -0,0 +1,16 @@
type LabelProps = {
title: string;
value: string;
};
export default function Label({ title, value }: LabelProps) {
return (
<div className="flex flex-col justify-center rounded-md bg-neutral-700">
<div className="flex items-center gap-2 p-[0.3rem]">
<p>{title}</p>
<div className="h-4 w-[1px] bg-neutral-100"></div>
<p>{value}</p>
</div>
</div>
);
}

@ -12,7 +12,7 @@ export type ScoresaberPlayer = {
role: string;
badges: ScoresaberBadge[];
histories: string;
scoreStats: ScoresaberScoreStats[];
scoreStats: ScoresaberScoreStats;
permissions: number;
banned: boolean;
inactive: boolean;

9
src/utils/number.ts Normal file

@ -0,0 +1,9 @@
/**
* Formats a number to a string with commas
*
* @param number the number to format
* @returns the formatted number
*/
export function formatNumber(number: number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

@ -17,6 +17,7 @@ const SEARCH_PLAYER_URL =
API_URL + "/players?search={}&page=1&withMetadata=false";
const PLAYER_SCORES =
API_URL + "/player/{}/scores?limit={}&sort={}&page={}&withMetadata=true";
const GET_PLAYER_DATA_FULL = API_URL + "/player/{}/full";
const SearchType = {
RECENT: "recent",
@ -43,6 +44,35 @@ export async function searchByName(
return json.players as ScoresaberPlayer[];
}
/**
* Returns the player info for the provided player id
*
* @param playerId the id of the player
* @returns the player info
*/
export async function getPlayerInfo(
playerId: string,
): Promise<ScoresaberPlayer | undefined> {
const response = await fetch(formatString(GET_PLAYER_DATA_FULL, playerId));
const json = await response.json();
// Check if there was an error fetching the user data
if (json.errorMessage) {
return undefined;
}
return json as ScoresaberPlayer;
}
/**
* Get the players scores from the given page
*
* @param playerId the id of the player
* @param page the page to get the scores from
* @param searchType the type of search to perform
* @param limit the limit of scores to get
* @returns a list of scores
*/
export async function fetchScores(
playerId: string,
page: number = 1,

@ -2608,6 +2608,11 @@ rc@^1.2.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"
react-country-flag@^3.1.0:
version "3.1.0"
resolved "https://registry.npmjs.org/react-country-flag/-/react-country-flag-3.1.0.tgz"
integrity sha512-JWQFw1efdv9sTC+TGQvTKXQg1NKbDU2mBiAiRWcKM9F1sK+/zjhP2yGmm8YDddWyZdXVkR8Md47rPMJmo4YO5g==
react-dom@^18, react-dom@^18.0.0, react-dom@^18.2.0:
version "18.2.0"
resolved "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz"
@ -2621,7 +2626,7 @@ react-is@^16.13.1:
resolved "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
"react@^16.8.0 || ^17.0.0 || ^18", react@^18, react@^18.0.0, react@^18.2.0, "react@>= 16", "react@>= 16.8.0 || 17.x.x || ^18.0.0-0":
"react@^16.8.0 || ^17.0.0 || ^18", react@^18, react@^18.0.0, react@^18.2.0, "react@>= 16", "react@>= 16.8.0 || 17.x.x || ^18.0.0-0", react@>=16:
version "18.2.0"
resolved "https://registry.npmjs.org/react/-/react-18.2.0.tgz"
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==