remove stack if quantity = 0
This commit is contained in:
parent
60a3823b47
commit
249cf02490
|
@ -316,7 +316,7 @@ export async function getItemQuantity(user: string, itemID: number): Promise<Ite
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function giveItem(user: string, item: Item, quantity = 1) {
|
export async function giveItem(user: string, item: Item, quantity = 1): Promise<ItemInventory> {
|
||||||
const storedItem = await db<ItemInventory>('itemInventories')
|
const storedItem = await db<ItemInventory>('itemInventories')
|
||||||
.where('user', user)
|
.where('user', user)
|
||||||
.where('item', item.id)
|
.where('item', item.id)
|
||||||
|
@ -324,6 +324,18 @@ export async function giveItem(user: string, item: Item, quantity = 1) {
|
||||||
|
|
||||||
let inv;
|
let inv;
|
||||||
if (storedItem) {
|
if (storedItem) {
|
||||||
|
if (storedItem.quantity + quantity === 0) {
|
||||||
|
await db<ItemInventory>('itemInventories')
|
||||||
|
.delete()
|
||||||
|
.where('user', user)
|
||||||
|
.where('item', item.id);
|
||||||
|
return {
|
||||||
|
user: user,
|
||||||
|
item: item.id,
|
||||||
|
quantity: 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
inv = await db<ItemInventory>('itemInventories')
|
inv = await db<ItemInventory>('itemInventories')
|
||||||
.update({
|
.update({
|
||||||
quantity: db.raw('MIN(quantity + ?, ?)', [quantity, getMaxStack(item)])
|
quantity: db.raw('MIN(quantity + ?, ?)', [quantity, getMaxStack(item)])
|
||||||
|
|
Loading…
Reference in New Issue