Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uo289659 #109

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions statistics/statisticsservice/statistics-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions statistics/statisticsservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const userSchema = new mongoose.Schema({
type:Number,
default:0,
},
totalTime:{
type:Number,
default:0,
},
avgTime:{
type:Number,
default:0,
},

});

Expand Down
8 changes: 8 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const userSchema = new mongoose.Schema({
type:Number,
default:0,
},
totalTime:{
type:Number,
default:0,
},
avgTime:{
type:Number,
default:0,
},

});

Expand Down
5 changes: 4 additions & 1 deletion webapp/src/components/QuizGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ 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);
const username=localStorage.getItem('username')
const statisticsData = {
username: username,
rigthAnswers: rigthAnswers,
wrongAnswers:wrongAnswers
wrongAnswers:wrongAnswers,
time:completedTime
};
saveStatistics(statisticsData);
}
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/components/Statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ const Statistics= () => {
<th>Partidas Jugadas</th>
<th>Preguntas Acertadas</th>
<th>Preguntas Falladas</th>
<th>Tiempo Medio (s)</th>
</tr>
</thead>
<tbody>
<tr>
<td>{userData.gamesPlayed}</td>
<td>{userData.rigthAnswers}</td>
<td>{userData.wrongAnswers}</td>
<td>{userData.avgTime}</td>
</tr>
</tbody>
</table>
Expand Down
8 changes: 5 additions & 3 deletions webapp/src/components/Statistics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (s)'];
const headerElements = screen.getAllByRole('columnheader');
columnHeaders.forEach(headerText => {
expect(headerElements.some(header => header.textContent === headerText)).toBeTruthy();
Expand All @@ -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
});

});
Expand Down
Loading