|
| 1 | +import Link from 'next/link' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | + |
| 4 | +import { PlayerStats } from 'src/types' |
| 5 | +import { getPlayerStats } from 'src/api' |
| 6 | + |
| 7 | +interface Props { |
| 8 | + index: number |
| 9 | + typeId: 'regular' | 'train' | 'turing' | 'hand' | 'brain' |
| 10 | + type: 'Regular' | 'Train' | 'Bot/Not' | 'Hand' | 'Brain' |
| 11 | + display_name: string |
| 12 | + elo: number |
| 13 | +} |
| 14 | + |
| 15 | +export const LeaderboardEntry = ({ |
| 16 | + typeId, |
| 17 | + type, |
| 18 | + index, |
| 19 | + display_name, |
| 20 | + elo, |
| 21 | +}: Props) => { |
| 22 | + const [hover, setHover] = useState(false) |
| 23 | + const [popup, setPopup] = useState(false) |
| 24 | + const [stats, setStats] = useState<PlayerStats | null>(null) |
| 25 | + |
| 26 | + let ratingKey: |
| 27 | + | 'regularRating' |
| 28 | + | 'trainRating' |
| 29 | + | 'botNotRating' |
| 30 | + | 'handRating' |
| 31 | + | 'brainRating' |
| 32 | + let highestRatingKey: |
| 33 | + | 'regularMax' |
| 34 | + | 'trainMax' |
| 35 | + | 'botNotMax' |
| 36 | + | 'handMax' |
| 37 | + | 'brainMax' |
| 38 | + let gamesKey: |
| 39 | + | 'regularGames' |
| 40 | + | 'trainGames' |
| 41 | + | 'botNotCorrect' |
| 42 | + | 'handGames' |
| 43 | + | 'brainGames' |
| 44 | + |
| 45 | + let gamesWonKey: |
| 46 | + | 'regularWins' |
| 47 | + | 'trainCorrect' |
| 48 | + | 'botNotCorrect' |
| 49 | + | 'handWins' |
| 50 | + | 'brainWins' |
| 51 | + |
| 52 | + switch (typeId) { |
| 53 | + case 'regular': |
| 54 | + ratingKey = 'regularRating' |
| 55 | + highestRatingKey = 'regularMax' |
| 56 | + gamesKey = 'regularGames' |
| 57 | + gamesWonKey = 'regularWins' |
| 58 | + break |
| 59 | + case 'train': |
| 60 | + ratingKey = 'trainRating' |
| 61 | + highestRatingKey = 'trainMax' |
| 62 | + gamesKey = 'trainGames' |
| 63 | + gamesWonKey = 'trainCorrect' |
| 64 | + break |
| 65 | + case 'turing': |
| 66 | + ratingKey = 'botNotRating' |
| 67 | + highestRatingKey = 'botNotMax' |
| 68 | + gamesKey = 'botNotCorrect' |
| 69 | + gamesWonKey = 'botNotCorrect' |
| 70 | + break |
| 71 | + case 'hand': |
| 72 | + ratingKey = 'handRating' |
| 73 | + highestRatingKey = 'handMax' |
| 74 | + gamesKey = 'handGames' |
| 75 | + gamesWonKey = 'handWins' |
| 76 | + break |
| 77 | + case 'brain': |
| 78 | + ratingKey = 'brainRating' |
| 79 | + highestRatingKey = 'brainMax' |
| 80 | + gamesKey = 'brainGames' |
| 81 | + gamesWonKey = 'brainWins' |
| 82 | + break |
| 83 | + default: |
| 84 | + ratingKey = 'regularRating' |
| 85 | + highestRatingKey = 'regularMax' |
| 86 | + gamesKey = 'regularGames' |
| 87 | + gamesWonKey = 'regularWins' |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + let timer: NodeJS.Timeout |
| 93 | + if (hover) { |
| 94 | + timer = setTimeout(() => { |
| 95 | + fetchStats() |
| 96 | + }, 300) |
| 97 | + } else { |
| 98 | + setPopup(false) |
| 99 | + } |
| 100 | + |
| 101 | + return () => clearTimeout(timer) |
| 102 | + }, [hover]) |
| 103 | + |
| 104 | + const fetchStats = async () => { |
| 105 | + try { |
| 106 | + const playerStats = await getPlayerStats(display_name) |
| 107 | + setStats(playerStats) |
| 108 | + setPopup(true) |
| 109 | + } catch (error) { |
| 110 | + console.error(error) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <div |
| 116 | + className={`relative flex w-full items-center justify-between px-6 py-2 ${index % 2 === 0 ? 'bg-background-1/90' : 'bg-background-1/50'}`} |
| 117 | + onMouseEnter={() => setHover(true)} |
| 118 | + onMouseLeave={() => setHover(false)} |
| 119 | + > |
| 120 | + <div className="flex items-center gap-2"> |
| 121 | + <p className="w-5">{index + 1}</p> |
| 122 | + <Link |
| 123 | + href={`/profile/${display_name}`} |
| 124 | + className="flex items-center gap-2 hover:underline" |
| 125 | + > |
| 126 | + <p> |
| 127 | + {display_name} {index == 0 && '👑'} |
| 128 | + </p> |
| 129 | + </Link> |
| 130 | + </div> |
| 131 | + <p>{elo}</p> |
| 132 | + {popup && stats && ( |
| 133 | + <div className="absolute left-0 top-[100%] z-20 flex w-full max-w-[26rem] flex-col overflow-hidden rounded border border-white/10 bg-background-1"> |
| 134 | + <div className="flex w-full justify-between bg-backdrop/50 px-4 py-2"> |
| 135 | + <p> |
| 136 | + <span className="font-bold">{display_name}</span>'s {type}{' '} |
| 137 | + Statistics |
| 138 | + </p> |
| 139 | + <Link href={`/profile/${display_name}`}> |
| 140 | + <i className="material-symbols-outlined select-none text-lg text-primary hover:text-human-1"> |
| 141 | + open_in_new |
| 142 | + </i> |
| 143 | + </Link> |
| 144 | + </div> |
| 145 | + <div className="flex items-center justify-between px-4 py-2"> |
| 146 | + <div className="flex flex-col items-center justify-center gap-0.5 text-human-1"> |
| 147 | + <p className="text-sm xl:text-base">Rating</p> |
| 148 | + <b className="text-3xl xl:text-3xl">{stats[ratingKey]}</b> |
| 149 | + </div> |
| 150 | + <div className="flex flex-col items-center justify-center gap-0.5"> |
| 151 | + <p className="text-sm xl:text-base">Highest</p> |
| 152 | + <b className="text-3xl xl:text-3xl">{stats[highestRatingKey]}</b> |
| 153 | + </div> |
| 154 | + <div className="flex flex-col items-center justify-center gap-0.5"> |
| 155 | + <p className="text-sm xl:text-base">Games</p> |
| 156 | + <b className="text-3xl xl:text-3xl">{stats[gamesKey]}</b> |
| 157 | + </div> |
| 158 | + <div className="flex flex-col items-center justify-center gap-0.5"> |
| 159 | + <p className="text-sm xl:text-base">Win %</p> |
| 160 | + <b className="text-3xl xl:text-3xl"> |
| 161 | + {((stats[gamesWonKey] / stats[gamesKey]) * 100).toFixed(0)}% |
| 162 | + </b> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + ) |
| 169 | +} |
0 commit comments