Skip to content

Commit

Permalink
"try" to add the set parameter to the method appendItemChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
p4535992 committed Jan 7, 2024
1 parent 81e1c7a commit fb617b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/API/private-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
21 changes: 19 additions & 2 deletions src/helpers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
37 changes: 35 additions & 2 deletions src/helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit fb617b4

Please sign in to comment.