From fb617b44151ab9617d12138d6c880d53260f78f7 Mon Sep 17 00:00:00 2001 From: p4535992 Date: Sun, 7 Jan 2024 18:27:06 +0100 Subject: [PATCH] "try" to add the set parameter to the method appendItemChanges --- src/API/private-api.js | 2 +- src/helpers/transaction.js | 21 +++++++++++++++++++-- src/helpers/utilities.js | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/API/private-api.js b/src/API/private-api.js index d1b522cd..b67b8333 100644 --- a/src/API/private-api.js +++ b/src/API/private-api.js @@ -371,7 +371,7 @@ export default class PrivateAPI { .filter(currency => Helpers.isRealNumber(currency.quantity) && currency.quantity >= 0); const itemsToUpdate2 = currenciesToUpdate.filter(currency => currency.type === "item") - .map(currency => ({ item: currency.data.item, quantity: currency.quantity })); + .map(currency => ({ item: currency.data.item, quantity: 1, cost: currency.quantity })); const attributesToUpdate = currenciesToUpdate.filter(currency => currency.type === "attribute") .map(currency => ({ path: currency.data.path, quantity: currency.quantity })); diff --git a/src/helpers/transaction.js b/src/helpers/transaction.js index c9a45b94..ec151c85 100644 --- a/src/helpers/transaction.js +++ b/src/helpers/transaction.js @@ -40,7 +40,12 @@ export default class Transaction { if (SYSTEMS.DATA.ITEM_TRANSFORMER && !remove) { itemData = await SYSTEMS.DATA.ITEM_TRANSFORMER(itemData); } - const incomingQuantity = Math.abs(data.quantity ?? Utilities.getItemQuantity(itemData)) * (remove ? -1 : 1); + const incomingQuantity = set + ? Math.abs(data.quantity ?? Utilities.getItemQuantity(itemData)) + : Math.abs(data.quantity ?? Utilities.getItemQuantity(itemData)) * (remove ? -1 : 1); + const incomingCost = set + ? Math.abs(data.cost ?? Utilities.getItemCost(itemData)) + : Math.abs(data.cost ?? Utilities.getItemCost(itemData)) * (remove ? -1 : 1); let itemId = itemData._id ?? itemData.id; let actorHasItem = false; let actorExistingItem = false; @@ -59,7 +64,8 @@ export default class Transaction { areItemsColliding(item, itemData) ) }); - } else { + } + else { actorHasItem = this.actor.items.get(itemId); actorExistingItem = actorHasItem || Utilities.findSimilarItem(this.actor.items, itemData); } @@ -77,10 +83,12 @@ export default class Transaction { if (actorExistingItem) { const itemQuantity = Utilities.getItemQuantity(actorExistingItem); + const itemCost = Utilities.getItemCost(actorExistingItem); if (itemQuantity > 1 || canItemStack) { const newQuantity = itemQuantity + incomingQuantity; + const newCost = itemCost + incomingCost; const existingItemUpdate = remove ? this.itemsToUpdate.find(item => item._id === itemId) @@ -90,11 +98,15 @@ export default class Transaction { Utilities.setItemQuantity(existingItemUpdate, newQuantity); if (keepIfZero && type !== "currency") { setProperty(existingItemUpdate, CONSTANTS.FLAGS.ITEM + ".notForSale", newQuantity === 0); + } else if(keepIfZero && type === "currency"){ + Utilities.setItemCost(existingItemUpdate, newCost); } } else { const update = Utilities.setItemQuantity(actorExistingItem.toObject(), newQuantity); if (keepIfZero && type !== "currency") { setProperty(update, CONSTANTS.FLAGS.ITEM + ".notForSale", newQuantity === 0); + } else if(keepIfZero && type === "currency"){ + Utilities.setItemCost(update, newCost); } this.itemTypeMap.set(actorExistingItem.id, type) this.itemsToUpdate.push(update); @@ -113,6 +125,7 @@ export default class Transaction { itemData._id = randomID(); } Utilities.setItemQuantity(itemData, incomingQuantity); + Utilities.setItemCost(itemData, incomingCost); this.itemsToCreate.push(itemData); this.itemTypeMap.set(itemData._id, type) @@ -123,11 +136,15 @@ export default class Transaction { if (existingItemCreation && canItemStack) { const newQuantity = Utilities.getItemQuantity(existingItemCreation) + incomingQuantity; Utilities.setItemQuantity(existingItemCreation, newQuantity); + + const newCost = Utilities.getItemCost(existingItemCreation) + incomingCost; + Utilities.setItemCost(existingItemCreation, newCost); } else { if (!itemData._id) { itemData._id = randomID(); } Utilities.setItemQuantity(itemData, incomingQuantity); + Utilities.setItemCost(itemData, incomingCost); this.itemsToCreate.push(itemData); this.itemTypeMap.set(itemData._id, type) } diff --git a/src/helpers/utilities.js b/src/helpers/utilities.js index ba38ced8..1f6befe0 100644 --- a/src/helpers/utilities.js +++ b/src/helpers/utilities.js @@ -214,19 +214,52 @@ export function hasItemQuantity(item) { * @param {Boolean} requiresExistingQuantity * @returns {Object} */ -export function setItemQuantity(itemData, quantity, requiresExistingQuantity = false) { +export function setItemQuantity(item, quantity, requiresExistingQuantity = false) { + const itemData = item instanceof Item ? item.toObject() : item; if (!requiresExistingQuantity || getItemTypesThatCanStack().has(itemData.type) || hasItemQuantity(itemData)) { setProperty(itemData, game.itempiles.API.ITEM_QUANTITY_ATTRIBUTE, quantity); } return itemData; } - +/** + * Returns a given item's cost/price + * + * @param {Item/Object} item + * @returns {number} + */ export function getItemCost(item) { const itemData = item instanceof Item ? item.toObject() : item; return getProperty(itemData, game.itempiles.API.ITEM_PRICE_ATTRIBUTE) ?? 0; } +/** + * Returns whether an item has the cost/price property + * + * @param {Item/Object} item + * @returns {Boolean} + */ +export function hasItemCost(item) { + const itemData = item instanceof Item ? item.toObject() : item; + return hasProperty(itemData, game.itempiles.API.ITEM_PRICE_ATTRIBUTE); +} + +/** + * Returns a given item's quantity + * + * @param {Object} itemData + * @param {Number} cost + * @param {Boolean} requiresExistingCost + * @returns {Object} + */ +export function setItemCost(item, cost, requiresExistingCost = false) { + const itemData = item instanceof Item ? item.toObject() : item; + if (!requiresExistingCost || hasItemCost(itemData)) { + setProperty(itemData, game.itempiles.API.ITEM_PRICE_ATTRIBUTE, cost); + } + return itemData +} + /** * Retrieves all visible tokens on a given location *