Leaderboard WSS & types fix

This commit is contained in:
Théo 2023-04-18 16:30:35 +02:00
parent 5d1aec2c16
commit f41e8d6123
4 changed files with 53 additions and 42 deletions

View file

@ -68,7 +68,7 @@ export type ScoreEvent = {
{ {
pseudo: string; pseudo: string;
tries: number; tries: number;
completions: number; completion: number;
score: number; score: number;
} }
]; ];

View file

@ -14,6 +14,8 @@ const SCORE_COLORS = ['text-yellow-400', 'text-gray-400', 'text-orange-400'];
export default function Leaderboard({ token }: { token: string }) { export default function Leaderboard({ token }: { token: string }) {
// const { data, isLoading } = useLeaderboard({ token }); // const { data, isLoading } = useLeaderboard({ token });
const CHAPITRE_EVENT = 3;
const subscription: SWRSubscription<string, ScoreEvent, Error> = (key, { next }) => { const subscription: SWRSubscription<string, ScoreEvent, Error> = (key, { next }) => {
const socket = new WebSocket(key); const socket = new WebSocket(key);
@ -29,7 +31,7 @@ export default function Leaderboard({ token }: { token: string }) {
}; };
const { data } = useSWRSubscription( const { data } = useSWRSubscription(
`wss://${process.env.NEXT_PUBLIC_API_URL?.split('//')[1]}/rleaderboard`, `wss://${process.env.NEXT_PUBLIC_API_URL?.split('//')[1]}/rleaderboard${CHAPITRE_EVENT}`,
subscription subscription
); );
@ -39,9 +41,11 @@ export default function Leaderboard({ token }: { token: string }) {
const { data: groups } = useGroups({ token }); const { data: groups } = useGroups({ token });
console.log(groups);
if (groups) { if (groups) {
options = groups options = groups
.filter((group) => !group.chapter) .filter((group) => group.chapter === null)
.map((group) => ({ value: group.name, title: group.name })) .map((group) => ({ value: group.name, title: group.name }))
.filter((group, index, self) => self.findIndex((g) => g.value === group.value) === index) .filter((group, index, self) => self.findIndex((g) => g.value === group.value) === index)
.sort((a, b) => a.title.localeCompare(b.title)); .sort((a, b) => a.title.localeCompare(b.title));
@ -56,8 +60,6 @@ export default function Leaderboard({ token }: { token: string }) {
return data; return data;
}, [data, filter]); }, [data, filter]);
console.log(filteredData);
return ( return (
<section className="flex h-full w-full flex-col space-y-4"> <section className="flex h-full w-full flex-col space-y-4">
<header className="sticky flex items-center justify-between"> <header className="sticky flex items-center justify-between">
@ -82,40 +84,49 @@ export default function Leaderboard({ token }: { token: string }) {
)} */} )} */}
</header> </header>
<main className="flex flex-col justify-between space-x-0 space-y-4 pb-4"> <main className="flex flex-col justify-between space-x-0 space-y-4 pb-4">
Waiting for the leaderboard to be implemented (Xavier WSS) <ul className="flex flex-col space-y-2">
{/* <ul className="flex flex-col space-y-2"> {data?.groups.map((group, key) => (
{(!isLoading &&
filteredData((group, key) => (
<li key={key} className="flex justify-between space-x-2"> <li key={key} className="flex justify-between space-x-2">
<div className="flex items-center space-x-4"> <div className="flex items-center space-x-4">
<span className={cn('font-semibold', SCORE_COLORS[score.rank - 1])}> <span className={cn('font-semibold', SCORE_COLORS[group.rank - 1])}>
{score.rank} {group.rank}
</span> </span>
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<AvatarComponent name={score.pseudo} src={score.avatar} className="h-9 w-9" />
<div className="flex flex-col gap-x-2 sm:flex-row sm:items-center"> <div className="flex flex-col gap-x-2 sm:flex-row sm:items-center">
<span className="text-lg">{score.pseudo}</span> <span className="text-lg">{group.name}</span>
<span className="text-sm text-muted"> <span className="text-sm text-muted">
{score.groups {group.players && group.players.length > 1
?.map((g) => g.name) ? group.players
.map((player) => player.pseudo || 'Anonyme')
.sort((a, b) => a.localeCompare(b)) .sort((a, b) => a.localeCompare(b))
.join(', ')} .join(', ')
: group.players[0].pseudo}
</span> </span>
</div> </div>
</div> </div>
</div> </div>
<div className="flex items-center space-x-4"> <div className="flex items-center space-x-4">
<div className="flex flex-col"> <div className="flex flex-col">
<span className="text-sm font-semibold">Puzzles</span> <span className="text-sm font-semibold">Essaies</span>
<span className="text-lg text-muted">{score.completions}</span> <span className="text-lg text-muted">
{group.players.reduce((a, b) => a + b.tries, 0)}
</span>
</div> </div>
{/* <div className="flex flex-col">
<span className="text-sm font-semibold">Puzzles</span>
<span className="text-lg text-muted">
{group.players.reduce((a, b) => a + b.completion, 0)}
</span>
</div> */}
<div className="flex flex-col"> <div className="flex flex-col">
<span className="text-sm font-semibold">Score</span> <span className="text-sm font-semibold">Score</span>
<span className="text-lg text-muted">{score.score}</span> <span className="text-lg text-muted">
{group.players.reduce((a, b) => a + b.score, 0)}
</span>
</div> </div>
</div> </div>
</li> </li>
))) || )) ||
[...Array(20).keys()].map((i) => ( [...Array(20).keys()].map((i) => (
<span <span
key={i} key={i}
@ -126,7 +137,7 @@ export default function Leaderboard({ token }: { token: string }) {
}} }}
/> />
))} ))}
</ul> */} </ul>
</main> </main>
</section> </section>
); );

View file

@ -116,11 +116,11 @@ export default function Puzzle({ token, id }: { token: string; id: number }) {
) : ( ) : (
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<div className=" items-center gap-x-2"> <div className=" items-center gap-x-2">
<p className="text-sm"> <p>
Tentatives : <span className="text-brand">{puzzle.tries}</span> Tentatives : <span className="text-brand-accent">{puzzle.tries}</span>
</p> </p>
<p className="text-sm"> <p>
Score : <span className="text-brand">{puzzle.score}</span> (Score maximum:{' '} Score : <span className="text-brand-accent">{puzzle.score}</span> (Score maximum:{' '}
{puzzle.scoreMax}) {puzzle.scoreMax})
</p> </p>
</div> </div>

View file

@ -13,7 +13,7 @@ export default function ToHTML({ data, className }: { data: string; className?:
a: ({ ...props }) => ( a: ({ ...props }) => (
<a <a
{...props} {...props}
className="text-brand hover:text-brand/80 hover:underline" className="text-brand-accent hover:text-brand hover:underline"
// MAKE thIS SHIT DOWNLOAD // MAKE thIS SHIT DOWNLOAD
target="_blank" target="_blank"
rel="noopener" rel="noopener"