Skip to content

Commit 21ea07b

Browse files
committed
Introduce basic leaderboard
1 parent cbab0bc commit 21ea07b

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

web/src/api/progress.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ export const usePartyProgress = (party_id: string) => {
5454
return codesTried;
5555
}, [events]);
5656

57+
const triedCodesByUserId = useMemo(() => {
58+
const codesTried = new Map<string, string[]>();
59+
60+
for (const page of events?.pages ?? []) {
61+
for (const event of page) {
62+
if (event.data.type === 'PartyCodesSubmitted') {
63+
const userCodes = codesTried.get(event.user_id) ?? [];
64+
65+
for (const code of event.data.codes) {
66+
userCodes.push(code);
67+
}
68+
69+
codesTried.set(event.user_id, userCodes);
70+
}
71+
}
72+
}
73+
74+
return codesTried;
75+
}, [events]);
76+
5777
const totalCodes = codes.length;
5878

5979
const percentages = useMemo(() => {
@@ -63,5 +83,6 @@ export const usePartyProgress = (party_id: string) => {
6383
return {
6484
percentages,
6585
triedCodes,
86+
triedCodesByUserId,
6687
};
6788
};

web/src/components/party/codes/PartyStats.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import { FC } from 'react';
1+
import { FC, useMemo } from 'react';
22

33
import { usePartyProgress } from '@/api/progress';
44

55
export const PartyStats: FC<{ party_id: string }> = ({ party_id }) => {
6-
const { percentages, triedCodes } = usePartyProgress(party_id);
6+
const { percentages, triedCodes, triedCodesByUserId } = usePartyProgress(party_id);
7+
8+
const userTopCodes = useMemo(() => {
9+
const userTopCodes: [string, number][] = [];
10+
11+
for (const [userId, codes] of triedCodesByUserId.entries()) {
12+
userTopCodes.push([userId, codes.length]);
13+
}
14+
15+
userTopCodes.sort((a, b) => b[1] - a[1]);
16+
17+
return userTopCodes;
18+
}, [triedCodesByUserId]);
719

820
return (
921
<div className="card w-full flex flex-col gap-2 !pb-2" style={{ gridColumnEnd: '-2' }}>
@@ -23,6 +35,19 @@ export const PartyStats: FC<{ party_id: string }> = ({ party_id }) => {
2335
</span>
2436
<span>{triedCodes.size}</span>
2537
</div>
38+
<div>
39+
<span>
40+
Leaderboard
41+
</span>
42+
<ul className="border">
43+
{userTopCodes.map(([userId, codes]) => (
44+
<li key={userId} className="flex items-center justify-between">
45+
<span>{userId}</span>
46+
<span>{codes}</span>
47+
</li>
48+
))}
49+
</ul>
50+
</div>
2651
</div>
2752
<div className="w-full h-4 bg-primary rounded-md border border-secondary overflow-hidden mb-2">
2853
<div className="h-full bg-accent" style={{ width: `${percentages}%` }}>

0 commit comments

Comments
 (0)