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/HistoricalUserData.js b/webapp/src/components/HistoricalUserData.js index c83e3abc..5b8d0d44 100644 --- a/webapp/src/components/HistoricalUserData.js +++ b/webapp/src/components/HistoricalUserData.js @@ -8,6 +8,7 @@ const HistoricalUserData = () => { const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; const [gameHistory, setGameHistory] = useState([]); + const [expandedRows, setExpandedRows] = useState([]); useEffect(() => { @@ -19,25 +20,33 @@ const HistoricalUserData = () => { const handleLoadHistory = async () => { try { const username = localStorage.getItem('username'); - const response = await axios.get(`${apiEndpoint}/getgamehistory/${username}`); + const response = await axios.get(`${apiEndpoint}/getgamehistory/${username}`); - // Ordenar la lista de historial de partidas por fecha (de más reciente a más antigua) - const sortedHistory = response.data.sort((a, b) => new Date(b.date) - new Date(a.date)); - - setGameHistory(sortedHistory); - - console.log("el historial actual es "+gameHistory); + // Ordenar la lista de historial de partidas por fecha (de más reciente a más antigua) + const sortedHistory = response.data.sort((a, b) => new Date(b.date) - new Date(a.date)); + setGameHistory(sortedHistory); } catch (error) { console.error('Error:', error); } }; - const handlePreviousPage = async () => { - let path= '/MainPage'; - navigate(path); - } + const handlePreviousPage = async () => { + let path = '/MainPage'; + navigate(path); + }; + const toggleRow = (index) => { + const newExpandedRows = [...expandedRows]; + if (newExpandedRows.includes(index)) { + // Si la fila ya está expandida, la contraemos + newExpandedRows.splice(newExpandedRows.indexOf(index), 1); + } else { + // Si la fila no está expandida, la expandimos + newExpandedRows.push(index); + } + setExpandedRows(newExpandedRows); + }; return ( @@ -59,9 +68,9 @@ const HistoricalUserData = () => { - {gameHistory.map((game) => ( + {gameHistory.map((game, index) => ( - + toggleRow(index)}> {game.date} {game.duration} {game.percentage}% @@ -69,10 +78,10 @@ const HistoricalUserData = () => { {game.correctAnswers} {game.incorrectAnswers} - {game.questions && game.questions.map((question, index) => ( - + {expandedRows.includes(index) && game.questions && game.questions.map((question, qIndex) => ( + -

Pregunta {index + 1}: {question.question}

+

Pregunta {qIndex + 1}: {question.question}

Respuesta Correcta: {question.correctAnswer}

Respuesta del Usuario: {question.userAnswer}

La respuesta fue: {question.correctAnswer === question.userAnswer ? 'Correcta' : 'Incorrecta'}

@@ -86,7 +95,7 @@ const HistoricalUserData = () => {
); - + }; export default HistoricalUserData; diff --git a/webapp/src/components/Navbar.js b/webapp/src/components/Navbar.js index 661a54dd..280766c4 100644 --- a/webapp/src/components/Navbar.js +++ b/webapp/src/components/Navbar.js @@ -1,14 +1,20 @@ import React, { useState } from 'react'; -import { Link } from 'react-router-dom'; +import { Link ,useNavigate} from 'react-router-dom'; import './Navbar.css'; const Navbar = () => { const [historialDropdownOpen, setHistorialDropdownOpen] = useState(false); - + const navigate = useNavigate(); const toggleHistorialDropdown = () => { setHistorialDropdownOpen(!historialDropdownOpen); }; + const handleLogout = () => { + localStorage.removeItem('username'); + navigate("/"); // Redirige a la página de inicio de sesión + }; + + return (