Skip to content

Commit

Permalink
Merge pull request #157 from Prodeko/main
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
nlinnanen authored Nov 21, 2023
2 parents ff2d4ce + 969e0ab commit 49fdd43
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
8 changes: 6 additions & 2 deletions app/api/game/route.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -15,6 +15,7 @@ interface Props {
}

const GameCreation = ({ setGames, onClose }: Props) => {
const { seasonal } = useSeasonalMode()
const [{ game }, dispatchModal] = useModalState()
const [_, dispatchQueue] = useQueueState()

Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className={styles.gauge}>
Expand All @@ -26,7 +29,7 @@ const EloMeter = ({ player }: { player: Player }) => {
nrOfLevels={20}
percent={pointerPosition}
/>
<span className={styles.currentelo}>{round(player.elo, 2)}</span>
<span className={styles.currentelo}>{round(elo, 2)}</span>
<span className={styles.minelo}>{MIN_ELO}</span>
<span className={styles.maxelo}>{MAX_ELO}</span>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/components/Profile/ProfileStats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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'
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'>

Expand Down Expand Up @@ -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' },
]}
Expand Down
9 changes: 9 additions & 0 deletions common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ const recentGame = game.extend({
})
type RecentGame = z.infer<typeof recentGame>

const addedGameResponse = z.object({
recentGame: recentGame,
winner: player,
loser: player,
})
type AddedGameResponse = z.infer<typeof addedGameResponse>

const mutualGames = z.object({
currentPlayerGamesWon: z.number().int().nonnegative(),
opposingPlayerGamesWon: z.number().int().nonnegative(),
Expand Down Expand Up @@ -160,6 +167,7 @@ export type {
GameWithPlayers,
NewGame,
RecentGame,
AddedGameResponse,
SubStatistic,
ProfileStatistic,
WinLossStats,
Expand All @@ -185,6 +193,7 @@ export {
playerWithStats,
game,
recentGame,
addedGameResponse,
mutualGames,
gameWithPlayers,
newGame,
Expand Down
10 changes: 8 additions & 2 deletions server/db/games/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,14 @@ const removeLatestGame = async (): Promise<GameModel> => {
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
Expand Down

0 comments on commit 49fdd43

Please sign in to comment.