diff --git a/game/qgservice/Question4Answers.js b/game/qgservice/Question4Answers.js new file mode 100644 index 0000000..62074ff --- /dev/null +++ b/game/qgservice/Question4Answers.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose'); + +const question4AnswersSchema = new mongoose.Schema({ + question: { + type: String, + required: true, + }, + correctAnswer: { + type: String, + required: true, + }, + incorrectAnswer1: { + type: String, + required: true, + }, + incorrectAnswer2: { + type: String, + required: true, + }, + incorrectAnswer3: { + type: String, + required: true, + }, +}); + +const Question4Answers = mongoose.model('Question4Answers', question4AnswersSchema); + +module.exports = Question4Answers; \ No newline at end of file diff --git a/game/qgservice/qg-service.js b/game/qgservice/qg-service.js index ec7aa5b..1652a25 100644 --- a/game/qgservice/qg-service.js +++ b/game/qgservice/qg-service.js @@ -2,6 +2,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const axios = require('axios'); +const mongoose = require('mongoose'); const { usaPopulationQuery, spainPopulationQuery } = require('./queries'); const { generateQuestionPopulation } = require('./questiongenerator'); @@ -10,6 +11,9 @@ const port = 8003; app.use(bodyParser.json()); +const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; +mongoose.connect(mongoUri); + async function executeSparqlQuery(query) { try { const response = await axios.get('https://query.wikidata.org/sparql', { diff --git a/game/qgservice/questiongenerator.js b/game/qgservice/questiongenerator.js index 3915314..3402b6c 100644 --- a/game/qgservice/questiongenerator.js +++ b/game/qgservice/questiongenerator.js @@ -1,29 +1,42 @@ -// questionGenerator.js +const Question4Answers = require('./Question4Answers'); + function generateQuestionPopulation(cityPopulationMap) { - const cityPopulationArray = Array.from(cityPopulationMap); - - const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); - const [city, population] = cityPopulationArray[randomIndex]; - - const incorrectAnswers = []; - while (incorrectAnswers.length < 3) { - const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; - const [randomCityName, randomCityPopulation] = randomCity; - if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { - incorrectAnswers.push(randomCityPopulation); - } + const cityPopulationArray = Array.from(cityPopulationMap); + + const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); + const [city, population] = cityPopulationArray[randomIndex]; + + const incorrectAnswers = []; + while (incorrectAnswers.length < 3) { + const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; + const [randomCityName, randomCityPopulation] = randomCity; + if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { + incorrectAnswers.push(randomCityPopulation); } - - const question = { - question: `What is the population of ${city}?`, - correctAnswer: population, - incorrectAnswers, - }; - - return question; } - - module.exports = { - generateQuestionPopulation, + + // Create the question object + const question = { + question: `What is the population of ${city}?`, + correctAnswer: population.toString(), + incorrectAnswer1: incorrectAnswers[0].toString(), + incorrectAnswer2: incorrectAnswers[1].toString(), + incorrectAnswer3: incorrectAnswers[2].toString(), }; - \ No newline at end of file + + // Save the question to MongoDB + const newQuestion = new Question4Answers(question); + newQuestion.save() + .then(savedQuestion => { + console.log('Question saved to MongoDB:', savedQuestion); + }) + .catch(error => { + console.error('Error saving question to MongoDB:', error.message); + }); + + return question; +} + +module.exports = { + generateQuestionPopulation, +}; \ No newline at end of file