fix: qty update

This commit is contained in:
cha0s 2024-09-27 11:09:56 -05:00
parent 493042b913
commit 92c85e57d3

View File

@ -109,8 +109,9 @@ class ItemProxy {
instance.clear(this.slot); instance.clear(this.slot);
} }
else { else {
const difference = qty - instance.slots[this.slot].qty;
instance.slots[this.slot].qty = 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() { get source() {
@ -138,7 +139,7 @@ export default class Inventory extends Component {
} }
if (qtyUpdated) { if (qtyUpdated) {
for (const slot in qtyUpdated) { for (const slot in qtyUpdated) {
slots[slot].qty = qtyUpdated[slot]; slots[slot].qty += qtyUpdated[slot];
} }
} }
if (swapped) { if (swapped) {
@ -187,7 +188,7 @@ export default class Inventory extends Component {
for (let slot = 1; slot < 11; ++slot) { for (let slot = 1; slot < 11; ++slot) {
if (slots[slot]?.source === stack.source) { if (slots[slot]?.source === stack.source) {
slots[slot].qty += stack.qty; slots[slot].qty += stack.qty;
Component.markChange(this.entity, 'qtyUpdated', {[slot]: slots[slot].qty}); Component.markChange(this.entity, 'qtyUpdated', {[slot]: stack.qty});
return; return;
} }
} }
@ -195,8 +196,8 @@ export default class Inventory extends Component {
if (!slots[slot]) { if (!slots[slot]) {
slots[slot] = stack; slots[slot] = stack;
this.$$items[slot] = new ItemProxy(Component, this, slot); this.$$items[slot] = new ItemProxy(Component, this, slot);
await this.$$items[slot].load(stack.source);
Component.markChange(this.entity, 'given', {[slot]: slots[slot]}); Component.markChange(this.entity, 'given', {[slot]: slots[slot]});
await this.$$items[slot].load(stack.source);
return; return;
} }
} }
@ -233,10 +234,6 @@ export default class Inventory extends Component {
mergeDiff(original, update) { mergeDiff(original, update) {
const merged = { const merged = {
...original, ...original,
qtyUpdated: {
...original.qtyUpdated,
...update.qtyUpdated,
},
cleared: { cleared: {
...original.cleared, ...original.cleared,
...update.cleared, ...update.cleared,
@ -250,6 +247,14 @@ export default class Inventory extends Component {
...(update.swapped || []), ...(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; return merged;
} }
static properties = { static properties = {