Skip to content

Commit

Permalink
stat-service added improved
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Nov 17, 2024
1 parent f634ff3 commit 830716e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions questionservice/question-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const questionSchema = new mongoose.Schema({
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});

const Question = mongoose.model('Question', questionSchema);
Expand Down
4 changes: 4 additions & 0 deletions statservice/stat-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const statSchema = new mongoose.Schema({
type: Number,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});

const Stat = mongoose.model('Stat', statSchema);
Expand Down
39 changes: 34 additions & 5 deletions webapp/src/pages/PlayGame1.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import Wrapper from '../assets/wrappers/Play';
import QuestionContainer from '../components/QuestionContainer';
import ScoreContainer from '../components/ScoreContainer';
import TimerContainer from '../components/TimerContainer';
import GameOverContainer from '../components/GameOverContainer';
import useScore from '../hooks/useScore';
import { v4 as uuidv4 } from 'uuid';

const apiEndpoint =
process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';
const answerTime = 20;

const PlayGame1 = ({ questions }) => {
// Estados
const [questionIndex, setQuestionIndex] = useState(0);
const [shuffledAnswers, setShuffledAnswers] = useState([]); // Estado para almacenar las respuestas mezcladas
const [score, updateScore] = useScore();
const [seconds, setSeconds] = useState(20); // Estados para controlar el temporizador
const [isActive, setIsActive] = useState(true);
const [previousScore, setPreviousScore] = useState(0);
const [seconds, setSeconds] = useState(answerTime); // Estados para controlar el temporizador
const [isActive, setIsActive] = useState(false);
const [isTimeOut, setIsTimeOut] = useState(false);
const [gameOver, setGameOver] = useState(false);
const [error, setError] = useState('');
const [gameId] = useState(() => uuidv4());

// Desestructuramos la pregunta
const { name, path, right, wrong1, wrong2, wrong3 } =
const { _id, name, path, right, wrong1, wrong2, wrong3 } =
questions[questionIndex];

// Efecto para mezclar las respuestas al montar el componente
Expand Down Expand Up @@ -46,12 +55,32 @@ const PlayGame1 = ({ questions }) => {
setIsActive(state);
};
const restartTimer = () => {
setSeconds(20);
setSeconds(answerTime);
setIsTimeOut(false);
};

const addQuestionStat = async () => {
try {
const usedTime = answerTime - seconds;
const points = score - previousScore;
const right = points === 300;
setPreviousScore(score);
await axios.post(`${apiEndpoint}/addstat`, {
userId: 'user',
gameId: gameId,
questionId: _id,
right: right,
time: usedTime,
points: points,
});
} catch (error) {
setError(error.response.data.error);
}
};

// Función para cargar la siguiente pregunta
const loadNextQuestion = () => {
const loadNextQuestion = async () => {
await addQuestionStat();
if (questionIndex < questions.length - 1) {
// Si no estamos en la última pregunta, avanzamos a la siguiente
setQuestionIndex((prevIndex) => prevIndex + 1);
Expand Down

0 comments on commit 830716e

Please sign in to comment.