-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from thutasann/feat/type-race
feat: scoreboard
- Loading branch information
Showing
8 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Progress } from '@chakra-ui/react'; | ||
import React from 'react'; | ||
import { PlayerProps } from '../../types'; | ||
import { Alert, AlertIcon, AlertTitle, useToast } from '@chakra-ui/react'; | ||
|
||
interface IProgress { | ||
players: PlayerProps[]; | ||
player: PlayerProps; | ||
wordsLength: number; | ||
} | ||
|
||
function calPercentage(player: PlayerProps, wordsLength: number): number { | ||
if (player.currentWordIndex !== 0) { | ||
return Number(((player.currentWordIndex! / wordsLength) * 100).toFixed(2)); | ||
} | ||
return 0; | ||
} | ||
|
||
const CustomProgress: React.FC<IProgress> = ({ | ||
players, | ||
player, | ||
wordsLength, | ||
}) => { | ||
const percentage = calPercentage(player, wordsLength); | ||
console.log('players', players); | ||
|
||
return ( | ||
<div className="w-[90%] mt-[50px] border-t pt-[50px]"> | ||
<h2 className="text-center mb-7 text-2xl font-[600]">Progresses</h2> | ||
<div className="flex items-center justify-between font-[500]"> | ||
<h5> | ||
{player.nickName} <span className="font-[600]">(You)</span> | ||
</h5> | ||
<span>{percentage} %</span> | ||
</div> | ||
<Progress | ||
value={percentage} | ||
rounded="md" | ||
colorScheme="teal" | ||
/> | ||
|
||
{/* Oponents */} | ||
<div className="mt-7"> | ||
{players?.map((playerObj) => { | ||
const percentage = calPercentage(playerObj, wordsLength); | ||
return playerObj._id !== player._id ? ( | ||
<div key={playerObj._id}> | ||
<div className="flex items-center justify-between font-[500]"> | ||
<h5>{playerObj.nickName}</h5> | ||
<span>{percentage} %</span> | ||
</div> | ||
<Progress | ||
value={percentage} | ||
rounded="md" | ||
colorScheme="teal" | ||
/> | ||
</div> | ||
) : null; | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CustomProgress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import { | ||
Table, | ||
Thead, | ||
Tbody, | ||
Tfoot, | ||
Tr, | ||
Th, | ||
Td, | ||
TableCaption, | ||
TableContainer, | ||
} from '@chakra-ui/react'; | ||
|
||
import { PlayerProps } from '../../types'; | ||
|
||
interface IScoreBoard { | ||
players: PlayerProps[]; | ||
} | ||
|
||
function getScoreBoard(players: PlayerProps[]): PlayerProps[] { | ||
const scoreboard = players.filter((player) => player.WPM !== -1); | ||
return scoreboard.sort((a, b) => | ||
a.WPM > b.WPM ? -1 : b.WPM > a.WPM ? 1 : 0, | ||
); | ||
} | ||
|
||
const ScoreBoard: React.FC<IScoreBoard> = ({ players }) => { | ||
const scoreBoard = getScoreBoard(players); | ||
|
||
if (scoreBoard.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="my-[50px] w-[90%]"> | ||
<h2 className="text-center mb-3 text-2xl font-[600]">Scoreboard</h2> | ||
<div> | ||
<TableContainer | ||
border="1px solid lightGray" | ||
rounded="md" | ||
p={5} | ||
> | ||
<Table variant="striped"> | ||
<Thead> | ||
<Tr> | ||
<Th>Player</Th> | ||
<Th>Words Per Minutes (WPM)</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
{scoreBoard?.map((player) => ( | ||
<Tr key={player._id}> | ||
<Td>{player.nickName}</Td> | ||
<Td>{player.WPM}</Td> | ||
</Tr> | ||
))} | ||
</Tbody> | ||
</Table> | ||
</TableContainer> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ScoreBoard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8f63496
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
chat-t – ./
chat-t-thutasann.vercel.app
mern-t-chat.vercel.app
chat-t-git-main-thutasann.vercel.app