Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
q.If(q.Equals(q.Select("ref", q.Var("buyer")), q.Select("ref", q.Var("seller"))),
// Attempting to buy an item you are selling, removes it from sale
q.Do(
q.Update(q.Select("ref", q.Var("item")), {
data : {
for_sale : false
}
}),
"item removed from sale"
),
// Check the credit balance of the purchasing player to ensure
// they have available funds.
q.If(q.LT(q.Var("buyerBalance"), q.Var("itemPrice")),
"purchase failed: insufficient funds",
// All clear! Preconditions passed.
q.Do(
// Write a purchase record.
q.Create(q.Class("purchases"), {
data : {
item : q.Select("ref", q.Var("item")),
price : q.Var("itemPrice"),
buyer : q.Select("ref", q.Var("buyer")),
seller : q.Select("ref", q.Var("seller"))
}
}),
// Update the balances of the selling player and the purchasing player.
q.Update(q.Select("ref", q.Var("buyer")), {
data : {
credits : q.Subtract(q.Var("buyerBalance"), q.Var("itemPrice"))
}
}),
q.Update(q.Select("ref", q.Var("seller")), {
// Load the current player and item based on their refs.
buyer : q.Get(player.ref),
item : q.Get(item.ref)
}, q.Let({
// Load the seller account and set the transaction price.
isForSale : q.Select(["data", "for_sale"], q.Var("item")),
itemPrice : q.Select(["data", "price"], q.Var("item")),
buyerBalance : q.Select(["data", "credits"], q.Var("buyer")),
seller : q.Get(q.Select(["data", "owner"], q.Var("item")))
},
// Check that the item is for sale.
q.If(q.Not(q.Var("isForSale")),
"purchase failed: item not for sale",
q.If(q.Equals(q.Select("ref", q.Var("buyer")), q.Select("ref", q.Var("seller"))),
// Attempting to buy an item you are selling, removes it from sale
q.Do(
q.Update(q.Select("ref", q.Var("item")), {
data : {
for_sale : false
}
}),
"item removed from sale"
),
// Check the credit balance of the purchasing player to ensure
// they have available funds.
q.If(q.LT(q.Var("buyerBalance"), q.Var("itemPrice")),
"purchase failed: insufficient funds",
// All clear! Preconditions passed.
q.Do(
// Write a purchase record.
q.Create(q.Class("purchases"), {
data : {