Skip to content

Commit

Permalink
Refactor accuracy calculation in statistics-service.js and update tes…
Browse files Browse the repository at this point in the history
…t cases
  • Loading branch information
UO285267 committed Apr 27, 2024
1 parent 5e8065d commit e6e071a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
11 changes: 9 additions & 2 deletions statistics/statisticsservice/statistics-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions statistics/statisticsservice/statistics-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});


Expand Down
1 change: 1 addition & 0 deletions webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion webapp/src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Navbar = () => {

<li className="nav-item">
<NavLink to={process.env.RUTA_LOGIN || '/login'} className="nav__link">
Login
Logout
</NavLink>
</li>

Expand Down

0 comments on commit e6e071a

Please sign in to comment.