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
}
}),
"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.
async function ensureTable (ctx) {
const tableName = getTableName(ctx)
await ctx.fauna.query(
q.If(
q.Not(q.Exists(q.Collection(tableName))),
q.CreateCollection({
name: tableName,
data: {
files: [],
plugins: [],
},
}),
false,
),
)
}