Improved handling of a missing updobject inside removeRandomItemFromAssorts()

This commit is contained in:
Dev 2024-04-22 17:54:32 +01:00
parent fe7d09e075
commit be841fbe3f

View File

@ -387,8 +387,8 @@ export class FenceService
/**
* Choose an item at random and remove it + mods from assorts
* @param assort Items to remove from
* @param rootItems Assort root items to pick from to remove
* @param assort Trader assort to remove item from
* @param rootItems Pool of root items to pick from to remove
*/
protected removeRandomItemFromAssorts(assort: ITraderAssort, rootItems: Item[]): void
{
@ -399,14 +399,24 @@ export class FenceService
// Get a random count of the chosen item to remove
const itemCountToRemove = this.randomUtil.getInt(1, stackSize);
if (itemCountToRemove > 1 && itemCountToRemove < rootItemToAdjust.upd.StackObjectsCount)
{ // More than 1 + less then full stack
// Reduce stack size but keep stack
rootItemToAdjust.upd.StackObjectsCount -= itemCountToRemove;
}
else
const isEntireStackToBeRemoved = itemCountToRemove === stackSize;
// Partial stack reduction
if (!isEntireStackToBeRemoved)
{
// Remove up item + any mods
if (!rootItemToAdjust.upd)
{
rootItemToAdjust.upd = {};
}
// Reduce stack to at smallest, 1
rootItemToAdjust.upd.StackObjectsCount -= Math.max(1, itemCountToRemove);
return;
}
// Remove item + child mods (if any)
const itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems(assort.items, rootItemToAdjust._id);
for (const itemToDelete of itemWithChildren)
{
@ -414,10 +424,10 @@ export class FenceService
assort.items.splice(assort.items.indexOf(itemToDelete), 1);
}
// Need to remove item from all areas of trader assort
delete assort.barter_scheme[rootItemToAdjust._id];
delete assort.loyal_level_items[rootItemToAdjust._id];
}
}
/**
* Get an integer rounded count of items to replace based on percentrage from traderConfig value