From c2978f04021468c2c189fc771255db6725a6df7b Mon Sep 17 00:00:00 2001 From: UO289659 Date: Mon, 8 Apr 2024 17:10:50 +0200 Subject: [PATCH 01/10] =?UTF-8?q?Arreglado=20bus=20estad=C3=ADsticas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/components/QuizGame.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/webapp/src/components/QuizGame.js b/webapp/src/components/QuizGame.js index 680c5f72..daa7763f 100644 --- a/webapp/src/components/QuizGame.js +++ b/webapp/src/components/QuizGame.js @@ -161,6 +161,17 @@ const QuizGame = () => { const handleTimeOver = () => { setIsFinished(true); + const username=localStorage.getItem('username') + const rigthAnswers = answeredQuestions.filter(question => question.isCorrect).length; + const wrongAnswers=numberOfQuestions+1-rigthAnswers; + const completedTime = totalTime - time; + const statisticsData = { + username: username, + rigthAnswers: rigthAnswers, + wrongAnswers:wrongAnswers, + time:completedTime + }; + saveStatistics(statisticsData); alert('¡Tiempo agotado!'); }; From 89c288b962902cce930fe76a59ea84391a52bce6 Mon Sep 17 00:00:00 2001 From: UO289659 Date: Mon, 8 Apr 2024 20:00:17 +0200 Subject: [PATCH 02/10] =?UTF-8?q?Interfaz=20ranking,=20backend=20y=20front?= =?UTF-8?q?end=20para=20escoger=20m=C3=A9trica=20por=20la=20que=20se=20qui?= =?UTF-8?q?ere=20ordenar.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gatewayservice/gateway-service.js | 18 ++++++ .../statisticsservice/statistics-service.js | 35 +++++++++++ webapp/src/components/Navbar.js | 8 +++ webapp/src/components/Ranking.js | 62 +++++++++++++++++++ webapp/src/components/Routes.js | 13 ++++ webapp/src/components/package.json | 11 ++++ 6 files changed, 147 insertions(+) create mode 100644 webapp/src/components/Ranking.js create mode 100644 webapp/src/components/package.json diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 31131742..654472e5 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -110,6 +110,24 @@ app.get('/users', async (req, res) => { } }); +app.get('/ranking/accuracy', async (req, res) => { + try { + const statResponse = await axios.get(statisticssServiceUrl+'/ranking/accuracy'); + res.json(statResponse.data); + } catch (error) { + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); + +app.get('/ranking/correct-answers', async (req, res) => { + try { + const statResponse = await axios.get(statisticssServiceUrl+'/ranking/correct-answers'); + res.json(statResponse.data); + } catch (error) { + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); + // Read the OpenAPI YAML file synchronously openapiPath='./openapi.yaml' diff --git a/statistics/statisticsservice/statistics-service.js b/statistics/statisticsservice/statistics-service.js index 021ac8e3..a26bccbc 100644 --- a/statistics/statisticsservice/statistics-service.js +++ b/statistics/statisticsservice/statistics-service.js @@ -58,6 +58,41 @@ app.post('/addStatistic', async (req, res) => { }); + + app.get('/ranking/accuracy', async (req, res) => { + try { + const users = await Statistic.find(); // Obtener todos los usuarios + const rankedUsers = users.map(user => { + const accuracy = user.rigthAnswers / user.gamesPlayed; // Calcular porcentaje de aciertos + return { username: user.username, accuracy }; // Crear objeto con nombre de usuario y porcentaje de aciertos + }); + const sortedRanking = rankedUsers.sort((a, b) => b.accuracy - a.accuracy); // Ordenar usuarios por porcentaje de aciertos + res.json(sortedRanking); // Devolver ranking ordenado + } catch (err) { + res.status(500).json({ message: err.message }); + } + }); + + app.get('/ranking/correct-answers', async (req, res) => { + try { + const users = await Statistic.find(); // Obtener todos los usuarios + // Mapear los usuarios para devolver solo las propiedades necesarias + const rankedUsers = users.map(user => ({ + username: user.username, + correctAnswers: user.rigthAnswers, // Cambiar el nombre de la propiedad si es necesario + totalQuestions: user.rigthAnswers + user.wrongAnswers // Calcular total de preguntas acertadas + })); + // Ordenar usuarios por respuestas correctas + const sortedRanking = rankedUsers.sort((a, b) => b.correctAnswers - a.correctAnswers); + res.json(sortedRanking); // Devolver ranking ordenado + } catch (err) { + res.status(500).json({ message: err.message }); + } + }); + + + + const server = app.listen(port, () => { console.log(`Statistics Service listening at http://localhost:${port}`); diff --git a/webapp/src/components/Navbar.js b/webapp/src/components/Navbar.js index b4d9dac1..c0c64d8a 100644 --- a/webapp/src/components/Navbar.js +++ b/webapp/src/components/Navbar.js @@ -15,6 +15,14 @@ const Navbar = () => { id="nav-menu" >