Add support for answers in Polish

This commit is contained in:
Aelita4 2024-07-08 20:44:45 +02:00
parent c53c517855
commit 6bb7e5ee89
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 38 additions and 0 deletions

View File

@ -149,4 +149,42 @@ router.post('/submitAnswer', authenticateJWT, (req, res) => {
});
});
router.post('/submitPolishAnswer', authenticateJWT, (req, res) => {
const answers = req.body as { id: number, answer: string }[];
const promises = answers.map(answer => db.query('SELECT * FROM words WHERE wordId = ?', [answer.id]));
Promise.all(promises)
.then(async (results: Word[][]) => {
const verified = results.map((result, index) => {
return {id: result[0].wordID, correct: result[0].polishWord === answers[index].answer }
});
const accuracy = verified.filter((v) => v.correct).length / verified.length;
const xp = accuracy * 10 + (accuracy === 1 ? 10 : 0);
// @ts-ignore
const time = times.find((time) => time.userId === req.user.userId);
if(!time) return res.status(404).json({ code: 404, error: 'Test not started' });
times.splice(times.indexOf(time), 1);
// @ts-ignore
await db.updateUserXP(req.user.userId, xp);
// @ts-ignore
await db.addScore(req.user.userId, Math.floor((Date.now() - time.start) / 1000), xp, verified.filter((v) => !v.correct).length);
res.json({ code: 200, results: {
questions: verified,
timeElapsed: Math.floor((Date.now() - time.start) / 1000),
accuracy,
experienceEarned: xp
}});
})
.catch((err: any) => {
console.log(err);
res.status(500).json({ code: 500, error: 'Internal server error' });
});
});
export default router;