Skip to content

Commit

Permalink
Merge pull request #173 from Arquisoft/UO289659
Browse files Browse the repository at this point in the history
Arreglado detalle ranking
  • Loading branch information
UO289659 authored Apr 27, 2024
2 parents 5e8065d + f288849 commit f3bfce6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions statistics/statisticsservice/statistics-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ app.post('/addStatistic', async (req, res) => {
app.get('/ranking/accuracy', async (req, res) => {
try {
const users = await Statistic.find(); // Obtener todos los usuarios
let roundedAccuracy=0;
const rankedUsers = users.map(user => {
const accuracy = (user.rigthAnswers / (user.gamesPlayed*10)) * 100; // Calcular porcentaje de aciertos
const roundedAccuracy = accuracy % 1 === 0 ? accuracy : accuracy.toFixed(2); // Redondear solo si tiene decimales
if (isNaN(accuracy)) {
roundedAccuracy = 0; // Asignar 0 si accuracy es NaN
} else {
roundedAccuracy = accuracy % 1 === 0 ? accuracy : accuracy.toFixed(2); // Redondear solo si tiene decimales
}
return { username: user.username, accuracy: roundedAccuracy }; // Crear objeto con nombre de usuario y porcentaje de aciertos redondeado si es necesario
});
});
const sortedRanking = rankedUsers.sort((a, b) => b.accuracy - a.accuracy); // Ordenar usuarios por porcentaje de aciertos
res.json(sortedRanking); // Devolver ranking ordenado
} catch (err) {
Expand Down

0 comments on commit f3bfce6

Please sign in to comment.