Skip to content

Commit

Permalink
Merge pull request #29 from Arquisoft/questiongenerator1
Browse files Browse the repository at this point in the history
Question generator 1
  • Loading branch information
pelazas authored Feb 28, 2024
2 parents 219bf25 + 085ffb0 commit 2ddef72
Show file tree
Hide file tree
Showing 16 changed files with 6,149 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
docs/build
docs/build
game/qgservice/.env
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

questiongeneratorservice:
container_name: qgservice-${teamname:-defaultASW}
image: pelazas1/questiongenerator:latest
profiles: ["dev", "prod"]
build: ./game/qgservice
depends_on:
- mongodb
ports:
- "8003:8003"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
volumes:
- ./game/qgservice:/usr/src/questiongeneratorservice

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand All @@ -55,6 +71,7 @@ services:
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
QG_SERVICE_URL: http://qgservice:8003

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
2 changes: 2 additions & 0 deletions game/qgservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
1 change: 1 addition & 0 deletions game/qgservice/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY="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"
20 changes: 20 additions & 0 deletions game/qgservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/questiongeneratorservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the app source code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 8003

# Define the command to run your app
CMD ["node", "qg-service.js"]
54 changes: 54 additions & 0 deletions game/qgservice/MathQuestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Question4Answers = require("./Question4Answers");

function generateRandomMathQuestion() {
const operators = ['+', '-', '*', '/'];

const operator = operators[Math.floor(Math.random() * operators.length)];
let num1 = Math.floor(Math.random() * 100) + 1;
let num2 = Math.floor(Math.random() * 100) + 1;

// Ensure division results in integers
if (operator === '/' && num2 !== 0) {
const divisionResult = num1 / num2;
num1 = divisionResult * num2; // Adjust num1 to get an integer result
}

const question = `${num1} ${operator} ${num2}`;
const correctAnswer = eval(question).toString();

// Generate incorrect answers
const incorrectAnswers = [];
while (incorrectAnswers.length < 3) {
const randomAnswer = Math.floor(Math.random() * 200) - 100; // Random integer between -100 and 100
if (incorrectAnswers.indexOf(randomAnswer.toString()) === -1 && randomAnswer.toString() !== correctAnswer) {
incorrectAnswers.push(randomAnswer.toString());
}
}

return {
question,
correctAnswer,
incorrectAnswer1: incorrectAnswers[0],
incorrectAnswer2: incorrectAnswers[1],
incorrectAnswer3: incorrectAnswers[2],
};
}

async function saveMathQuestions(numberOfQuestions) {
const questions = [];

for (let i = 0; i < numberOfQuestions; i++) {
const mathQuestion = generateRandomMathQuestion();
questions.push(mathQuestion);
}

try {
const savedQuestions = await Question4Answers.insertMany(questions);
console.log(`${numberOfQuestions} math questions saved to MongoDB:`, savedQuestions);
return questions;
} catch (error) {
console.error('Error saving math questions to MongoDB:', error.message);
}
}

module.exports = { saveMathQuestions };
28 changes: 28 additions & 0 deletions game/qgservice/Question4Answers.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 2ddef72

Please sign in to comment.