diff --git a/src/app/stream-card/[id]/page.tsx b/src/app/stream-card/[id]/page.tsx new file mode 100644 index 0000000..5571747 --- /dev/null +++ b/src/app/stream-card/[id]/page.tsx @@ -0,0 +1,123 @@ +'use client' + +import { RANKED_CHANNEL } from '@/shared/constants' +import { api } from '@/trpc/react' +import { ArrowDown, ArrowUp } from 'lucide-react' +import { useParams } from 'next/navigation' + +export default function LeaderboardCard() { + const { id } = useParams() + if (!id || typeof id !== 'string') { + return null + } + + const gamesQuery = api.history.user_games.useQuery({ user_id: id }) + const games = gamesQuery?.data || [] // Ensure games is always an array + + const { data: rankedUserRank } = api.leaderboard.get_user_rank.useQuery({ + channel_id: RANKED_CHANNEL, + user_id: id, + }) + + if (!rankedUserRank || !games?.length) { + return null + } + const filteredGamesByLeaderboard = games.filter( + (game) => game.gameType === 'ranked' + ) + + const games_played = filteredGamesByLeaderboard.length + let wins = 0 + let losses = 0 + let ties = 0 + for (const game of filteredGamesByLeaderboard) { + if (game.result === 'win') { + wins++ + } else if (game.result === 'loss') { + losses++ + } else if (game.result === 'tie') { + ties++ + } else { + ties++ + } + } + + const lastGame = filteredGamesByLeaderboard.at(0) + + const currentName = lastGame?.playerName + const meaningful_games = games_played - ties + const playerData = { + username: currentName, + games: games_played, + meaningful_games, + wins, + losses, + ties, + winRate: + meaningful_games > 0 ? Math.ceil((wins / meaningful_games) * 100) : 0, + lossRate: + meaningful_games > 0 ? Math.floor((losses / meaningful_games) * 100) : 0, + rank: rankedUserRank.rank, + mmr: Math.round(rankedUserRank.mmr), + mmrChange: `${(lastGame?.mmrChange ?? 0) >= 0 ? '+' : '-'}${Math.round(lastGame?.mmrChange ?? 0)}`, + streak: rankedUserRank?.streak, + } + return ( +
+
+ {playerData.rank} +
+ + {/* Player Name */} +
+
{playerData.username}
+
+ + {/* MMR */} +
+
MMR:
+
{playerData.mmr}
+
{playerData.mmrChange}
+
+ + {/* Win Rate */} +
+
Win Rate:
+
+ {playerData.winRate}% +
+
+ + {/* Win/Loss */} +
+
+ +
+ {playerData.wins} +
+
+
+ +
+ {playerData.losses} +
+
+
+ + {/* Streak */} +
+
Streak:
+
{playerData.streak}
+
+ + {/* Live Indicator */} +
+
+ Live +
+
+ ) +}