diff --git a/backend/models/packs.js b/backend/models/packs.js index 98a7415..1cde483 100644 --- a/backend/models/packs.js +++ b/backend/models/packs.js @@ -1,8 +1,8 @@ const mongoose = require('mongoose') const packPriceSchema = mongoose.Schema({ - userToken: { type: mongoose.Schema.Types.String, ref: 'users' }, - price: Number, + userToken: String, + price: Number }) const packSchema = mongoose.Schema({ diff --git a/backend/models/users.js b/backend/models/users.js index a42d9ca..5a0a94b 100644 --- a/backend/models/users.js +++ b/backend/models/users.js @@ -1,5 +1,10 @@ const mongoose = require('mongoose') +const packIdSchema = mongoose.Schema({ + packId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'packs' }], + quantity: Number +}) + const userSchema = mongoose.Schema({ email: String, username: String, @@ -7,8 +12,8 @@ const userSchema = mongoose.Schema({ stock: Number, token: String, credits: Number, - cardsId: [{type: mongoose.Schema.Types.ObjectId, ref: 'cards'}], - packsId: [{type: mongoose.Schema.Types.ObjectId, ref: 'packs'}], + cardsId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'cards' }], + packsId: [packIdSchema] }) const User = mongoose.model('users', userSchema) diff --git a/backend/routes/pack.js b/backend/routes/pack.js index 5207901..2404f32 100644 --- a/backend/routes/pack.js +++ b/backend/routes/pack.js @@ -11,25 +11,49 @@ router.get("/marketPacks", (req, res) => { Pack.find({ 'packPrices.price': { $gt: 0 } }) .then((pack) => { res.json({ result: true, pack: pack }) - }); -}); + }) +}) router.patch("/openPack/:userToken/:userPack", async (req, res) => { // find the pack to open - const user = await User.findOne({ "token": req.params.userToken }) - console.log("User cards: ", user.cardsId) - const userId = await user._id - const pack = await Pack.findOne({ "_id": req.params.userPack, "packPrices.userToken": req.params.userToken }) - // Decrease pack Stock - const packToUpdate = pack.packPrices[0]._id + const pack = await Pack.findOne({ "_id": req.params.userPack }) + + // Decrease pack's Stock const newStock = Number(pack.stock) - 1 - await Pack.updateOne({ "packPrices.userToken": req.params.userToken }, { "stock": newStock }) + await Pack.updateOne({ "_id": req.params.userPack }, { "stock": newStock }) + const packsEligible = [] + pack.packPrices.map((e) => { + if (e.userToken == req.params.userToken) { + packsEligible.push(e) + } + }) + const packToremove = packsEligible[0]._id // Remove user from Pack await Pack.updateOne( { "_id": req.params.userPack }, - { $pull: { "packPrices": { "_id": packToUpdate } } } + { $pull: { "packPrices": { "_id": packToremove } } } ) + + // Decrease pack quantity for user + + const packQtyToDecrease = await User.findOne({ "token": req.params.userToken }) + + packQtyToDecrease.packsId.map((e) => { + if (e.packId == req.params.userPack) { + User.updateOne({ + "packsId": { + "packId": req.params.userPack + } + }, { + "packsId": { + "quantity": packsEligible.length + } + }) + } + }) + + // Give 5 random cards to user const cardsToAdd = await Card.aggregate([ { $sample: { size: 5 } } ]) @@ -37,13 +61,14 @@ router.patch("/openPack/:userToken/:userPack", async (req, res) => { await Card.updateOne({ "_id": card._id }, { $push: { "cardPrices": { - price: 3000, - userId: userId + price: 0, + userToken: req.params.userToken } } }) await User.updateOne({ "token": req.params.userToken }, { $push: { "cardsId": card._id } }) + res.json({ result: true, newCards: cardsToAdd }) }) }) diff --git a/backend/routes/users.js b/backend/routes/users.js index ed0126a..92fe039 100644 --- a/backend/routes/users.js +++ b/backend/routes/users.js @@ -28,7 +28,7 @@ router.post("/signup", (req, res) => { email: req.body.email, password: hash, token: token, - credits: Math.floor(Math.random()*89999+10000), + credits: Math.floor(Math.random() * 89999 + 10000), gamesId: [], cardsId: [], packsId: ["657ad5ea8f96e935b9b105a4"], @@ -69,6 +69,7 @@ router.post("/signin", (req, res) => { res.json({ result: false, error: "Wrong password" }); return; } + console.log(data.packsList) res.json({ result: true, token: data.token, diff --git a/frontend/components/modals/SigninModal.js b/frontend/components/modals/SigninModal.js index f65c59d..167f3cf 100644 --- a/frontend/components/modals/SigninModal.js +++ b/frontend/components/modals/SigninModal.js @@ -22,17 +22,17 @@ function SigninModal() { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: signinEmail, password: signinPassword }), }) - .then(response => response.json()) - .then(data => { - if(data.result) { - setSigninEmail('') - setSigninPassword('') - addUser({ token: data.token, email: signinEmail, username: data.username, cardsList: data.cardsList, gamesList: data.gamesList, packsList: data.packsList }) - router.push("/home"); - } else { - setErrorMessage(data.error) - } - }) + .then(response => response.json()) + .then(data => { + if (data.result) { + setSigninEmail('') + setSigninPassword('') + addUser({ token: data.token, email: signinEmail, username: data.username, cardsList: data.cardsList, gamesList: data.gamesList, packsList: data.packsList }) + router.push("/home"); + } else { + setErrorMessage(data.error) + } + }) } const state = useSelector((state) => state.users.value) return ( diff --git a/frontend/reducers/users.js b/frontend/reducers/users.js index 2424a9c..0548660 100644 --- a/frontend/reducers/users.js +++ b/frontend/reducers/users.js @@ -1,7 +1,7 @@ import { createSlice } from '@reduxjs/toolkit' const initialState = { - value: { token: null, username: null, email: null, cardsList: [], packsList: []} + value: { token: null, username: null, email: null, cardsList: [], packsList: [] } } export const usersSlice = createSlice({