From a1f043d088fedcb65df6b6621413db5dbe8e67b7 Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 20 Jan 2024 00:19:13 +0000 Subject: [PATCH] Fix issie with `BuyRestrictionCurrent` value persisting between profiles --- project/src/helpers/TradeHelper.ts | 15 ++++++++----- project/src/helpers/TraderAssortHelper.ts | 21 +++++++++++++++++++ .../TraderPurchasePersisterService.ts | 19 +++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/project/src/helpers/TradeHelper.ts b/project/src/helpers/TradeHelper.ts index e0aaae51..0a1654d5 100644 --- a/project/src/helpers/TradeHelper.ts +++ b/project/src/helpers/TradeHelper.ts @@ -22,6 +22,7 @@ import { RagfairServer } from "@spt-aki/servers/RagfairServer"; import { FenceService } from "@spt-aki/services/FenceService"; import { LocalisationService } from "@spt-aki/services/LocalisationService"; import { PaymentService } from "@spt-aki/services/PaymentService"; +import { TraderPurchasePersisterService } from "@spt-aki/services/TraderPurchasePersisterService"; import { HttpResponseUtil } from "@spt-aki/utils/HttpResponseUtil"; import { JsonUtil } from "@spt-aki/utils/JsonUtil"; @@ -44,6 +45,7 @@ export class TradeHelper @inject("InventoryHelper") protected inventoryHelper: InventoryHelper, @inject("RagfairServer") protected ragfairServer: RagfairServer, @inject("TraderAssortHelper") protected traderAssortHelper: TraderAssortHelper, + @inject("TraderPurchasePersisterService") protected traderPurchasePersisterService: TraderPurchasePersisterService, @inject("ConfigServer") protected configServer: ConfigServer, ) { @@ -84,7 +86,7 @@ export class TradeHelper const assortHasBuyRestrictions = this.itemHelper.hasBuyRestrictions(itemPurchased); if (assortHasBuyRestrictions) { - this.checkPurchaseIsWithinTraderItemLimit(itemPurchased, buyRequestData.item_id, buyCount); + this.checkPurchaseIsWithinTraderItemLimit(sessionID, buyRequestData.tid, itemPurchased, buyRequestData.item_id, buyCount); } // Decrement trader item count @@ -153,7 +155,7 @@ export class TradeHelper const assortHasBuyRestrictions = this.itemHelper.hasBuyRestrictions(itemPurchased); if (assortHasBuyRestrictions) { - this.checkPurchaseIsWithinTraderItemLimit(itemPurchased, buyRequestData.item_id, buyCount); + this.checkPurchaseIsWithinTraderItemLimit(sessionID, buyRequestData.tid, itemPurchased, buyRequestData.item_id, buyCount); } // Decrement trader item count @@ -291,13 +293,16 @@ export class TradeHelper /** * Traders allow a limited number of purchases per refresh cycle (default 60 mins) + * @param sessionId Session id + * @param traderId Trader assort is purchased from * @param assortBeingPurchased the item from trader being bought * @param assortId Id of assort being purchased - * @param count How many are being bought + * @param count How many of the item are being bought */ - protected checkPurchaseIsWithinTraderItemLimit(assortBeingPurchased: Item, assortId: string, count: number): void + protected checkPurchaseIsWithinTraderItemLimit(sessionId: string, traderId: string, assortBeingPurchased: Item, assortId: string, count: number): void { - if ((assortBeingPurchased.upd.BuyRestrictionCurrent + count) > assortBeingPurchased.upd?.BuyRestrictionMax) + const traderPurchaseData = this.traderPurchasePersisterService.getProfileTraderPurchase(sessionId, traderId, assortBeingPurchased._id); + if ((traderPurchaseData?.count ?? 0 + count) > assortBeingPurchased.upd?.BuyRestrictionMax) { throw new Error( `Unable to purchase ${count} items, this would exceed your purchase limit of ${assortBeingPurchased.upd.BuyRestrictionMax} from the traders assort: ${assortId} this refresh`, diff --git a/project/src/helpers/TraderAssortHelper.ts b/project/src/helpers/TraderAssortHelper.ts index 0747cdea..9045eda8 100644 --- a/project/src/helpers/TraderAssortHelper.ts +++ b/project/src/helpers/TraderAssortHelper.ts @@ -83,6 +83,8 @@ export class TraderAssortHelper trader.assort = this.assortHelper.stripLockedLoyaltyAssort(pmcProfile, traderId, trader.assort); } + this.resetBuyRestrictionCurrentValue(trader.assort.items); + // Append nextResupply value to assorts so client knows when refresh is occuring trader.assort.nextResupply = trader.base.nextResupply; @@ -139,6 +141,25 @@ export class TraderAssortHelper return trader.assort; } + /** + * Reset every traders root item `BuyRestrictionCurrent` property to 0 + * @param assortItems Items to adjust + */ + protected resetBuyRestrictionCurrentValue(assortItems: Item[]): void + { + // iterate over root items + for (const assort of assortItems.filter(item => item.slotId === "hideout")) + { + // no value to adjust + if (!assort.upd.BuyRestrictionCurrent) + { + continue; + } + + assort.upd.BuyRestrictionCurrent = 0; + } + } + /** * Create a dict of all assort id = quest id mappings used to work out what items should be shown to player based on the quests they've started/completed/failed */ diff --git a/project/src/services/TraderPurchasePersisterService.ts b/project/src/services/TraderPurchasePersisterService.ts index ff0deee9..5c4aaa1b 100644 --- a/project/src/services/TraderPurchasePersisterService.ts +++ b/project/src/services/TraderPurchasePersisterService.ts @@ -46,6 +46,25 @@ export class TraderPurchasePersisterService return profile.traderPurchases[traderId]; } + /** + * Get a purchase made from a trader for requested profile before the last trader reset + * @param sessionId Session id + * @param traderId Trader to loop up purchases for + * @param assortId Id of assort to get data for + * @returns TraderPurchaseData + */ + public getProfileTraderPurchase(sessionId: string, traderId: string, assortId: string): TraderPurchaseData + { + const profile = this.profileHelper.getFullProfile(sessionId); + + if (!profile.traderPurchases) + { + return null; + } + + return profile.traderPurchases[traderId][assortId]; + } + /** * Remove all trader purchase records from all profiles that exist * @param traderId Traders id