Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open pack/frontend #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/models/packs.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
9 changes: 7 additions & 2 deletions backend/models/users.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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,
password: String,
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)
Expand Down
49 changes: 37 additions & 12 deletions backend/routes/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,64 @@ 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 } }
])
await cardsToAdd.map(async (card) => {
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 })
})
})

Expand Down
3 changes: 2 additions & 1 deletion backend/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions frontend/components/modals/SigninModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion frontend/reducers/users.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down