133 lines
2.8 KiB
JavaScript
133 lines
2.8 KiB
JavaScript
import { pool } from '../modules/database.js';
|
|
|
|
class Game {
|
|
constructor(id = null, playerId) {
|
|
this.id = id;
|
|
this.player = playerId;
|
|
this.questions = [];
|
|
}
|
|
|
|
async get() {
|
|
try {
|
|
const [rows] = await pool.execute(
|
|
'SELECT * FROM games WHERE id = ? AND player = ? LIMIT 1', [this.id, this.player],
|
|
);
|
|
if (!rows.length) return false;
|
|
const [questions] = await pool.execute(
|
|
'SELECT * FROM games_questions WHERE game = ?', [this.id],
|
|
);
|
|
questions.forEach(q => {
|
|
const question = new Question(q.id, q.question);
|
|
this.questions.push(question);
|
|
});
|
|
return true;
|
|
}
|
|
catch (error) {
|
|
console.error(error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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 WHERE theme = ? ORDER BY RAND() LIMIT 10', [themeId],
|
|
);
|
|
rows.forEach(async row => {
|
|
const question = new Question(row.id, row.question);
|
|
this.questions.push(question);
|
|
await pool.execute('INSERT INTO games_questions (game, question) VALUES (?, ?)', [this.id, row.id]);
|
|
});
|
|
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 get() {
|
|
try {
|
|
const [rows] = await pool.execute(
|
|
'SELECT * FROM questions WHERE id = ? LIMIT 1', [this.id],
|
|
);
|
|
if (!rows.length) return false;
|
|
this.question = rows[0].question;
|
|
const [answers] = await pool.execute(
|
|
'SELECT * FROM answers WHERE question = ?', [this.id],
|
|
);
|
|
answers.forEach(a => {
|
|
const answer = new Answer(a.id, a.answer, a.correct);
|
|
this.answers.push(answer);
|
|
});
|
|
return true;
|
|
}
|
|
catch (error) {
|
|
console.error(error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
async verifyAnswer(answerId) {
|
|
try {
|
|
const [rows] = await pool.execute(
|
|
'SELECT * FROM answers WHERE question = ? AND id = ? AND correct = 1', [this.id, answerId],
|
|
);
|
|
if (!rows.length) return false;
|
|
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, Question, Answer }; |