From f263f8b0cb3aa7fc0d584fb04d5af9a389208c88 Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 8 Jun 2024 19:38:16 +0100 Subject: [PATCH] Added implementation of BSG feature `Increased personal trader item limits by 20% for Edge of Darkness and The Unheard edition owners who upgraded from Edge of Darkness` This fixes EoD/UH/DEV profiles throwing errors when purchasing items near their trader BuyRestrictionMax limits --- project/src/helpers/TradeHelper.ts | 23 +++++++++++++------ project/src/helpers/TraderHelper.ts | 35 ++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/project/src/helpers/TradeHelper.ts b/project/src/helpers/TradeHelper.ts index 96b5fdfc..7bafca09 100644 --- a/project/src/helpers/TradeHelper.ts +++ b/project/src/helpers/TradeHelper.ts @@ -88,6 +88,7 @@ export class TradeHelper { this.checkPurchaseIsWithinTraderItemLimit( sessionID, + pmcData, buyRequestData.tid, itemPurchased, buyRequestData.item_id, @@ -151,6 +152,7 @@ export class TradeHelper // Will throw error if check fails this.checkPurchaseIsWithinTraderItemLimit( sessionID, + pmcData, buyRequestData.tid, itemPurchased, buyRequestData.item_id, @@ -293,6 +295,7 @@ export class TradeHelper /** * Traders allow a limited number of purchases per refresh cycle (default 60 mins) * @param sessionId Session id + * @param pmcData Profile making the purchase * @param traderId Trader assort is purchased from * @param assortBeingPurchased the item from trader being bought * @param assortId Id of assort being purchased @@ -300,21 +303,27 @@ export class TradeHelper */ protected checkPurchaseIsWithinTraderItemLimit( sessionId: string, + pmcData: IPmcData, traderId: string, assortBeingPurchased: Item, assortId: string, count: number, ): void { - const traderPurchaseData = this.traderPurchasePersisterService.getProfileTraderPurchase( - sessionId, - traderId, - assortBeingPurchased._id, - ); - if ((traderPurchaseData?.count ?? 0 + count) > assortBeingPurchased.upd?.BuyRestrictionMax) + const traderPurchaseData + = this.traderPurchasePersisterService.getProfileTraderPurchase( + sessionId, + traderId, + assortBeingPurchased._id, + ); + const traderItemPurchaseLimit + = this.traderHelper.getAccountTypeAdjustedTraderPurchaseLimit( + assortBeingPurchased.upd?.BuyRestrictionMax, + pmcData.Info.GameVersion); + if ((traderPurchaseData?.count ?? 0 + count) > traderItemPurchaseLimit) { 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`, + `Unable to purchase: ${count} items, this would exceed your purchase limit of ${traderItemPurchaseLimit} from the trader: ${traderId} assort: ${assortId} this refresh`, ); } } diff --git a/project/src/helpers/TraderHelper.ts b/project/src/helpers/TraderHelper.ts index 2558a601..5fdd8a1d 100644 --- a/project/src/helpers/TraderHelper.ts +++ b/project/src/helpers/TraderHelper.ts @@ -410,18 +410,15 @@ export class TraderHelper // Iterate over assorts bought and add to profile for (const purchasedItem of newPurchaseDetails.items) { - if (!profile.traderPurchases) - { - profile.traderPurchases = {}; - } + const currentTime = this.timeUtil.getTimestamp(); - if (!profile.traderPurchases[traderId]) - { - profile.traderPurchases[traderId] = {}; - } + // Nullguard traderPurchases + profile.traderPurchases ||= {}; + // Nullguard traderPurchases for this trader + profile.traderPurchases[traderId] ||= {}; // Null guard when dict doesnt exist - const currentTime = this.timeUtil.getTimestamp(); + if (!profile.traderPurchases[traderId][purchasedItem.itemId]) { profile.traderPurchases[traderId][purchasedItem.itemId] = { @@ -434,7 +431,9 @@ export class TraderHelper if ( profile.traderPurchases[traderId][purchasedItem.itemId].count + purchasedItem.count - > itemPurchased.upd!.BuyRestrictionMax! + > this.getAccountTypeAdjustedTraderPurchaseLimit( + itemPurchased.upd!.BuyRestrictionMax!, + profile.characters.pmc.Info.GameVersion) ) { throw new Error( @@ -450,6 +449,22 @@ export class TraderHelper } } + /** + * EoD and Unheard get a 20% bonus to personal trader limit purchases + * @param buyRestrictionMax Existing value from trader item + * @param gameVersion Profiles game version + * @returns buyRestrictionMax value + */ + public getAccountTypeAdjustedTraderPurchaseLimit(buyRestrictionMax: number, gameVersion: string): number + { + if (["edge_of_darkness", "unheard"].includes(gameVersion)) + { + return Math.floor(buyRestrictionMax * 1.2); + } + + return buyRestrictionMax; + } + /** * Get the highest rouble price for an item from traders * UNUSED