Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transactItem(item, player) {
return this.client.query(
q.Let({
// 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
transactItem(item, player) {
return this.client.query(
q.Let({
// 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 : {
transactItem(item, player) {
return this.client.query(
q.Let({
// 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.
(row) => q.Let({row:q.Get(row)},
q.Let({
buyer : q.Get(q.Select(["data","buyer"], q.Var("row"))),
seller : q.Get(q.Select(["data","seller"], q.Var("row"))),
price : q.Select(["data","price"], q.Var("row")),
item : q.Get(q.Select(["data","item"], q.Var("row")))
},
{
buyer : q.Select(["data","name"], q.Var("buyer")),
seller : q.Select(["data","name"], q.Var("seller")),
price : q.Var("price"),
label : q.Select(["data","label"], q.Var("item")),
key : q.Select(["ref"], q.Var("row"))
}
))
)