Skip to content

Commit

Permalink
ScoreBoard imlementado
Browse files Browse the repository at this point in the history
  • Loading branch information
UO290054 committed Apr 15, 2024
1 parent a3bb07f commit 04962bd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});



Expand Down
39 changes: 39 additions & 0 deletions questions/retrieveservice/retrieve-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
3 changes: 3 additions & 0 deletions webapp/src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const Navbar = () => {
<li className="nav-item">
<Link className="nav-link" to="/RegisteredUsers">Usuarios registrados</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/ScoreBoard">ScoreBoard</Link>
</li>
<li className="nav-item">
<button className="nav-link btn btn-link" onClick={handleLogout}>Logout</button>
</li>
Expand Down
59 changes: 59 additions & 0 deletions webapp/src/components/ScoreBoard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { Container, Button } from '@mui/material';

const ScoreBoard = () => {
const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';
const navigate = useNavigate();
const [scoreboard, setScoreboard] = useState([]);

useEffect(() => {
loadScoreboard();
}, []);

const loadScoreboard = async () => {
try {
const response = await axios.get(`${apiEndpoint}/getScoreBoard`);
setScoreboard(response.data);
} catch (error) {
console.error('Error:', error);
}
};
const handlePreviousPage = async () => {
let path = '/MainPage';
navigate(path);
};
return (
<Container component="main" maxWidth="md" sx={{ marginTop: 4 }}>
<Button variant="contained" color="primary" onClick={handlePreviousPage}>
Página anterior
</Button>
<div>
<h2>Tabla de Puntuaciones</h2>
<table>
<thead>
<tr>
<th>Usuario</th>
<th>Preguntas Totales Acertadas</th>
<th>Preguntas Totales Falladas</th>
<th>Puntos</th>
</tr>
</thead>
<tbody>
{scoreboard.map((user, index) => (
<tr key={index}>
<td>{user.username}</td>
<td>{user.totalCorrect}</td>
<td>{user.totalIncorrect}</td>
<td>{user.points}</td>
</tr>
))}
</tbody>
</table>
</div>
</Container>
);
};

export default ScoreBoard;
2 changes: 2 additions & 0 deletions webapp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import HistoricalData from './components/HistoricalData';
import MainPage from './components/MainPage';
import HistoricalUserData from './components/HistoricalUserData';
import RegisteredUsers from './components/RegisteredUsers';
import ScoreBoard from './components/ScoreBoard';

import './index.css';
import 'animate.css';
Expand All @@ -26,6 +27,7 @@ root.render(
<Route path="/historicaldata" element={<HistoricalData />}> </Route>
<Route path="/historicalUserdata" element={<HistoricalUserData />}> </Route>
<Route path="/RegisteredUsers" element={<RegisteredUsers />}> </Route>
<Route path="/ScoreBoard" element={<ScoreBoard />}> </Route>
</Routes>
</Router>
</React.StrictMode>
Expand Down

0 comments on commit 04962bd

Please sign in to comment.