Skip to content

Commit

Permalink
Modificada API para evitar respuestas duplicadas
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289432 committed Apr 24, 2024
1 parent 03d4c92 commit e78a044
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions questionservice/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ app.get('/pregunta', async (req, res) => {
// Extraemos los resultados de la consulta
const bindings = response.data.results.bindings;

// Patrón para identificar los códigos de Wikidata
let wikidataCodePattern = /^Q\d+$/;
let correctAnswer = null;
let correctAnswerIndex = 0;
Expand All @@ -48,7 +49,7 @@ app.get('/pregunta', async (req, res) => {
correctAnswerIndex = Math.floor(randomValue * bindings.length);
// Obtenemos la respuesta correcta
correctAnswer = bindings[correctAnswerIndex];
} while (wikidataCodePattern.test(correctAnswer.questionSubjectLabel.value));
} while (wikidataCodePattern.test(correctAnswer.questionSubjectLabel.value) || wikidataCodePattern.test(correctAnswer.answerSubjectLabel.value));

// Creamos la pregunta
const question = questionItem.question.replace('{sujetoPregunta}', correctAnswer.questionSubjectLabel.value);
Expand All @@ -60,11 +61,12 @@ app.get('/pregunta', async (req, res) => {
for (let i = 0; i < 3; i++) {
let randomIndex;
do {
buf = crypto.randomBytes(1)
randomValue = buf[0]/255
// Seleccionamos un índice aleatorio distinto al de la respuesta correcta
randomIndex = Math.floor(randomValue * bindings.length);
} while (randomIndex === correctAnswerIndex);
buf = crypto.randomBytes(1)
randomValue = buf[0]/255
// Seleccionamos un índice aleatorio distinto al de la respuesta correcta
randomIndex = Math.floor(randomValue * bindings.length);
} while (randomIndex === correctAnswerIndex || wikidataCodePattern.test(bindings[randomIndex].answerSubjectLabel.value)
|| answers.includes(bindings[randomIndex].answerSubjectLabel.value));
// Añadimos la capital del país seleccionado aleatoriamente a las respuestas
answers.push(bindings[randomIndex].answerSubjectLabel.value);
}
Expand Down

0 comments on commit e78a044

Please sign in to comment.