From 92c85e57d36b48304fa80460e03917cad518d2ea Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 27 Sep 2024 11:09:56 -0500 Subject: [PATCH] fix: qty update --- app/ecs/components/inventory.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/ecs/components/inventory.js b/app/ecs/components/inventory.js index 872dfb1..d9c7368 100644 --- a/app/ecs/components/inventory.js +++ b/app/ecs/components/inventory.js @@ -109,8 +109,9 @@ class ItemProxy { instance.clear(this.slot); } else { + const difference = qty - instance.slots[this.slot].qty; instance.slots[this.slot].qty = qty; - this.Component.markChange(instance.entity, 'qtyUpdated', {[this.slot]: qty}); + this.Component.markChange(instance.entity, 'qtyUpdated', {[this.slot]: difference}); } } get source() { @@ -138,7 +139,7 @@ export default class Inventory extends Component { } if (qtyUpdated) { for (const slot in qtyUpdated) { - slots[slot].qty = qtyUpdated[slot]; + slots[slot].qty += qtyUpdated[slot]; } } if (swapped) { @@ -187,7 +188,7 @@ export default class Inventory extends Component { for (let slot = 1; slot < 11; ++slot) { if (slots[slot]?.source === stack.source) { slots[slot].qty += stack.qty; - Component.markChange(this.entity, 'qtyUpdated', {[slot]: slots[slot].qty}); + Component.markChange(this.entity, 'qtyUpdated', {[slot]: stack.qty}); return; } } @@ -195,8 +196,8 @@ export default class Inventory extends Component { if (!slots[slot]) { slots[slot] = stack; this.$$items[slot] = new ItemProxy(Component, this, slot); - await this.$$items[slot].load(stack.source); Component.markChange(this.entity, 'given', {[slot]: slots[slot]}); + await this.$$items[slot].load(stack.source); return; } } @@ -233,10 +234,6 @@ export default class Inventory extends Component { mergeDiff(original, update) { const merged = { ...original, - qtyUpdated: { - ...original.qtyUpdated, - ...update.qtyUpdated, - }, cleared: { ...original.cleared, ...update.cleared, @@ -250,6 +247,14 @@ export default class Inventory extends Component { ...(update.swapped || []), ], }; + if (update.qtyUpdated) { + if (!merged.qtyUpdated) { + merged.qtyUpdated = {}; + } + for (const slot in update.qtyUpdated) { + merged.qtyUpdated[slot] = (merged.qtyUpdated[slot] ?? 0) + update.qtyUpdated[slot]; + } + } return merged; } static properties = {