diff --git a/app/account/page.tsx b/app/account/page.tsx index 756f287..14a2e98 100644 --- a/app/account/page.tsx +++ b/app/account/page.tsx @@ -13,6 +13,9 @@ import updateProfileType from "@/utils/firebase/db/updateProfileType"; import deleteAccount from "@/utils/firebase/account/deleteAccount"; import googleSignOut from "@/utils/firebase/account/googleSignOut"; import { useRouter } from "next/navigation"; +import getAverageScore from "@/utils/score/getAverageScore"; +import countTotalSplits from "@/utils/score/countTotalSplits"; +import StatBox from "@/components/account/statBox"; export default function History() { // general @@ -21,6 +24,13 @@ export default function History() { const { user } = useAuthContext() as { user: any }; const router = useRouter(); + // stats + const [averageScore, setAverageScore] = useState(0); + const [highScore, setHighScore] = useState(0); + const [lowScore, setLowScore] = useState(0); + const [totalGames, setTotalGames] = useState(0); + const [totalSplits, setTotalSplits] = useState(0); + // account const [name, setName] = useState(""); const [namePlaceholder, setNamePlaceholder] = useState(""); @@ -39,6 +49,11 @@ export default function History() { setUserDoc(doc); setNamePlaceholder(doc.displayName); setProfileType(doc.profileType); + setAverageScore(getAverageScore(doc.allScores)); + setTotalSplits(countTotalSplits(doc.allScores)); + setTotalGames(doc.allScores.length); + setHighScore(doc.highScore); + setLowScore(doc.lowScore); }); }, []); @@ -77,7 +92,15 @@ export default function History() { selected={currentWindow == "account" ? true : false} /> - {currentWindow == "stats" ?

Stats

: null} + {currentWindow == "stats" ? ( +
+ + + + + +
+ ) : null} {currentWindow == "social" ?

Social

: null} {currentWindow == "account" ? (
diff --git a/components/account/statBox.tsx b/components/account/statBox.tsx new file mode 100644 index 0000000..bf3b23e --- /dev/null +++ b/components/account/statBox.tsx @@ -0,0 +1,8 @@ +export default function StatBox({ name, value }: { name: string; value: number }) { + return ( +
+

{name}

+

{value}

+
+ ); +} diff --git a/utils/score/countTotalSplits.ts b/utils/score/countTotalSplits.ts new file mode 100644 index 0000000..f1a75fd --- /dev/null +++ b/utils/score/countTotalSplits.ts @@ -0,0 +1,6 @@ +export default function countTotalSplits(allScores: any) { + return allScores.reduce((count: any, score: any) => { + score = JSON.parse(score); + return count + score.length; + }, 0); +} diff --git a/utils/score/getAverageScore.ts b/utils/score/getAverageScore.ts new file mode 100644 index 0000000..c2613ed --- /dev/null +++ b/utils/score/getAverageScore.ts @@ -0,0 +1,14 @@ +import countTotalSplits from "./countTotalSplits"; + +export default function getAverageScore(allScores: any) { + let total = 0; + + allScores.forEach((score: any) => { + score = score.replace(/[[\]]/g, "").split(",").map(Number); + total += score.reduce((a: any, b: any) => a + b); + }); + + let average = total / countTotalSplits(allScores); + + return Math.round(average * 10) / 10; +}