37 lines
993 B
JavaScript
37 lines
993 B
JavaScript
import express from 'express';
|
|
import { verifyToken } from '../modules/token.js';
|
|
import { respondWithStatusJSON } from '../modules/requestHandler.js';
|
|
import { Game } from '../Classes/Games.js';
|
|
|
|
const router = express.Router();
|
|
|
|
router.post('/create/:theme', verifyToken, async (req, res) => {
|
|
const game = new Game(null, req.user.id);
|
|
await game.create();
|
|
await game.generateQuestions(req.params.theme);
|
|
return await respondWithStatusJSON(res, 200, {
|
|
message: 'Successfully created game',
|
|
game,
|
|
});
|
|
});
|
|
|
|
router.post('/verify/:game', verifyToken, async (req, res) => {
|
|
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)) {
|
|
res.status(200).json({
|
|
message: 'Answer is correct',
|
|
});
|
|
}
|
|
else {
|
|
res.status(200).json({
|
|
message: 'Answer is incorrect',
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
export default router; |