From 393cbfdf8de27c47691550553e4b03bf705c2ae3 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 13 Jan 2022 00:02:29 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=82=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=B0=20=D0=BD=D0=B0=20backen?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - backend/.env.example | 13 ++++---- backend/index.js | 54 +++++++++++++++++++++++--------- frontend/src/components/Game.vue | 10 ++++-- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 2d18c8a..b9fdac5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,5 @@ ### Todo: - Собирать в БД не только картинки, но и репосты и видео -- Переместить определение правильности ответа на сервер - Вынести контрольный вопрос и название игры в `.env`, как вынесен пароль - Записывать статистику ответов как n документов, соответствующих возможным вариантам ответа diff --git a/backend/.env.example b/backend/.env.example index 297a204..131f8e1 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -1,6 +1,7 @@ -PORT=Порт, например 3000 -URI=Адрес базы данных -FRONTEND=Адрес откуда будут поступать запросы -DB_NAME=Имя базы данных -SECRET=Секретное слово для сессии -PASSWORD=Ответ на вопрос на странице авторизации \ No newline at end of file +NODE_ENV=production +PORT=3031 +URI=mongodb://localhost?retryWrites=true&w=majority +FRONTEND=https://bingo.anatolykopyl.ru +DB_NAME=vk_bingo +SECRET=secretword +PASSWORD=pass diff --git a/backend/index.js b/backend/index.js index 0adde4e..1f27d07 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,7 +1,7 @@ const express = require('express') const session = require('express-session') const app = express() -const {MongoClient} = require('mongodb') +const { MongoClient, ObjectId } = require('mongodb') const MongoStore = require('connect-mongo') const cors = require('cors') require('dotenv').config() @@ -9,7 +9,6 @@ require('dotenv').config() app.use(cors({ origin: [ process.env.FRONTEND, - process.env.FRONTEND_HTTPS ], credentials: true, exposedHeaders: ['set-cookie'] @@ -102,7 +101,13 @@ app.get('/api/card', async (req, res) => { let card // Тянем карты и отбрасываем их в соответствии с их вероятностью отбрасывания do { - card = await cardsCollection.aggregate([{ $sample: { size: 1 } }]).toArray() + card = await cardsCollection.aggregate([ + { + $sample: { size: 1 } + }, { + $unset: [ 'name', 'link', 'date' ] + } + ]).toArray() card = card[0] } while (Math.random() < dropProb[card.name]) res.status(200).send(card) @@ -130,20 +135,39 @@ app.get('/api/meme', async (req, res) => { } }) -app.post('/api/answer', (req, res) => { +app.post('/api/answer', async (req, res) => { if (req.session.loggedIn) { - if (req.body.data.correct) { - req.session.right++ + if (req.body.data.id && req.body.data.name) { + const card = await cardsCollection.findOne({ _id: ObjectId(req.body.data.id) }) + if (card) { + if (card.name === req.body.data.name) { + req.session.right++ + answersCollection.insertOne({ + correct: true, + selected: req.body.data.name + }) + res.status(200).send({ + correct: true, + name: card.name, + date: card.date + }) + } else { + req.session.wrong++ + answersCollection.insertOne({ + correct: false, + selected: req.body.data.name + }) + res.status(200).send({ + correct: false, + name: card.name, + date: card.date + }) + } + } else { + res.status(500).send() + } } else { - req.session.wrong++ - } - - try { - answersCollection.insertOne(req.body.data) - res.status(200).send() - } catch (e) { - console.log("Error: " + e) - res.status(500).send() + res.status(400).send() } } else { res.status(403).send() diff --git a/frontend/src/components/Game.vue b/frontend/src/components/Game.vue index 8a2a2ea..1a1be2a 100644 --- a/frontend/src/components/Game.vue +++ b/frontend/src/components/Game.vue @@ -69,7 +69,6 @@ export default { } }, selectAnswer: function(selection) { - this.correctAnswer = selection === this.card.name this.selectedAnswer = selection let innerThis = this setTimeout(function() { @@ -84,10 +83,15 @@ export default { axios .post(process.env.VUE_APP_BACKEND + '/answer', { 'data': { - 'correct': this.correctAnswer, - 'selected': this.selectedAnswer + 'id': this.card._id, + 'name': this.selectedAnswer } }) + .then((response) => { + this.correctAnswer = response.data.correct + this.card.name = response.data.name + this.card.date = response.data.date + }) } }, mounted() {