Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arreglado detalle ranking #173

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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