diff --git a/statistics/statisticsservice/statistics-service.js b/statistics/statisticsservice/statistics-service.js index 33387251..e613030a 100644 --- a/statistics/statisticsservice/statistics-service.js +++ b/statistics/statisticsservice/statistics-service.js @@ -63,9 +63,16 @@ app.post('/addStatistic', async (req, res) => { try { const users = await Statistic.find(); // Obtener todos los usuarios 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 + let roundedAccuracy; + if(user.wrongAnswers === 0 & user.rigthAnswers !=0) return { username: user.username, accuracy: 100 }; // Evitar divisiĆ³n por cero (no se han jugado partidas + const accuracy = (user.rigthAnswers / (user.wrongAnswers + user.rigthAnswers )) * 100; // Calcular porcentaje de aciertos + 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 diff --git a/statistics/statisticsservice/statistics-service.test.js b/statistics/statisticsservice/statistics-service.test.js index 5b863285..504e6b9e 100644 --- a/statistics/statisticsservice/statistics-service.test.js +++ b/statistics/statisticsservice/statistics-service.test.js @@ -110,16 +110,16 @@ describe('GET /ranking/accuracy', () => { expect(response.status).toBe(200); // Verificar el cuerpo de la respuesta para testuser - expect(response.body[0]).toHaveProperty('username', 'testuser'); + expect(response.body[0]).toHaveProperty('username', 'user1'); expect(response.body[0]).toHaveProperty('accuracy', 100); // Verificar el cuerpo de la respuesta para user1 - expect(response.body[1]).toHaveProperty('username', 'user1'); - expect(response.body[1]).toHaveProperty('accuracy', 8); + expect(response.body[1]).toHaveProperty('username', 'user2'); + expect(response.body[1]).toHaveProperty('accuracy', 100); // Verificar el cuerpo de la respuesta para user2 - expect(response.body[2]).toHaveProperty('username', 'user2'); - expect(response.body[2]).toHaveProperty('accuracy', '7.50'); + expect(response.body[2]).toHaveProperty('username', 'testuser'); + expect(response.body[2]).toHaveProperty('accuracy', '71.43'); }); diff --git a/webapp/package-lock.json b/webapp/package-lock.json index b17b8793..dbad338c 100644 --- a/webapp/package-lock.json +++ b/webapp/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "webapp", "version": "0.1.0", "dependencies": { "@emotion/react": "^11.11.3", diff --git a/webapp/src/components/Navbar.js b/webapp/src/components/Navbar.js index 55b14635..0ff86ab9 100644 --- a/webapp/src/components/Navbar.js +++ b/webapp/src/components/Navbar.js @@ -32,7 +32,7 @@ const Navbar = () => {
  • - Login + Logout