diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 4c82eac9..0a6a749f 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -66,6 +66,14 @@ app.get('/getgamehistory/:username', async (req, res) => { res.status(error.response.status).json({ error: error.response.data.error }); } }); +app.get('/getScoreBoard', async (req, res) => { + try { + const userResponse = await axios.get(`${retrieveServiceUrl}/getScoreBoard`); + res.json(userResponse.data); + } catch (error) { + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); diff --git a/questions/retrieveservice/retrieve-service.js b/questions/retrieveservice/retrieve-service.js index c0b233f4..56bd9af3 100644 --- a/questions/retrieveservice/retrieve-service.js +++ b/questions/retrieveservice/retrieve-service.js @@ -79,6 +79,45 @@ app.get('/getgamehistory/:username', async (req, res) => { }); } }); +app.get('/getScoreBoard', async (req, res) => { + try { + // Obtener todas las partidas + const games = await Game.find({}); + + // Objeto para almacenar el scoreboard + const scoreboard = {}; + + // Calcular el scoreboard para cada usuario + games.forEach(game => { + if (!scoreboard[game.username]) { + scoreboard[game.username] = { + username: game.username, + totalCorrect: 0, + totalIncorrect: 0, + points: 0 + }; + } + + // Sumar el número total de preguntas acertadas y falladas + scoreboard[game.username].totalCorrect += game.correctAnswers; + scoreboard[game.username].totalIncorrect += game.incorrectAnswers; + + // Calcular los puntos totales + scoreboard[game.username].points += (game.correctAnswers * 15) - (game.incorrectAnswers * 5); + }); + + // Convertir el objeto de scoreboard en un array de objetos + const scoreboardArray = Object.values(scoreboard); + + // Enviar la respuesta con el scoreboard + res.json(scoreboardArray); + } catch (error) { + res.status(400).json({ + error: error.message + }); + } +}); + const server = app.listen(port, () => { console.log(`Creation Service listening at http://localhost:${port}`); diff --git a/webapp/src/components/Navbar.js b/webapp/src/components/Navbar.js index 013ab731..280766c4 100644 --- a/webapp/src/components/Navbar.js +++ b/webapp/src/components/Navbar.js @@ -42,6 +42,9 @@ const Navbar = () => {
Usuario | +Preguntas Totales Acertadas | +Preguntas Totales Falladas | +Puntos | +
---|---|---|---|
{user.username} | +{user.totalCorrect} | +{user.totalIncorrect} | +{user.points} | +