updateMaxQuantities(itemList)
{
let tempInventory = null;
let maxStacks = 0;
// Collapses the pending items into a replica of the players inventory in a grouped format
if (this.state.shipMode == ShipItemShopMode.Disabled)
{
tempInventory = [].concat(pc.inventory.storage);
maxStacks = pc.inventory.maxSlots;
}
else if (this.state.shipMode == ShipItemShopMode.Gadgets)
{
tempInventory = [].concat(pc_ship.inventory.storage);
maxStacks = pc_ship.capacity();
}
else if (this.state.shipMode == ShipItemShopMode.Weapons)
{
tempInventory = [].concat(pc_ship.inventory.equippedItems.weapons);
maxStacks = pc_ship.gunCapacity();
}
let startingInventoryCount = tempInventory.length;
// Use copies so we can mess with the values
tempInventory = this.groupItems(tempInventory.map(v => v.makeCopy()));
// Remove sale quantities
this.groupItems(this.state.itemsToSell).forEach(fs => {
let ii = tempInventory.filter(e => e.getClassName() == fs.getClassName());
if (ii.length > 0)
{
ii[0].quantity -= fs.quantity;
}
});
// Inject purchase quantities
this.groupItems(this.state.itemsToBuy).forEach(fs => {
let ii = tempInventory.filter(e => e.getClassName() == fs.getClassName());
if (ii.length > 0)
{
ii[0].quantity += fs.quantity;
}
else
{
let c = fs.makeCopy();
c.quantity = fs.quantity;
tempInventory.push(c);
}
});
// Remove anything that ended up at 0 quant
tempInventory = tempInventory.filter(i => i.quantity > 0);
// Ungroup the collapsed items back into a similar form that it would take as an inventory list
let postChangeInventory = this.ungroupItems(tempInventory);
if (this.state.ShopMode === ShopMode.BUY)
{
itemList.forEach(item => {
let potentiallyMergableStacks = postChangeInventory.filter(e => e.getClassName() == item.getClassName());
let availableMerge = potentiallyMergableStacks.reduce((acc, cur) => {
return acc + (cur.stackSize - cur.quantity)
}, 0);
// We have to figure out which items are actually new so that we can properly isolate "items of this type that are already bought"
// and new items that we need to include in the max quantity values appropriately.
let newItemsOfType = this.state.itemsToBuy.filter(e => e.getClassName() == item.getClassName());
let newMax = newItemsOfType.reduce((acc, curr) => acc + curr.quantity, 0);
newMax += availableMerge;
newMax += ((maxStacks - postChangeInventory.length) * item.stackSize);
if (item._saleMax != 0) newMax = Math.min(newMax, item._saleMax);
item.maxQuantity = newMax;
});
}
let diffFromInitial = postChangeInventory.length - startingInventoryCount;
return [postChangeInventory.length, maxStacks, diffFromInitial];
}