diff --git a/app/api/game/route.ts b/app/api/game/route.ts index 0df57f5f..9b84f9a0 100644 --- a/app/api/game/route.ts +++ b/app/api/game/route.ts @@ -1,7 +1,7 @@ import { revalidatePath } from 'next/cache' import { NextResponse } from 'next/server' -import { createGameType } from '@common/types' +import { AddedGameResponse, createGameType } from '@common/types' import { createGame, removeLatestGame } from '@server/db/games' import { formatRecentGame } from '@server/db/games/derivatives' @@ -12,7 +12,11 @@ export async function POST(req: Request) { const jsonGame = formatRecentGame(game) revalidatePath('/stats') - return NextResponse.json({ recentGame: jsonGame, winner: game.winner, loser: game.loser }) + return NextResponse.json({ + recentGame: jsonGame, + winner: game.winner, + loser: game.loser, + } as AddedGameResponse) } export async function DELETE() { diff --git a/app/components/Homepage/AddGame/AddGameModal/Content/GameCreation/index.tsx b/app/components/Homepage/AddGame/AddGameModal/Content/GameCreation/index.tsx index e08b4193..5411a88a 100644 --- a/app/components/Homepage/AddGame/AddGameModal/Content/GameCreation/index.tsx +++ b/app/components/Homepage/AddGame/AddGameModal/Content/GameCreation/index.tsx @@ -1,7 +1,7 @@ import { Dispatch, SetStateAction } from 'react' -import { RecentGame } from '@common/types' -import Player from '@server/models/rawModels/Player' +import { AddedGameResponse, RecentGame } from '@common/types' +import useSeasonalMode from '@hooks/useSeasonalMode' import { addToRecentPlayers, setPlayerId, setUndertable, useModalState } from '@state/Modal' import { removeFromQueue, useQueueState } from '@state/Queue' @@ -15,6 +15,7 @@ interface Props { } const GameCreation = ({ setGames, onClose }: Props) => { + const { seasonal } = useSeasonalMode() const [{ game }, dispatchModal] = useModalState() const [_, dispatchQueue] = useQueueState() @@ -32,9 +33,18 @@ const GameCreation = ({ setGames, onClose }: Props) => { }, body: JSON.stringify(game), }) - const data = await res.json() + const data = (await res.json()) as unknown as AddedGameResponse dispatchModal( - addToRecentPlayers([data.loser as unknown as Player, data.winner as unknown as Player]) + addToRecentPlayers([ + { + ...data.loser, + elo: seasonal ? data.loser.seasonElo ?? 400 : data.loser.elo, + }, + { + ...data.loser, + elo: seasonal ? data.loser.seasonElo ?? 400 : data.loser.elo, + }, + ]) ) dispatchModal(setPlayerId('winner', undefined)) dispatchModal(setPlayerId('loser', undefined)) diff --git a/app/components/Homepage/AddGame/AddGameModal/Content/PlayerSelection/SelectedPlayer/EloMeter.tsx b/app/components/Homepage/AddGame/AddGameModal/Content/PlayerSelection/SelectedPlayer/EloMeter.tsx index 17393ebb..fcbe9bca 100644 --- a/app/components/Homepage/AddGame/AddGameModal/Content/PlayerSelection/SelectedPlayer/EloMeter.tsx +++ b/app/components/Homepage/AddGame/AddGameModal/Content/PlayerSelection/SelectedPlayer/EloMeter.tsx @@ -3,15 +3,18 @@ import { memo } from 'react' import GaugeChart from 'react-gauge-chart' import type { Player } from '@common/types' +import useSeasonalMode from '@hooks/useSeasonalMode' import styles from './SelectedPlayer.module.scss' // API: https://github.com/Martin36/react-gauge-chart const EloMeter = ({ player }: { player: Player }) => { + const { seasonal } = useSeasonalMode() + const elo = seasonal ? player.seasonElo ?? 400 : player.elo const MIN_ELO = 0 const MAX_ELO = 800 - const pointerPosition = player.elo / MAX_ELO + const pointerPosition = elo / MAX_ELO return (
@@ -26,7 +29,7 @@ const EloMeter = ({ player }: { player: Player }) => { nrOfLevels={20} percent={pointerPosition} /> - {round(player.elo, 2)} + {round(elo, 2)} {MIN_ELO} {MAX_ELO}
diff --git a/app/components/Profile/ProfileStats/index.tsx b/app/components/Profile/ProfileStats/index.tsx index 20e9cc14..f86b5d9e 100644 --- a/app/components/Profile/ProfileStats/index.tsx +++ b/app/components/Profile/ProfileStats/index.tsx @@ -2,6 +2,7 @@ import { max, round } from 'lodash' import { ComponentProps } from 'react' import { FiCalendar, FiFolder, FiPlay, FiTrendingUp } from 'react-icons/fi' +import { DEFAULT_ELO } from '@common/utils/constants' import { formatIsoStringToDate } from '@common/utils/helperFunctions' import { getPlayerDetailedGames, getPlayerStats } from '@server/db/games/derivatives' import { getPlayerById } from '@server/db/players' @@ -9,7 +10,6 @@ import { PlayerModel } from '@server/models' import ProfileStat from './ProfileStat' import styles from './ProfileStats.module.scss' -import { DEFAULT_ELO } from '@common/utils/constants' type DivProps = ComponentProps<'div'> @@ -63,7 +63,7 @@ const ProfileStats = async ({ playerId, ...props }: Props) => { Icon={FiCalendar} label="Seasonal" subStatistics={[ - { label: 'Fargo', value: player.seasonElo?.toString() ?? DEFAULT_ELO }, + { label: 'Fargo', value: round(player.seasonElo, 2).toFixed(2) ?? DEFAULT_ELO }, { label: 'Games', value: playerStats.seasonal.totalGames?.toString() ?? 0 }, { label: 'Win %', value: playerStats.seasonal.winPercentage?.toString() ?? 'NaN' }, ]} diff --git a/common/types/index.ts b/common/types/index.ts index 4706817a..7f4ea241 100644 --- a/common/types/index.ts +++ b/common/types/index.ts @@ -71,6 +71,13 @@ const recentGame = game.extend({ }) type RecentGame = z.infer +const addedGameResponse = z.object({ + recentGame: recentGame, + winner: player, + loser: player, +}) +type AddedGameResponse = z.infer + const mutualGames = z.object({ currentPlayerGamesWon: z.number().int().nonnegative(), opposingPlayerGamesWon: z.number().int().nonnegative(), @@ -160,6 +167,7 @@ export type { GameWithPlayers, NewGame, RecentGame, + AddedGameResponse, SubStatistic, ProfileStatistic, WinLossStats, @@ -185,6 +193,7 @@ export { playerWithStats, game, recentGame, + addedGameResponse, mutualGames, gameWithPlayers, newGame, diff --git a/server/db/games/index.ts b/server/db/games/index.ts index 9be31cf3..a3e21ba7 100644 --- a/server/db/games/index.ts +++ b/server/db/games/index.ts @@ -184,8 +184,14 @@ const removeLatestGame = async (): Promise => { id: latest.id, }, }), - updatePlayerById(latest.winnerId, { elo: latest.winnerEloBefore }), - updatePlayerById(latest.loserId, { elo: latest.loserEloBefore }), + updatePlayerById(latest.winnerId, { + elo: latest.winnerEloBefore, + seasonElo: latest.winnerSeasonEloBefore, + }), + updatePlayerById(latest.loserId, { + elo: latest.loserEloBefore, + seasonElo: latest.loserSeasonEloBefore, + }), ]) return latest