From 6bb7e5ee894723159deb05dbff14e5c5b43d7ed6 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Mon, 8 Jul 2024 20:44:45 +0200 Subject: [PATCH] Add support for answers in Polish --- src/routes/learn.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/routes/learn.ts b/src/routes/learn.ts index a5f9a31..8595e04 100644 --- a/src/routes/learn.ts +++ b/src/routes/learn.ts @@ -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; \ No newline at end of file