From 7755d011b7ffc380934d020d46af895881e4a585 Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Tue, 30 Mar 2021 19:33:03 +0300 Subject: [PATCH] =?UTF-8?q?:art:=20=D0=A1=D0=BE=D0=B1=D1=80=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20=D0=BA=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D0=B8=20=D0=B2=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/index.js b/backend/index.js index 1da2c0f..c5435c0 100644 --- a/backend/index.js +++ b/backend/index.js @@ -34,10 +34,12 @@ app.use(session({ })) client.connect() +const cardsCollection = client.db(process.env.DB_NAME).collection('cards') +const answersCollection = client.db(process.env.DB_NAME).collection('answers') // Выравнивание вероятности let dropProb = {} // Объект хранящий вероятности с которой карта каждого участника отбрасывается -client.db(process.env.DB_NAME).collection('cards').aggregate([ +cardsCollection.aggregate([ { '$group': { '_id': '$name', @@ -47,7 +49,7 @@ client.db(process.env.DB_NAME).collection('cards').aggregate([ } } ]).toArray().then((memeCount) => { - client.db(process.env.DB_NAME).collection('cards').countDocuments().then((totalCount) => { + cardsCollection.countDocuments().then((totalCount) => { let quota, quotaTimes quota = totalCount/memeCount.length // Квота мемов на человека console.log(totalCount + " мемов всего. Квота: " + quota) @@ -90,7 +92,7 @@ app.get('/card', async (req, res) => { let card // Тянем карты и отбрасываем их в соответствии с их вероятностью отбрасывания do { - card = await client.db(process.env.DB_NAME).collection('cards').aggregate([{ $sample: { size: 1 } }]).toArray() + card = await cardsCollection.aggregate([{ $sample: { size: 1 } }]).toArray() card = card[0] } while (Math.random() < dropProb[card.name]) res.status(200).send(card) @@ -106,7 +108,7 @@ app.get('/card', async (req, res) => { app.post('/answer', (req, res) => { if (req.session.loggedIn) { try { - client.db(process.env.DB_NAME).collection('answers').insertOne(req.body.data) + answersCollection.insertOne(req.body.data) res.status(200).send() } catch (e) { console.log("Error: " + e)