From 84868ab1dfc472339d3661adcdd7f83ad848f3a7 Mon Sep 17 00:00:00 2001 From: Daithi Hearn Date: Mon, 27 Feb 2023 22:10:05 +0100 Subject: [PATCH 1/2] Showing stats for all players --- src/caches/GameStatsSlice.ts | 34 ------- src/caches/caches.ts | 2 - src/components/GameStats/GameStats.tsx | 80 +++------------- src/components/GameStats/PlayerSwitcher.tsx | 21 ++--- .../GameStats/WinPercentageGraph.tsx | 92 +++++++++++++++++++ src/components/StartNewGame/StartNewGame.tsx | 30 ++++-- src/components/Tables/CustomStyles.ts | 1 + src/pages/Home/Home.tsx | 4 + src/services/StatsService.ts | 7 +- 9 files changed, 143 insertions(+), 128 deletions(-) delete mode 100644 src/caches/GameStatsSlice.ts create mode 100644 src/components/GameStats/WinPercentageGraph.tsx diff --git a/src/caches/GameStatsSlice.ts b/src/caches/GameStatsSlice.ts deleted file mode 100644 index 0231833..0000000 --- a/src/caches/GameStatsSlice.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { createSlice, PayloadAction } from "@reduxjs/toolkit" -import { RootState } from "./caches" - -export interface GameStats { - gameId: string - timestamp: string - winner: boolean - score: number - rings: number -} - -export interface GameStatsState { - stats: GameStats[] -} - -const initialState: GameStatsState = { - stats: [], -} - -export const gameStatsSlice = createSlice({ - name: "gameStats", - initialState: initialState, - reducers: { - updateGameStats: (_, action: PayloadAction) => { - return { - stats: action.payload, - } - }, - }, -}) - -export const { updateGameStats } = gameStatsSlice.actions - -export const getGameStats = (state: RootState) => state.gameStats.stats diff --git a/src/caches/caches.ts b/src/caches/caches.ts index d28377d..278a56b 100644 --- a/src/caches/caches.ts +++ b/src/caches/caches.ts @@ -9,7 +9,6 @@ import { import { myProfileSlice } from "./MyProfileSlice" import { gameSlice } from "./GameSlice" -import { gameStatsSlice } from "./GameStatsSlice" import { myGamesSlice } from "./MyGamesSlice" import { myCardsSlice } from "./MyCardsSlice" import { autoPlaySlice } from "./AutoPlaySlice" @@ -19,7 +18,6 @@ const combinedReducer = combineReducers({ myProfile: myProfileSlice.reducer, game: gameSlice.reducer, myGames: myGamesSlice.reducer, - gameStats: gameStatsSlice.reducer, playerProfiles: playerProfilesSlice.reducer, myCards: myCardsSlice.reducer, autoPlay: autoPlaySlice.reducer, diff --git a/src/components/GameStats/GameStats.tsx b/src/components/GameStats/GameStats.tsx index 039931e..c57bc11 100644 --- a/src/components/GameStats/GameStats.tsx +++ b/src/components/GameStats/GameStats.tsx @@ -1,65 +1,17 @@ -import { Doughnut } from "react-chartjs-2" import { Card, CardHeader, CardBody, CardGroup, Input, Label } from "reactstrap" -import { useCallback, useMemo, useState } from "react" +import { useCallback, useEffect, useState } from "react" import { useAppSelector } from "../../caches/hooks" -import { getGameStats } from "../../caches/GameStatsSlice" -import "chart.js/auto" -import { ChartOptions } from "chart.js" import { getMyProfile } from "../../caches/MyProfileSlice" import PlayerSwitcher from "./PlayerSwitcher" +import WinPercentageGraph from "./WinPercentageGraph" +import { PlayerProfile } from "../../model/Player" const GameStats = () => { const myProfile = useAppSelector(getMyProfile) - const stats = useAppSelector(getGameStats) - + const [player, setPlayer] = useState() const [last3Months, updateLast3Months] = useState(true) - const fromDate = new Date() - fromDate.setMonth(fromDate.getMonth() - 3) - - const filteredStats = useMemo( - () => - last3Months - ? stats.filter(s => new Date(s.timestamp) >= fromDate) - : stats, - [last3Months, stats], - ) - - const wins = useMemo( - () => filteredStats.filter(g => g.winner), - [filteredStats], - ) - - const data = useMemo(() => { - return { - labels: ["Win", "Loss"], - datasets: [ - { - label: "My Win Percentage", - data: [wins.length, filteredStats.length - wins.length], - backgroundColor: ["rgb(54, 162, 235)", "rgb(255, 99, 132)"], - hoverOffset: 4, - }, - ], - } - }, [wins, filteredStats]) - - const options: ChartOptions = useMemo(() => { - return { - maintainAspectRatio: false, - plugins: { - title: { - display: true, - text: `Win Percentage (${( - (wins.length / filteredStats.length) * - 100 - ).toFixed(1)}%)`, - position: "bottom", - }, - }, - } - }, [wins, filteredStats]) - + useEffect(() => setPlayer(myProfile), [myProfile]) const threeMonthsCheckboxChanged = useCallback( (e: React.ChangeEvent) => { updateLast3Months(e.target.checked) @@ -67,28 +19,22 @@ const GameStats = () => { [], ) - if (!stats) { - return null - } - return ( Stats - {myProfile.isAdmin ? : null} + {myProfile.isAdmin ? ( + + ) : null} - {filteredStats.length > 0 ? ( - - ) : ( - "No stats available currently" - )} + ) : null}