From 50bd00b6cd469bd44a0fa5fbf3187b8ff683044a Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 18:33:00 +0200 Subject: [PATCH 1/9] Guardado de partida del modo Calculadora --- webapp/src/pages/Bateria/Bateria.js | 2 +- webapp/src/pages/Calculadora/Calculadora.js | 44 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/webapp/src/pages/Bateria/Bateria.js b/webapp/src/pages/Bateria/Bateria.js index cb866171..aa952d7f 100644 --- a/webapp/src/pages/Bateria/Bateria.js +++ b/webapp/src/pages/Bateria/Bateria.js @@ -65,7 +65,7 @@ const JuegoPreguntas = () => { }, 1000); return () => clearInterval(timer); // eslint-disable-next-line - }, [tiempoRestante]); + }, [tiempoRestante, preguntasCorrectas, preguntasFalladas]); useEffect(() => { if (juegoTerminado && tiempoMedio !== 0) { diff --git a/webapp/src/pages/Calculadora/Calculadora.js b/webapp/src/pages/Calculadora/Calculadora.js index 5572f7b3..9db5fa5f 100644 --- a/webapp/src/pages/Calculadora/Calculadora.js +++ b/webapp/src/pages/Calculadora/Calculadora.js @@ -3,6 +3,7 @@ import Nav from "../../components/Nav/Nav.js"; import Footer from "../../components/Footer/Footer.js"; import { Link } from "react-router-dom"; import { Box, Flex, Heading, Button, Input } from "@chakra-ui/react"; +import axios from 'axios'; const generateRandomOperation = () => { let operators = ["+", "-", "*", "/"]; @@ -44,16 +45,22 @@ const CalculadoraHumana = () => { const [juegoTerminado, setJuegoTerminado] = useState(false); const [progressPercent, setProgressPercent] = useState(100); + const [tiempoMedio, setTiempoMedio] = useState(0); + useEffect(() => { if (tiempoRestante === 0) { setJuegoTerminado(true); + if(puntuacion>0){ + const tMedio=TIME/puntuacion; + setTiempoMedio(tMedio); + } } const timer = setInterval(() => { setTiempoRestante((prevTiempo) => (prevTiempo <= 0 ? 0 : prevTiempo - 1)); }, 1000); return () => clearInterval(timer); // eslint-disable-next-line - }, [tiempoRestante]); + }, [tiempoRestante, puntuacion]); useEffect(() => { setProgressPercent((tiempoRestante / TIME) * 100); @@ -68,6 +75,41 @@ const CalculadoraHumana = () => { // eslint-disable-next-line }, [tiempoRestante]); + useEffect(() => { + if (juegoTerminado && tiempoMedio !== 0) { + guardarPartida(); + } + // eslint-disable-next-line + }, [juegoTerminado, tiempoMedio]); + + const guardarPartida = async () => { + + const username = localStorage.getItem("username"); + const newGame = { + username: username, + gameMode: "calculadora", + gameData: { + correctAnswers: 0, + incorrectAnswers: 0, + points: puntuacion, + avgTime: tiempoMedio, + }, + }; + try { + const response = await axios.post(URL + '/saveGame', newGame); + console.log("Solicitud exitosa:", response.data); + + } catch (error) { + console.error('Error al guardar el juego:', error); + } + try { + const response = await axios.post(URL + "/saveGameList", newGame); + console.log("Solicitud exitosa:", response.data); + } catch (error) { + console.error("Error al guardar el juego:", error); + } + } + const handleAnswer = (valSubmit) => { setValSubmit(""); valSubmit = Number(valSubmit); From a38898582a2be93c2200a8ba72240f55c3c45613 Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 18:39:23 +0200 Subject: [PATCH 2/9] =?UTF-8?q?Modificada=20la=20vista=20de=20estad=C3=ADs?= =?UTF-8?q?ticaas=20para=20las=20stats=20de=20Calculadora?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- statsservice/model/stats-getter.js | 2 +- statsservice/stats-service.js | 2 +- webapp/src/pages/Stats/Stats.js | 74 +++++++++++++++++------------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/statsservice/model/stats-getter.js b/statsservice/model/stats-getter.js index 405e885e..ab36a5db 100644 --- a/statsservice/model/stats-getter.js +++ b/statsservice/model/stats-getter.js @@ -4,7 +4,7 @@ class StatsForUser { async getStatsForUser(username,gamemode){ var statsJSON=null; - if(gamemode=="clasico" || gamemode=="bateria"){ + if(gamemode=="clasico" || gamemode=="bateria" || gamemode=="calculadora"){ statsJSON = await this.getStats(username,gamemode); } return statsJSON; diff --git a/statsservice/stats-service.js b/statsservice/stats-service.js index e8111f94..083fd2e7 100644 --- a/statsservice/stats-service.js +++ b/statsservice/stats-service.js @@ -27,7 +27,7 @@ app.post("/saveGame", async (req, res) => { const gamemode = req.body.gameMode; const gameData = req.body.gameData; - if (gamemode == "clasico" || gamemode == "bateria") { + if (gamemode == "clasico" || gamemode == "bateria" || gamemode =="calculadora") { // Buscar las estadísticas existentes del usuario y modo de juego let stats = await Stats.findOne({ username: username, gamemode: gamemode }); diff --git a/webapp/src/pages/Stats/Stats.js b/webapp/src/pages/Stats/Stats.js index 72d9f578..e21079cd 100644 --- a/webapp/src/pages/Stats/Stats.js +++ b/webapp/src/pages/Stats/Stats.js @@ -58,7 +58,6 @@ const Stats = () => { setGamemode(mode); fetchStats(mode); }; - const handleSearch = () => { fetchStats(gamemode); @@ -69,6 +68,8 @@ const Stats = () => { return "Clásico"; } else if (gamemode === "bateria") { return "Batería de sabios"; + } else if (gamemode === "calculadora") { + return "Calculadora humana"; } return gamemode; }; @@ -139,6 +140,12 @@ const Stats = () => { > Batería de sabios </Button> + <Button + className={gamemode === "calculadora" ? "active" : ""} + onClick={() => handleGamemodeChange("calculadora")} + > + Calculadora humana + </Button> </Flex> {stats === null && !isLoading && ( <p mt="10rem">El usuario no ha jugado ninguna partida.</p> @@ -162,36 +169,40 @@ const Stats = () => { </Td> <Td>{stats.avgPoints.toFixed(2)}</Td> </Tr> - <Tr> - <Td> - <strong>Puntos totales</strong> - </Td> - <Td>{stats.totalPoints}</Td> - </Tr> - <Tr> - <Td> - <strong>Preguntas correctas totales</strong> - </Td> - <Td>{stats.totalCorrectQuestions}</Td> - </Tr> - <Tr> - <Td> - <strong>Preguntas incorrectas totales</strong> - </Td> - <Td>{stats.totalIncorrectQuestions}</Td> - </Tr> - <Tr> - <Td> - <strong>Porcentaje de aciertos</strong> - </Td> - <Td>{stats.ratioCorrect.toFixed(2)}%</Td> - </Tr> - <Tr> - <Td> - <strong>Tiempo por pregunta (s):</strong> - </Td> - <Td>{stats.avgTime.toFixed(2)}</Td> - </Tr> + <Tr> + <Td> + <strong>Puntos totales</strong> + </Td> + <Td>{stats.totalPoints}</Td> + </Tr> + {gamemode !== "calculadora" && ( + <> + <Tr> + <Td> + <strong>Preguntas correctas totales</strong> + </Td> + <Td>{stats.totalCorrectQuestions}</Td> + </Tr> + <Tr> + <Td> + <strong>Preguntas incorrectas totales</strong> + </Td> + <Td>{stats.totalIncorrectQuestions}</Td> + </Tr> + <Tr> + <Td> + <strong>Porcentaje de aciertos</strong> + </Td> + <Td>{stats.ratioCorrect.toFixed(2)}%</Td> + </Tr> + </> + )} + <Tr> + <Td> + <strong>Tiempo por pregunta (s):</strong> + </Td> + <Td>{stats.avgTime.toFixed(2)}</Td> + </Tr> </Tbody> </Table> </div> @@ -203,3 +214,4 @@ const Stats = () => { }; export default Stats; + From 33cb9a6852ad49c4c5fb59f8eda59317538c40bf Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 18:41:46 +0200 Subject: [PATCH 3/9] Modificada la vista de ranking para calculadora --- webapp/src/pages/Ranking/Ranking.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/webapp/src/pages/Ranking/Ranking.js b/webapp/src/pages/Ranking/Ranking.js index c472ac65..75582fd9 100644 --- a/webapp/src/pages/Ranking/Ranking.js +++ b/webapp/src/pages/Ranking/Ranking.js @@ -39,6 +39,8 @@ const Ranking = () => { return "Clásico"; } else if(gamemode === "bateria"){ return "Batería de sabios"; + } else if(gamemode === "calculadora"){ + return "Calculadora humana"; } return gamemode; }; @@ -59,6 +61,7 @@ const Ranking = () => { case "totalPoints": return "Puntos totales"; case "ratioCorrect": + if (gamemode === "calculadora") return null; return "Ratio de aciertos (%)"; case "avgTime": return "Tiempo por pregunta (s)"; @@ -74,6 +77,7 @@ const Ranking = () => { case "totalPoints": return stat.totalPoints; case "ratioCorrect": + if (gamemode === "calculadora") return null; return Math.round(stat.ratioCorrect * 100) / 100; case "avgTime": return Math.round(stat.avgTime * 100) / 100; @@ -127,6 +131,12 @@ const Ranking = () => { > Batería de sabios </Button> + <Button + className={gamemode === "calculadora" ? "active" : ""} + onClick={() => handleGamemodeChange("calculadora")} + > + Calculadora humana + </Button> <Table> <Thead> <Tr> @@ -152,3 +162,4 @@ const Ranking = () => { export default Ranking; + From 7e69608d80cf7d58530a401e62bfc38b7f71ae8b Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:22:31 +0200 Subject: [PATCH 4/9] Arreglos en el guardado de partida --- webapp/src/pages/Calculadora/Calculadora.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/webapp/src/pages/Calculadora/Calculadora.js b/webapp/src/pages/Calculadora/Calculadora.js index 9db5fa5f..cf0e38aa 100644 --- a/webapp/src/pages/Calculadora/Calculadora.js +++ b/webapp/src/pages/Calculadora/Calculadora.js @@ -37,6 +37,7 @@ function findDivisors(num) { const CalculadoraHumana = () => { const TIME = 60; + const URL = process.env.REACT_APP_API_ENDPOINT || "http://localhost:8000"; const [valSubmit, setValSubmit] = useState(""); const [puntuacion, setPuntuacion] = useState(0); @@ -120,6 +121,10 @@ const CalculadoraHumana = () => { let newOperation = generateOperation(valSubmit); setOperation(newOperation); } else { + if(puntuacion>0){ + const tMedio=TIME/puntuacion; + setTiempoMedio(tMedio); + } setJuegoTerminado(true); } }; From 34397157a6f454d627ff17898a1de2c6163e5b39 Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:36:06 +0200 Subject: [PATCH 5/9] Ya se guardan las partidas bien --- statsservice/stats-service.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/statsservice/stats-service.js b/statsservice/stats-service.js index 083fd2e7..ee899598 100644 --- a/statsservice/stats-service.js +++ b/statsservice/stats-service.js @@ -30,8 +30,11 @@ app.post("/saveGame", async (req, res) => { if (gamemode == "clasico" || gamemode == "bateria" || gamemode =="calculadora") { // Buscar las estadísticas existentes del usuario y modo de juego let stats = await Stats.findOne({ username: username, gamemode: gamemode }); - if (!stats) { + var ratioCorrect=0; + if(gameData.incorrectAnswers + gameData.correctAnswers>0){ + ratioCorrect=(gameData.correctAnswers / (gameData.incorrectAnswers + gameData.correctAnswers)) * 100; + } // Si no existen estadísticas, crear un nuevo documento stats = new Stats({ username: username, @@ -41,10 +44,9 @@ app.post("/saveGame", async (req, res) => { totalPoints: gameData.points, totalCorrectQuestions: gameData.correctAnswers, totalIncorrectQuestions: gameData.incorrectAnswers, - ratioCorrect: (gameData.correctAnswers / (gameData.incorrectAnswers + gameData.correctAnswers)) * 100, + ratioCorrect: ratioCorrect, avgTime: gameData.avgTime, }); - await stats.save(); } else { From 842e56c0ce67e9eb353acbafe890eef45c9ba4eb Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:38:45 +0200 Subject: [PATCH 6/9] Ajustes en la interfaz de Perfil --- webapp/src/pages/Perfil/Perfil.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/webapp/src/pages/Perfil/Perfil.js b/webapp/src/pages/Perfil/Perfil.js index a03f733c..31c31cca 100644 --- a/webapp/src/pages/Perfil/Perfil.js +++ b/webapp/src/pages/Perfil/Perfil.js @@ -69,13 +69,13 @@ const Perfil = () => { </Thead> <Tbody> {userData.games.slice(0, 10).map((game, index) => ( - <Tr key={index}> - <Td>{game.gamemode}</Td> - <Td>{game.correctAnswers}</Td> - <Td>{game.incorrectAnswers}</Td> - <Td>{game.points}</Td> - <Td>{parseFloat(game.avgTime).toFixed(2)} segundos</Td> - </Tr> + <Tr key={index}> + <Td>{game.gamemode}</Td> + <Td>{game.gamemode === 'calculadora' ? '-' : game.correctAnswers}</Td> + <Td>{game.gamemode === 'calculadora' ? '-' : game.incorrectAnswers}</Td> + <Td>{game.points}</Td> + <Td>{parseFloat(game.avgTime).toFixed(2)} segundos</Td> + </Tr> ))} </Tbody> </Table> From e8e3529714bf799b3452675ea0b4947e7a2e45cc Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:40:23 +0200 Subject: [PATCH 7/9] Ajustes en el filtrado de Ranking --- webapp/src/pages/Ranking/Ranking.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/webapp/src/pages/Ranking/Ranking.js b/webapp/src/pages/Ranking/Ranking.js index 75582fd9..60cbb89f 100644 --- a/webapp/src/pages/Ranking/Ranking.js +++ b/webapp/src/pages/Ranking/Ranking.js @@ -115,9 +115,11 @@ const Ranking = () => { <Flex flexDirection="column" rowGap="1rem"> <Heading as="h2">Ranking - modo {getModeName()}</Heading> <Select id="displaySelector" onChange={handleDisplayChange}> - {displayOptions.map(option => ( - <option key={option.value} value={option.value}>{option.label}</option> - ))} + {displayOptions.map(option => { + if (gamemode === "calculadora" && option.value === "ratioCorrect") { + } + return <option key={option.value} value={option.value}>{option.label}</option>; + })} </Select> <Button className={gamemode === "clasico" ? "active" : ""} From 153fe5858d9818e83b98d2d028f5e198804f729d Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:41:36 +0200 Subject: [PATCH 8/9] =?UTF-8?q?A=C3=B1adido=20return=20null=20que=20si=20n?= =?UTF-8?q?o=20obviamente=20no=20va?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/pages/Ranking/Ranking.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webapp/src/pages/Ranking/Ranking.js b/webapp/src/pages/Ranking/Ranking.js index 60cbb89f..b7b05589 100644 --- a/webapp/src/pages/Ranking/Ranking.js +++ b/webapp/src/pages/Ranking/Ranking.js @@ -117,6 +117,7 @@ const Ranking = () => { <Select id="displaySelector" onChange={handleDisplayChange}> {displayOptions.map(option => { if (gamemode === "calculadora" && option.value === "ratioCorrect") { + return null; } return <option key={option.value} value={option.value}>{option.label}</option>; })} From 07935ce1346cb4d5dd42963b55ef60b05f6b5fe2 Mon Sep 17 00:00:00 2001 From: iyanfdezz <uo288231@uniovi.es> Date: Fri, 5 Apr 2024 19:47:09 +0200 Subject: [PATCH 9/9] Arreglo en el calculo de tiempo --- webapp/src/pages/Calculadora/Calculadora.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/pages/Calculadora/Calculadora.js b/webapp/src/pages/Calculadora/Calculadora.js index cf0e38aa..cad42ae2 100644 --- a/webapp/src/pages/Calculadora/Calculadora.js +++ b/webapp/src/pages/Calculadora/Calculadora.js @@ -122,7 +122,7 @@ const CalculadoraHumana = () => { setOperation(newOperation); } else { if(puntuacion>0){ - const tMedio=TIME/puntuacion; + const tMedio=(TIME-tiempoRestante)/puntuacion; setTiempoMedio(tMedio); } setJuegoTerminado(true);