Improve handling of buying less than max stacksize from fence

This commit is contained in:
Dev 2024-02-02 15:56:37 +00:00
parent 8dab47b3fd
commit 034ac83a10
2 changed files with 38 additions and 10 deletions

View File

@ -5,7 +5,7 @@ import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
import { TraderAssortHelper } from "@spt-aki/helpers/TraderAssortHelper"; import { TraderAssortHelper } from "@spt-aki/helpers/TraderAssortHelper";
import { TraderHelper } from "@spt-aki/helpers/TraderHelper"; import { TraderHelper } from "@spt-aki/helpers/TraderHelper";
import { IPmcData } from "@spt-aki/models/eft/common/IPmcData"; import { IPmcData } from "@spt-aki/models/eft/common/IPmcData";
import { Item, Upd } from "@spt-aki/models/eft/common/tables/IItem"; import { Item } from "@spt-aki/models/eft/common/tables/IItem";
import { IAddItemDirectRequest } from "@spt-aki/models/eft/inventory/IAddItemDirectRequest"; import { IAddItemDirectRequest } from "@spt-aki/models/eft/inventory/IAddItemDirectRequest";
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse"; import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData"; import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData";
@ -120,12 +120,12 @@ export class TradeHelper
{ {
// Update assort/flea item values // Update assort/flea item values
const traderAssorts = this.traderHelper.getTraderAssortsByTraderId(buyRequestData.tid).items; const traderAssorts = this.traderHelper.getTraderAssortsByTraderId(buyRequestData.tid).items;
const itemPurchased = traderAssorts.find((x) => x._id === buyRequestData.item_id); const itemPurchased = traderAssorts.find((assort) => assort._id === buyRequestData.item_id);
// Decrement trader item count // Decrement trader item count
itemPurchased.upd.StackObjectsCount -= buyCount; itemPurchased.upd.StackObjectsCount -= buyCount;
this.fenceService.removeFenceOffer(buyRequestData.item_id); this.fenceService.amendOrRemoveFenceOffer(buyRequestData.item_id, buyCount);
}; };
const fenceItems = this.fenceService.getRawFenceAssorts().items; const fenceItems = this.fenceService.getRawFenceAssorts().items;

View File

@ -897,21 +897,49 @@ export class FenceService
} }
/** /**
* Remove an assort from fence by id * Remove or lower stack size of an assort from fence by id
* @param assortIdToRemove assort id to remove from fence assorts * @param assortId assort id to adjust
* @param buyCount Count of items bought
*/ */
public removeFenceOffer(assortIdToRemove: string): void public amendOrRemoveFenceOffer(assortId: string, buyCount: number): void
{
let fenceAssortItem = this.fenceAssort.items.find(item => item._id === assortId);
if (!fenceAssortItem)
{
// Not in main assorts, check secondary section
fenceAssortItem = this.fenceDiscountAssort.items.find(item => item._id === assortId);
if (!fenceAssortItem)
{
this.logger.error(`Offer with id: ${assortId} not found`);
return;
}
}
// Player wants to buy whole stack, delete stack
if (fenceAssortItem.upd.StackObjectsCount === buyCount)
{
this.deleteOffer(assortId, this.fenceAssort.items);
return;
}
// Adjust stack size
fenceAssortItem.upd.StackObjectsCount -= buyCount;
}
protected deleteOffer(assortId: string, assorts: Item[]): void
{ {
// Assort could have child items, remove those too // Assort could have child items, remove those too
const itemWithChildrenToRemove = this.itemHelper.findAndReturnChildrenAsItems(this.fenceAssort.items, assortIdToRemove); const itemWithChildrenToRemove = this.itemHelper.findAndReturnChildrenAsItems(assorts, assortId);
for (const itemToRemove of itemWithChildrenToRemove) for (const itemToRemove of itemWithChildrenToRemove)
{ {
let indexToRemove = this.fenceAssort.items.findIndex(item => item._id === itemToRemove._id); let indexToRemove = assorts.findIndex(item => item._id === itemToRemove._id);
// No offer found in main assort, check discount items // No offer found in main assort, check discount items
if (indexToRemove === -1) if (indexToRemove === -1)
{ {
indexToRemove = this.fenceDiscountAssort.items.findIndex((i) => i._id === itemToRemove._id); indexToRemove = this.fenceDiscountAssort.items.findIndex((item) => item._id === itemToRemove._id);
this.fenceDiscountAssort.items.splice(indexToRemove, 1); this.fenceDiscountAssort.items.splice(indexToRemove, 1);
if (indexToRemove === -1) if (indexToRemove === -1)
@ -923,7 +951,7 @@ export class FenceService
} }
// Remove offer from assort // Remove offer from assort
this.fenceAssort.items.splice(indexToRemove, 1); assorts.splice(indexToRemove, 1);
} }
} }
} }