feat: round scores and factor attempts into ranking system

This commit is contained in:
BillieFaiqul 2025-07-04 18:39:34 +07:00
parent bf65115b50
commit ba9b5a6d88

View File

@ -79,13 +79,22 @@ public function getRankingByProject(Request $request)
} }
} }
// Calculate percentage score // Calculate percentage score and round to nearest integer
$score = $totalTests > 0 ? ($passedTests / $totalTests) * 100 : 0; $score = $totalTests > 0 ? ($passedTests / $totalTests) * 100 : 0;
$score = round($score, 2); // Round to 2 decimal places $score = round($score, 0); // Round to nearest integer
// If we already processed this user, only keep the higher score // If we already processed this user, only keep the higher score
// If scores are equal, keep the one with fewer attempts
if (isset($processedUsers[$userId])) { if (isset($processedUsers[$userId])) {
$shouldReplace = false;
if ($score > $processedUsers[$userId]['score']) { if ($score > $processedUsers[$userId]['score']) {
$shouldReplace = true;
} elseif ($score == $processedUsers[$userId]['score'] && $submission->attempts < $processedUsers[$userId]['attempts']) {
$shouldReplace = true;
}
if ($shouldReplace) {
$processedUsers[$userId] = [ $processedUsers[$userId] = [
'user_id' => $userId, 'user_id' => $userId,
'user_name' => $submission->user_name, 'user_name' => $submission->user_name,
@ -109,14 +118,21 @@ public function getRankingByProject(Request $request)
} }
} }
// Convert to array and sort by score (descending) // Convert to array and sort by score (descending), then by attempts (ascending), then by submission date (ascending)
$rankings = array_values($processedUsers); $rankings = array_values($processedUsers);
usort($rankings, function($a, $b) { usort($rankings, function($a, $b) {
if ($a['score'] == $b['score']) { // First priority: score (higher is better)
// If scores are equal, sort by submission date (earlier is better) if ($a['score'] != $b['score']) {
return strtotime($a['submission_date']) - strtotime($b['submission_date']);
}
return $b['score'] - $a['score']; return $b['score'] - $a['score'];
}
// Second priority: attempts (fewer is better)
if ($a['attempts'] != $b['attempts']) {
return $a['attempts'] - $b['attempts'];
}
// Third priority: submission date (earlier is better)
return strtotime($a['submission_date']) - strtotime($b['submission_date']);
}); });
// Add rank information // Add rank information