From 50bd00b6cd469bd44a0fa5fbf3187b8ff683044a Mon Sep 17 00:00:00 2001 From: iyanfdezz 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 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 + {stats === null && !isLoading && (

El usuario no ha jugado ninguna partida.

@@ -162,36 +169,40 @@ const Stats = () => { {stats.avgPoints.toFixed(2)} - - - Puntos totales - - {stats.totalPoints} - - - - Preguntas correctas totales - - {stats.totalCorrectQuestions} - - - - Preguntas incorrectas totales - - {stats.totalIncorrectQuestions} - - - - Porcentaje de aciertos - - {stats.ratioCorrect.toFixed(2)}% - - - - Tiempo por pregunta (s): - - {stats.avgTime.toFixed(2)} - + + + Puntos totales + + {stats.totalPoints} + + {gamemode !== "calculadora" && ( + <> + + + Preguntas correctas totales + + {stats.totalCorrectQuestions} + + + + Preguntas incorrectas totales + + {stats.totalIncorrectQuestions} + + + + Porcentaje de aciertos + + {stats.ratioCorrect.toFixed(2)}% + + + )} + + + Tiempo por pregunta (s): + + {stats.avgTime.toFixed(2)} + @@ -203,3 +214,4 @@ const Stats = () => { }; export default Stats; + From 33cb9a6852ad49c4c5fb59f8eda59317538c40bf Mon Sep 17 00:00:00 2001 From: iyanfdezz 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 + @@ -152,3 +162,4 @@ const Ranking = () => { export default Ranking; + From 7e69608d80cf7d58530a401e62bfc38b7f71ae8b Mon Sep 17 00:00:00 2001 From: iyanfdezz 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 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 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 = () => { {userData.games.slice(0, 10).map((game, index) => ( - - - - - - - + + + + + + + ))}
{game.gamemode}{game.correctAnswers}{game.incorrectAnswers}{game.points}{parseFloat(game.avgTime).toFixed(2)} segundos
{game.gamemode}{game.gamemode === 'calculadora' ? '-' : game.correctAnswers}{game.gamemode === 'calculadora' ? '-' : game.incorrectAnswers}{game.points}{parseFloat(game.avgTime).toFixed(2)} segundos
From e8e3529714bf799b3452675ea0b4947e7a2e45cc Mon Sep 17 00:00:00 2001 From: iyanfdezz 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 = () => { Ranking - modo {getModeName()}