From c535b495edf72a9b1fbe443f67b71dfb78d7d995 Mon Sep 17 00:00:00 2001 From: uo283055 Date: Wed, 1 May 2024 22:48:40 +0200 Subject: [PATCH] edits game --- webapp/src/components/Game.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/webapp/src/components/Game.js b/webapp/src/components/Game.js index fd83d6b0..07abcce7 100644 --- a/webapp/src/components/Game.js +++ b/webapp/src/components/Game.js @@ -9,6 +9,7 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { const [error, setError] = useState(''); const [correctQuestions, setCorrectQuestions] = useState(0); const [timer, setTimer] = useState(0); + const [themesSelected, setThemesSelected] = useState(themes); const [numberClics, setNumberClics] = useState(0); const [finished, setFinished] = useState(false); const [selectedAnswer, setSelectedAnswer] = useState(''); @@ -16,7 +17,7 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { const [almacenado, setAlmacenado] = useState(false); const pricePerQuestion = 25; const delayBeforeNextQuestion = 3000; // 3 segundos de retardo antes de pasar a la siguiente pregunta - const themesSelected = themes; + const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; function getRandomIndex(length) { @@ -57,7 +58,7 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { .map(([tema]) => tema); const randomIndex = getRandomIndex(temas.length); const temaAleatorio = temas[randomIndex]; - console.log(selectedAnswer); + const response = await axios.get(`${apiEndpoint}/getRandomQuestion${temaAleatorio}`); setQuestion(response.data); const respuestas = [...response.data.incorrectas, response.data.correcta]; @@ -69,7 +70,7 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { }; obtenerPreguntaAleatoria(); - }, [apiEndpoint, setQuestion, setRespuestasAleatorias, setError, themesSelected, selectedAnswer]); + }, [apiEndpoint, setQuestion, setRespuestasAleatorias, setError, themesSelected]); const handleTimeRemaining = () => { let minsR = Math.floor((timeLimit - timer) / 60); @@ -111,7 +112,7 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { const respuestas = [...response.data.incorrectas, response.data.correcta]; setRespuestasAleatorias(respuestas.sort(randomSort).slice(0, 4)); // Mostrar solo 4 respuestas } catch (error) { - console.error("Error al obtener la pregunta aleatoria", error); + console.error("Error al obtener la pregunta aleatoria "+selectedAnswer, error); setError('Error al obtener la pregunta aleatoria'); } }; @@ -158,6 +159,9 @@ const Game = ({ username, totalQuestions, timeLimit, themes }) => { }); } catch (error) { setError(error.response.data.error); + if (timeLimit>15000) { + setThemesSelected(themes); + } } };