diff --git a/game/qgservice/QGController.js b/game/qgservice/QGController.js index 63b6ca8..33ec9c1 100644 --- a/game/qgservice/QGController.js +++ b/game/qgservice/QGController.js @@ -1,8 +1,8 @@ -const { generateQuestionPopulation, generateQuestionCapital } = require('./generatorLogic/questiongenerator'); +const { generateQuestionPopulation, generateQuestionCapital, generateQuestionChemical, generateQuestionMonument } = require('./generatorLogic/questiongenerator'); const Question4Answers = require('./Question4Answers'); const { executeSparqlQuery } = require('./generatorLogic/SparqlQuery') -const { bindCapitalsResults, bindPopulationResults } = require('./generatorLogic/BindResults') -const { spainCapitalQuery, worldCapitalQuery, worldPopulationQuery } = require('./generatorLogic/queries') +const { bindCapitalsResults, bindPopulationResults, bindChemicalResults, bindMonumentResults } = require('./generatorLogic/BindResults') +const { spainCapitalQuery, worldCapitalQuery, worldPopulationQuery, chemicalElementQuery, monumentQuery } = require('./generatorLogic/queries') const { createMathQuestions } = require('./generatorLogic/MathQuestions') let QGController = { @@ -12,7 +12,7 @@ let QGController = { const questions = []; // spain capitals - nQuestions = 2; + nQuestions = 3; const spainQueryResult = await executeSparqlQuery(spainCapitalQuery); const spainCapitals = bindCapitalsResults(spainQueryResult) @@ -22,7 +22,7 @@ let QGController = { } // world capitals - nQuestions = 3; + nQuestions = 2; const worldQueryResult = await executeSparqlQuery(worldCapitalQuery); const worldCapitals = bindCapitalsResults(worldQueryResult) @@ -32,7 +32,7 @@ let QGController = { } // world population - nQuestions = 5; + nQuestions = 2; const worldPopulationResult = await executeSparqlQuery(worldPopulationQuery); const worldPopulation = bindPopulationResults(worldPopulationResult) @@ -41,8 +41,28 @@ let QGController = { questions.push(question); } + // chemical elements + nQuestions = 2; + const chemicalResult = await executeSparqlQuery(chemicalElementQuery); + const chemicalElement = bindChemicalResults(chemicalResult) + + for (let i = 0; i < nQuestions; i++) { + const question = generateQuestionChemical(chemicalElement); + questions.push(question); + } + + // monuments + nQuestions = 2; + const monumentResult = await executeSparqlQuery(monumentQuery); + const monument = bindMonumentResults(monumentResult) + + for (let i = 0; i < nQuestions; i++) { + const question = generateQuestionMonument(monument); + questions.push(question); + } + // math questions - const mathquestions = await createMathQuestions(5) + const mathquestions = await createMathQuestions(3) questions.push(...mathquestions) res.json(questions); diff --git a/game/qgservice/generatorLogic/BindResults.js b/game/qgservice/generatorLogic/BindResults.js index 60330b6..25ecc42 100644 --- a/game/qgservice/generatorLogic/BindResults.js +++ b/game/qgservice/generatorLogic/BindResults.js @@ -24,7 +24,33 @@ function bindPopulationResults(queryResult){ return populationMap } +function bindChemicalResults(queryResult){ + const chemicalElementMap = new Map(); + + queryResult.results.bindings.forEach(entry => { + const elementLabel = entry.elementLabel.value; + const symbol = entry.symbol.value; + chemicalElementMap.set(symbol, elementLabel); + }); + + return chemicalElementMap +} + +function bindMonumentResults(queryResult){ + const monumentMap = new Map(); + + queryResult.results.bindings.forEach(entry => { + const monumentLabel = entry.monumentLabel.value; + const countryLabel = entry.countryLabel.value; + monumentMap.set(monumentLabel, countryLabel); + }); + + return monumentMap +} + module.exports = { bindCapitalsResults, - bindPopulationResults + bindPopulationResults, + bindChemicalResults, + bindMonumentResults } diff --git a/game/qgservice/generatorLogic/queries.js b/game/qgservice/generatorLogic/queries.js index e4960ef..8f355a6 100644 --- a/game/qgservice/generatorLogic/queries.js +++ b/game/qgservice/generatorLogic/queries.js @@ -40,6 +40,20 @@ SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE { } ` +const chemicalElementQuery = `SELECT ?element ?elementLabel ?symbol WHERE { + ?element wdt:P31 wd:Q11344; # Instance of chemical element + wdt:P246 ?symbol. # Chemical symbol + SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } +} +` + +const monumentQuery = `SELECT ?monument ?monumentLabel ?country ?countryLabel WHERE { + ?monument wdt:P31 wd:Q4989906; # Instance of historical monument + wdt:P17 ?country. # Country of the monument + SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } +} +LIMIT 1000 +` const chatgptPrompt = ` @@ -58,5 +72,7 @@ module.exports = { spainPopulationQuery, //chatgptPrompt, spainCapitalQuery, - worldCapitalQuery + worldCapitalQuery, + chemicalElementQuery, + monumentQuery }; diff --git a/game/qgservice/generatorLogic/questiongenerator.js b/game/qgservice/generatorLogic/questiongenerator.js index 5dbbfa5..e259aa5 100644 --- a/game/qgservice/generatorLogic/questiongenerator.js +++ b/game/qgservice/generatorLogic/questiongenerator.js @@ -64,6 +64,43 @@ function generateQuestionCapital(countryCapitalMap) { incorrectAnswer2: incorrectAnswers[1], incorrectAnswer3: incorrectAnswers[2], }; + // 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; + } + + function generateQuestionChemical(chemicalElementMap) { + const chemicalElementArray = Array.from(chemicalElementMap); + + const randomIndex = Math.floor(Math.random() * chemicalElementArray.length); + const [ symbol, chemical] = chemicalElementArray[randomIndex]; + + const incorrectAnswers = []; + while (incorrectAnswers.length < 3) { + const randomElement = chemicalElementArray[Math.floor(Math.random() * chemicalElementArray.length)]; + const [randomSymbol, randomElementName] = randomElement; + if (randomElementName !== chemical && !incorrectAnswers.includes(randomSymbol)) { + incorrectAnswers.push(randomSymbol); + } + } + + // Create the question object + const question = { + uuid: uuid.v4(), + question: `What is the symbol of ${chemical}?`, + correctAnswer: symbol, + incorrectAnswer1: incorrectAnswers[0], + incorrectAnswer2: incorrectAnswers[1], + incorrectAnswer3: incorrectAnswers[2], + }; // Save the question to MongoDB const newQuestion = new Question4Answers(question); @@ -78,7 +115,47 @@ function generateQuestionCapital(countryCapitalMap) { return question; } +function generateQuestionMonument(monumentMap) { + const monumentArray = Array.from(monumentMap); + + const randomIndex = Math.floor(Math.random() * monumentArray.length); + const [ monumentLabel, countryLabel] = monumentArray[randomIndex]; + + const incorrectAnswers = []; + while (incorrectAnswers.length < 3) { + const randomMonument = monumentArray[Math.floor(Math.random() * monumentArray.length)]; + const [randomMonumentLabel, randomCountry] = randomMonument; + if (randomMonumentLabel !== monumentLabel && !incorrectAnswers.includes(randomCountry)) { + incorrectAnswers.push(randomCountry); + } + } + + // Create the question object + const question = { + uuid: uuid.v4(), + question: `Where is ${monumentLabel}?`, + correctAnswer: countryLabel, + incorrectAnswer1: incorrectAnswers[0], + incorrectAnswer2: incorrectAnswers[1], + incorrectAnswer3: incorrectAnswers[2], + }; + +// 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, - generateQuestionCapital + generateQuestionCapital, + generateQuestionChemical, + generateQuestionMonument }; \ No newline at end of file