Fix and Game Class

This commit is contained in:
2023-12-07 22:27:05 +01:00
parent fec22e6388
commit 8e7e8f22f6
3 changed files with 78 additions and 3 deletions

75
api/Classes/Games.js Normal file
View File

@@ -0,0 +1,75 @@
import { pool } from '../modules/database.js';
class Game {
constructor(id = null, playerId) {
this.id = id;
this.player = playerId;
this.questions = [];
}
async create() {
try {
const [rows] = await pool.execute(
'INSERT INTO games (player) VALUES (?)', [this.player],
);
this.id = rows.insertId;
return true;
}
catch (error) {
console.error(error);
return false;
}
}
async generateQuestions(themeId) {
try {
const [rows] = await pool.execute(
'SELECT * FROM questions ORDER BY RAND() LIMIT 10 WHERE theme = ?', [themeId],
);
rows.forEach(row => {
const question = new Question(row.id, row.question);
this.questions.push(question);
});
return true;
}
catch (error) {
console.error(error);
return false;
}
}
}
class Question {
constructor(id = null, question = null) {
this.id = id;
this.question = question;
this.answers = [];
}
async fetchAnswers() {
try {
const [rows] = await pool.execute(
'SELECT * FROM answers WHERE question = ?', [this.id],
);
rows.forEach(row => {
const answer = new Answer(row.id, row.answer, row.correct);
this.answers.push(answer);
});
return true;
}
catch (error) {
console.error(error);
return false;
}
}
}
class Answer {
constructor(id = null, answer = null, correct = null) {
this.id = id;
this.answer = answer;
this.correct = correct;
}
}
export { Game };

View File

@@ -5,8 +5,8 @@ CREATE DATABASE IF NOT EXISTS `nuitdelinfo2023`
COLLATE utf8mb4_unicode_ci;
DROP USER IF EXISTS 'nuitdelinfo2023';
CREATE USER 'nuitdelinfo2023'@'%' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON airjet.* TO 'nuitdelinfo2023'@'%';
CREATE USER 'nuitdelinfo2023'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON nuitdelinfo2023.* TO 'nuitdelinfo2023'@'localhost';
USE `nuitdelinfo2023`;

View File

@@ -4,7 +4,7 @@ const router = express.Router();
router.get('/', (req, res) => {
res.status(200).json({ code: 200, message:'Received GET request' });
res.status(200).json({ code: 200, message:'Received GET request from ip:' + req.ip });
});