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 = () => {
  • Usuarios registrados
  • +
  • + ScoreBoard +
  • diff --git a/webapp/src/components/ScoreBoard.js b/webapp/src/components/ScoreBoard.js new file mode 100644 index 00000000..5c9c4042 --- /dev/null +++ b/webapp/src/components/ScoreBoard.js @@ -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 ( + + +
    +

    Tabla de Puntuaciones

    + + + + + + + + + + + {scoreboard.map((user, index) => ( + + + + + + + ))} + +
    UsuarioPreguntas Totales AcertadasPreguntas Totales FalladasPuntos
    {user.username}{user.totalCorrect}{user.totalIncorrect}{user.points}
    +
    +
    + ); +}; + +export default ScoreBoard; diff --git a/webapp/src/index.js b/webapp/src/index.js index b3b8cc0f..b2d639f9 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -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'; @@ -26,6 +27,7 @@ root.render( }> }> }> + }>