diff --git a/client/src/pages/ScuntLeaderboard/ScuntLeaderboard.jsx b/client/src/pages/ScuntLeaderboard/ScuntLeaderboard.jsx
index 0d292d9b..0b206879 100644
--- a/client/src/pages/ScuntLeaderboard/ScuntLeaderboard.jsx
+++ b/client/src/pages/ScuntLeaderboard/ScuntLeaderboard.jsx
@@ -33,32 +33,32 @@ const ScuntLeaderboard = () => {
socket.emit('getScores');
});
socket.on('scores', (scores) => {
- setLeaderboard(
- scores.map((team) => {
- if (team.points < 0) {
- team.points = 0;
- }
- return team;
- }),
- );
+ // Sort the scores before setting the leaderboard state
+ const sortedScores = scores.map((team) => ({
+ ...team,
+ points: team.points < 0 ? 0 : team.points
+ })).sort((a, b) => b.points - a.points); // Sort by points in descending order
+
+ setLeaderboard(sortedScores);
});
socket.on('update', (teamNumber, points) => {
setLeaderboard((prevLeaderboard) => {
- return prevLeaderboard.map((team) => {
- if (team.number === teamNumber) {
- team.points = points < 0 ? 0 : points;
- }
- return team;
- });
+ const updatedLeaderboard = prevLeaderboard.map((team) => ({
+ ...team,
+ points: team.number === teamNumber ? Math.max(points, 0) : team.points
+ }));
+ // Sort again after updating
+ return updatedLeaderboard.sort((a, b) => b.points - a.points);
});
});
-
+
return () => {
socket.off('connect');
socket.off('scores');
socket.disconnect();
};
}, []);
+
useEffect(() => {
if (scuntSettings) {
@@ -130,17 +130,17 @@ const ScuntLeaderboardShow = ({ leaderboard }) => {