Updated games

This commit is contained in:
2023-12-08 01:37:38 +01:00
parent 57271f51b4
commit 5a7656046c
3 changed files with 53 additions and 13 deletions

View File

@@ -47,9 +47,10 @@ class Game {
const [rows] = await pool.execute(
'SELECT * FROM questions ORDER BY RAND() LIMIT 10 WHERE theme = ?', [themeId],
);
rows.forEach(row => {
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;
}
@@ -67,6 +68,28 @@ class 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(
@@ -107,4 +130,4 @@ class Answer {
}
}
export { Game };
export { Game, Question, Answer };

View File

@@ -23,13 +23,13 @@ CREATE TABLE games (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
player INT UNSIGNED NOT NULL,
PRIMARY KEY (id),
INDEX g_player_idx (player),
INDEX g_player_idx (player)
) ENGINE=InnoDB;
CREATE TABLE themes (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
theme VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
PRIMARY KEY (id)
) ENGINE=InnoDB;
CREATE TABLE questions (
@@ -37,7 +37,7 @@ CREATE TABLE questions (
theme INT UNSIGNED NOT NULL,
question VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
INDEX q_theme_idx (theme),
INDEX q_theme_idx (theme)
) ENGINE=InnoDB;
CREATE TABLE answers (
@@ -46,5 +46,15 @@ CREATE TABLE answers (
answer VARCHAR(255) NOT NULL,
correct BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (id),
INDEX a_question_idx (question),
INDEX a_question_idx (question)
) ENGINE=InnoDB;
CREATE TABLE game_questions (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
game INT UNSIGNED NOT NULL,
question INT UNSIGNED NOT NULL,
score INT UNSIGNED,
PRIMARY KEY (id),
INDEX gq_game_idx (game),
INDEX gq_question_idx (question)
) ENGINE=InnoDB;

View File

@@ -1,7 +1,8 @@
import express from 'express';
import { pool } from '../modules/database.js';
import { verifyToken } from '../modules/token.js';
import { respondWithStatusJSON } from '../modules/requestHandler.js';
import { Game } from '../Classes/Games.js';
import { respondWithStatus, respondWithStatusJSON } from '../modules/requestHandler.js';
import { Game, Question } from '../Classes/Games.js';
const router = express.Router();
@@ -16,12 +17,18 @@ router.post('/create/:theme', verifyToken, async (req, res) => {
});
router.post('/verify/:game', verifyToken, async (req, res) => {
const [rows] = await pool.execute(
'SELECT * FROM games WHERE id = ? AND player = ? LIMIT 1', [req.params.game, req.userId],
);
if (!rows.length) return await respondWithStatus(res, 404, 'Game not found');
const { question, answer } = req.body;
const game = new Game(req.params.game, req.userId);
await game.get();
const foundQuestion = game.questions.find(q => q.id === question);
if (foundQuestion && foundQuestion.verifyAnswer(answer)) {
if (![question, answer].every(Boolean)) return await respondWithStatus(res, 400, 'Missing fields');
const q = new Question(question);
const [gameQuestions] = await pool.execute('SELECT * FROM game_questions WHERE game = ? AND question = ?', [req.params.game, question]);
if (!gameQuestions.length) return await respondWithStatus(res, 404, 'Question not found');
if (gameQuestions[0].score) return await respondWithStatus(res, 400, 'Question already answered');
if (q.verifyAnswer(answer)) {
await pool.execute('UPDATE game_questions SET score = 1 WHERE game = ? AND question = ?', [req.params.game, question]);
res.status(200).json({
message: 'Answer is correct',
});