@@ -9,8 +9,7 @@ const Challenge = require('../models/challenge.model');
99router . get ( '/leaderboard' , async ( req , res ) => {
1010 try {
1111 const teams = await Team . find ( { role : 'team' } )
12- . select ( 'teamName points completedChallenges' )
13- . sort ( '-points' )
12+ . select ( 'teamName points juryScore completedChallenges' )
1413 . lean ( ) ;
1514
1615 // Get all challenges to calculate progress percentage
@@ -22,8 +21,8 @@ router.get('/leaderboard', async (req, res) => {
2221 totalChallengePoints += challenge . points || 0 ;
2322 } ) ;
2423
25- // Calculate percentage and format response
26- const leaderboard = teams . map ( ( team , index ) => {
24+ // Calculate combined scores and format response
25+ const leaderboard = teams . map ( team => {
2726 // For progress bar, calculate based on points earned vs total possible points
2827 const progressPercentage = totalChallengePoints > 0
2928 ? Math . round ( ( team . points || 0 ) / totalChallengePoints * 100 )
@@ -32,14 +31,34 @@ router.get('/leaderboard', async (req, res) => {
3231 // Still keep track of completed challenges count
3332 const completedCount = team . completedChallenges ? team . completedChallenges . length : 0 ;
3433
34+ // Calculate normalized challenge score (50% of final score)
35+ const normalizedChallengeScore = totalChallengePoints > 0
36+ ? ( ( team . points || 0 ) / totalChallengePoints ) * 50
37+ : 0 ;
38+
39+ // Jury score (50% of final score)
40+ const juryScore = team . juryScore || 0 ;
41+
42+ // Calculate final combined score (challenge score + jury score)
43+ const finalScore = normalizedChallengeScore + juryScore ;
44+
3545 return {
36- rank : index + 1 ,
3746 teamName : team . teamName ,
38- points : team . points ,
47+ challengePoints : team . points || 0 ,
48+ juryScore : juryScore ,
49+ finalScore : parseFloat ( finalScore . toFixed ( 2 ) ) ,
3950 progress : progressPercentage ,
4051 completedCount
4152 } ;
4253 } ) ;
54+
55+ // Sort by final score in descending order
56+ leaderboard . sort ( ( a , b ) => b . finalScore - a . finalScore ) ;
57+
58+ // Add rank after sorting
59+ leaderboard . forEach ( ( team , index ) => {
60+ team . rank = index + 1 ;
61+ } ) ;
4362
4463 res . json ( {
4564 success : true ,
0 commit comments