Skip to content

Commit

Permalink
Merge pull request #76 from Arquisoft/Marina
Browse files Browse the repository at this point in the history
Corrección bug al guardar datos de las partidas.
  • Loading branch information
UO288559 authored Mar 27, 2024
2 parents 7c13315 + 471e7cb commit 73dc301
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 59 deletions.
101 changes: 43 additions & 58 deletions questiongenerator/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var question = "";
var url = 'https://query.wikidata.org/sparql';
var questionToSave = null;
var gameId = null;
var numberOfQuestions = 0;
// Todas las consultas
var queries = [`SELECT ?question ?questionLabel ?option ?optionLabel
WHERE {
Expand Down Expand Up @@ -64,14 +65,22 @@ mongoose.connect(mongoUri);

app.get('/generateQuestion', async (req, res) => {
try {
const createNewGame = await req.query.newGame;
if(numberOfQuestions == 0){
gameId = null;
}
const user = req.query.user;
await generarPregunta();

numberOfQuestions++;
if(numberOfQuestions>=5){
numberOfQuestions = 0;
}

var id = await saveData();
console.log("CREATE NEW GAME ANTES DE IF DE GAMEID: " + createNewGame);
gameId = (createNewGame === true) ? null : gameId;
await saveGame(user, id, createNewGame);
// Construcción de la respuesta

await saveGame(user, id);


var response = {
responseQuestion: question,
responseOptions: options,
Expand Down Expand Up @@ -148,73 +157,49 @@ function procesarDatos(data) {

}

async function saveGame(username,id,createNewGame){
async function saveGame(username,id) {

console.log("HAY QUE CREAR UN NUEVO JUEGO ? " + createNewGame);
console.log( "GAME ID: " + gameId);
if (gameId === null) {

if(gameId === null){
try {
const newGame = new Game({userId: username, questions: []});
newGame.questions.push(questionToSave._id);
await newGame.save();
gameId = newGame._id;
return null;
} catch (error) {
console.error("Error al guardar datos de la partida: " + error);
}
} else {
const existingGame = await Game.findById(gameId);

try{
console.log("primer if");
console.log("ID DE LA PREGUNTA: " + id);
console.log("PREGUNTA: " + questionToSave);
console.log("PREGUNTA ID: " + questionToSave._id);
const newGame = new Game({ userId: username, questions: [] });
if (!existingGame) {

try {
const newGame = new Game({userId: username, questions: []});
newGame.questions.push(questionToSave._id);
await newGame.save();
console.log(" ID AL AÑADIR: " + newGame._id);
gameId = newGame._id;
console.log( " EYEYYEY GAME ID: " + gameId);
return null;
}catch (error){
} catch (error) {
console.error("Error al guardar datos de la partida: " + error);
}
}else{
console.log("HAY QUE CREAR UN NUEVO JUEGO ? " + createNewGame);
console.log("primer else");
const existingGame = await Game.findById(gameId);

if(!existingGame){

try{
console.log("segundo if");
console.log("ID DE LA PREGUNTA: " + id);
console.log("PREGUNTA: " + questionToSave);
console.log("PREGUNTA ID: " + questionToSave._id);
const newGame = new Game({ userId: username, questions: [] });
newGame.questions.push(questionToSave._id);
await newGame.save();
console.log(" ID AL AÑADIR: " + newGame._id);
gameId = newGame._id;
console.log( " EYEYYEY GAME ID: " + gameId);
return null;
}catch (error){
console.error("Error al guardar datos de la partida: " + error);
}

}else{
try{
console.log("segundo else");
console.log("ID DE LA PREGUNTA: " + id);
console.log("PREGUNTA: " + questionToSave);
console.log("PREGUNTA ID: " + questionToSave._id);
existingGame.questions.push(questionToSave._id);
await existingGame.save();
console.log( " ID AL AÑADIR: " + existingGame._id);
gameId = existingGame._id;
console.log( " EYEYYEY GAME ID: " + gameId);
return null;
}catch (error){
console.error("Error al guardar datos de la partida: " + error);
}

} else {
try {
existingGame.questions.push(questionToSave._id);
await existingGame.save();
gameId = existingGame._id;
return null;
} catch (error) {
console.error("Error al guardar datos de la partida: " + error);
}
}


}
}
}


async function saveData(){

try {
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const Game = () => {
const response = await axios.get(`${apiEndpoint}/generateQuestion`, {
params: {
user: usernameGlobal,
newGame: createNewGame
newGame: createNewGame,
numberOfQuestiona: answeredQuestionsValue
}
});
setQuestionId(response.data.question_Id);
Expand Down

0 comments on commit 73dc301

Please sign in to comment.