From 15e4e92e07f62bebe01dcd29764cd1ecba72b379 Mon Sep 17 00:00:00 2001 From: UO289659 Date: Thu, 4 Apr 2024 20:53:03 +0200 Subject: [PATCH 1/3] =?UTF-8?q?A=C3=B1adido=20a=20la=20vista=20de=20estad?= =?UTF-8?q?=C3=ADsticas=20tiempo=20medio=20del=20total=20de=20partidas.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- statistics/statisticsservice/statistics-service.js | 10 ++++++---- statistics/statisticsservice/user-model.js | 8 ++++++++ users/userservice/user-model.js | 8 ++++++++ webapp/src/components/QuizGame.js | 5 ++++- webapp/src/components/Statistics.js | 2 ++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/statistics/statisticsservice/statistics-service.js b/statistics/statisticsservice/statistics-service.js index 4c45881e..c00b2046 100644 --- a/statistics/statisticsservice/statistics-service.js +++ b/statistics/statisticsservice/statistics-service.js @@ -21,11 +21,11 @@ mongoose.connect(mongoUri); // Function to validate required fields in the request body function validateRequiredFields(req, requiredFields) { - for (const field of requiredFields) { - if (!(field in req.body)) { - throw new Error(`Missing required field: ${field}`); - } + for (const field of requiredFields) { + if (!(field in req.body)) { + throw new Error(`Missing required field: ${field}`); } + } } app.post('/addStatistic', async (req, res) => { @@ -36,6 +36,8 @@ app.post('/addStatistic', async (req, res) => { userStatistics.gamesPlayed++; // Incrementar el contador de juegos jugados userStatistics.rigthAnswers += req.body.rigthAnswers; // Sumar las respuestas correctas userStatistics.wrongAnswers += req.body.wrongAnswers; // Sumar las respuestas incorrectas + userStatistics.totalTime+=req.body.time; + userStatistics.avgTime=(userStatistics.totalTime/userStatistics.gamesPlayed); await userStatistics.save(); // Guardar las estadísticas actualizadas en la base de datos res.json(userStatistics); diff --git a/statistics/statisticsservice/user-model.js b/statistics/statisticsservice/user-model.js index 7124659f..a4ce0e2b 100644 --- a/statistics/statisticsservice/user-model.js +++ b/statistics/statisticsservice/user-model.js @@ -25,6 +25,14 @@ const userSchema = new mongoose.Schema({ type:Number, default:0, }, + totalTime:{ + type:Number, + default:0, + }, + avgTime:{ + type:Number, + default:0, + }, }); diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 7124659f..a4ce0e2b 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -25,6 +25,14 @@ const userSchema = new mongoose.Schema({ type:Number, default:0, }, + totalTime:{ + type:Number, + default:0, + }, + avgTime:{ + type:Number, + default:0, + }, }); diff --git a/webapp/src/components/QuizGame.js b/webapp/src/components/QuizGame.js index 8614d7d4..680c5f72 100644 --- a/webapp/src/components/QuizGame.js +++ b/webapp/src/components/QuizGame.js @@ -100,6 +100,8 @@ const QuizGame = () => { if (questionsNumber === numberOfQuestions) { const rigthAnswers = answeredQuestions.filter(question => question.isCorrect).length; const wrongAnswers=numberOfQuestions+1-rigthAnswers; + const completedTime = totalTime - time; + console.log(completedTime) setTimeout(() => { setIsFinished(true); }, 1000); @@ -107,7 +109,8 @@ const QuizGame = () => { const statisticsData = { username: username, rigthAnswers: rigthAnswers, - wrongAnswers:wrongAnswers + wrongAnswers:wrongAnswers, + time:completedTime }; saveStatistics(statisticsData); } diff --git a/webapp/src/components/Statistics.js b/webapp/src/components/Statistics.js index a17b9c0e..230d67cb 100644 --- a/webapp/src/components/Statistics.js +++ b/webapp/src/components/Statistics.js @@ -32,6 +32,7 @@ const Statistics= () => { Partidas Jugadas Preguntas Acertadas Preguntas Falladas + Tiempo Medio @@ -39,6 +40,7 @@ const Statistics= () => { {userData.gamesPlayed} {userData.rigthAnswers} {userData.wrongAnswers} + {userData.avgTime} From 5cb90cd7e2f559d5bfa19ca54ae52f53891dd726 Mon Sep 17 00:00:00 2001 From: UO289659 Date: Thu, 4 Apr 2024 20:57:54 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Actualizados=20test=20de=20estad=C3=ADstica?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/components/Statistics.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/webapp/src/components/Statistics.test.js b/webapp/src/components/Statistics.test.js index c93218e0..8f533d9e 100644 --- a/webapp/src/components/Statistics.test.js +++ b/webapp/src/components/Statistics.test.js @@ -21,7 +21,8 @@ describe('Statistics component', () => { const userData = { gamesPlayed: 10, rigthAnswers: 7, - wrongAnswers: 3 + wrongAnswers: 3, + avgTime: 56 }; // Simulamos el usuario almacenado en localStorage @@ -46,7 +47,7 @@ const table = await screen.findByRole('table'); expect(table).toBeInTheDocument(); // Verificar que la tabla contiene las columnas esperadas -const columnHeaders = ['Partidas Jugadas', 'Preguntas Acertadas', 'Preguntas Falladas']; +const columnHeaders = ['Partidas Jugadas', 'Preguntas Acertadas', 'Preguntas Falladas', 'Tiempo Medio']; const headerElements = screen.getAllByRole('columnheader'); columnHeaders.forEach(headerText => { expect(headerElements.some(header => header.textContent === headerText)).toBeTruthy(); @@ -58,10 +59,11 @@ expect(tableRows.length).toBeGreaterThan(1); // Verificar que hay más de una fi const dataRows = tableRows.slice(1); // Ignorar la primera fila que son los encabezados dataRows.forEach(row => { const cells = row.querySelectorAll('td'); // Obtener todas las celdas de la fila - expect(cells.length).toBe(3); // Verificar que hay exactamente 3 celdas por fila + expect(cells.length).toBe(4); // Verificar que hay exactamente 3 celdas por fila expect(cells[0]).toHaveTextContent('10'); // Verificar que la primera celda contiene el número de partidas jugadas expect(cells[1]).toHaveTextContent('7'); // Verificar que la segunda celda contiene el número de preguntas acertadas expect(cells[2]).toHaveTextContent('3'); // Verificar que la tercera celda contiene el número de preguntas falladas + expect(cells[3]).toHaveTextContent('56'); // Verificar que la tercera celda contiene el tiempo medio }); }); From d6d2982021adab0ce8e2cdd867874621ebe3591a Mon Sep 17 00:00:00 2001 From: UO289659 Date: Thu, 4 Apr 2024 22:51:26 +0200 Subject: [PATCH 3/3] Detalle tiempo --- webapp/src/components/Statistics.js | 2 +- webapp/src/components/Statistics.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/webapp/src/components/Statistics.js b/webapp/src/components/Statistics.js index 230d67cb..32815130 100644 --- a/webapp/src/components/Statistics.js +++ b/webapp/src/components/Statistics.js @@ -32,7 +32,7 @@ const Statistics= () => { Partidas Jugadas Preguntas Acertadas Preguntas Falladas - Tiempo Medio + Tiempo Medio (s) diff --git a/webapp/src/components/Statistics.test.js b/webapp/src/components/Statistics.test.js index 8f533d9e..dcdcec71 100644 --- a/webapp/src/components/Statistics.test.js +++ b/webapp/src/components/Statistics.test.js @@ -47,7 +47,7 @@ const table = await screen.findByRole('table'); expect(table).toBeInTheDocument(); // Verificar que la tabla contiene las columnas esperadas -const columnHeaders = ['Partidas Jugadas', 'Preguntas Acertadas', 'Preguntas Falladas', 'Tiempo Medio']; +const columnHeaders = ['Partidas Jugadas', 'Preguntas Acertadas', 'Preguntas Falladas', 'Tiempo Medio (s)']; const headerElements = screen.getAllByRole('columnheader'); columnHeaders.forEach(headerText => { expect(headerElements.some(header => header.textContent === headerText)).toBeTruthy();