From 140a1e8c00d4d90b572ab589db2304a649863cb8 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 22 Dec 2024 19:49:57 +0000 Subject: [PATCH] Added filtering of offers that have exceeded the buy limit threshold from possible offers to display 0 - Fixed a limit-reached trader offers always showing over a PMC offer Various improvements to `getOffersForBuild`: Renamed offers to offersToReturn Removed one layer of indentation by using early return Replaced use of `ragfairOfferService.getOffers` with `ragfairOfferService.getOffersOfType` to improve performance by using a more appropriately indexed dictionary of offers Run `getOffersInsideBuyRestrictionLimits` as part of offer filtering process removed redundant reassignment of availableOffers/possibleOffers --- project/src/helpers/RagfairOfferHelper.ts | 87 +++++++++++++++++------ 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/project/src/helpers/RagfairOfferHelper.ts b/project/src/helpers/RagfairOfferHelper.ts index 57064e34..55a9d859 100644 --- a/project/src/helpers/RagfairOfferHelper.ts +++ b/project/src/helpers/RagfairOfferHelper.ts @@ -208,22 +208,36 @@ export class RagfairOfferHelper { pmcData: IPmcData, ): IRagfairOffer[] { const offersMap = new Map(); - const offers: IRagfairOffer[] = []; + const offersToReturn: IRagfairOffer[] = []; const playerIsFleaBanned = this.profileHelper.playerIsFleaBanned(pmcData); const tieredFlea = this.ragfairConfig.tieredFlea; const tieredFleaLimitTypes = Object.keys(tieredFlea.unlocksType); - for (const offer of this.ragfairOfferService.getOffers()) { - // Dont show pack offers - if (offer.sellInOnePiece) { - continue; - } + for (const desiredItemTpl of Object.keys(searchRequest.buildItems)) { + const matchingOffers = this.ragfairOfferService.getOffersOfType(desiredItemTpl); + for (const offer of matchingOffers) { + // Dont show pack offers + if (offer.sellInOnePiece) { + continue; + } - if (!this.passesSearchFilterCriteria(searchRequest, offer, pmcData)) { - continue; - } + if (!this.passesSearchFilterCriteria(searchRequest, offer, pmcData)) { + continue; + } + + if ( + !this.isDisplayableOffer( + searchRequest, + itemsToAdd, + traderAssorts, + offer, + pmcData, + playerIsFleaBanned, + ) + ) { + continue; + } - if (this.isDisplayableOffer(searchRequest, itemsToAdd, traderAssorts, offer, pmcData, playerIsFleaBanned)) { const isTraderOffer = offer.user.memberType === MemberCategory.TRADER; if (isTraderOffer) { if (this.traderBuyRestrictionReached(offer)) { @@ -271,17 +285,41 @@ export class RagfairOfferHelper { const lockedOffers = this.getLoyaltyLockedOffers(possibleOffers, pmcData); // Exclude locked offers + above loyalty locked offers if at least 1 was found - const availableOffers = possibleOffers.filter((x) => !(x.locked || lockedOffers.includes(x._id))); - if (availableOffers.length > 0) { - possibleOffers = availableOffers; + possibleOffers = possibleOffers.filter((offer) => !(offer.locked || lockedOffers.includes(offer._id))); + + // Exclude trader offers over their buy restriction limit + possibleOffers = this.getOffersInsideBuyRestrictionLimits(possibleOffers); + } + + // Sort offers by price and pick the best + const offer = this.ragfairSortHelper.sortOffers(possibleOffers, RagfairSort.PRICE, 0)[0]; + offersToReturn.push(offer); + } + + return offersToReturn; + } + + /** + * Get offers that have not exceeded buy limits + * @param possibleOffers offers to process + * @returns Offers + */ + protected getOffersInsideBuyRestrictionLimits(possibleOffers: IRagfairOffer[]) { + // Check offer has buy limit + is from trader + current buy count is at or over max + return possibleOffers.filter((offer) => { + if ( + typeof offer.buyRestrictionMax !== "undefined" && + this.offerIsFromTrader(offer) && + offer.buyRestrictionCurrent >= offer.buyRestrictionMax + ) { + if (offer.buyRestrictionCurrent >= offer.buyRestrictionMax) { + return false; } } - const offer = this.ragfairSortHelper.sortOffers(possibleOffers, RagfairSort.PRICE, 0)[0]; - offers.push(offer); - } - - return offers; + // Doesnt have buy limits, retrun offer + return true; + }); } /** @@ -622,7 +660,7 @@ export class RagfairOfferHelper { searchRequest.oneHourExpiration && offer.endTime - this.timeUtil.getTimestamp() > TimeUtil.ONE_HOUR_AS_SECONDS ) { - // offer doesnt expire within an hour + // offer expires within an hour return false; } @@ -690,7 +728,7 @@ export class RagfairOfferHelper { * @returns True if the given item is functional */ public isItemFunctional(offerRootItem: IItem, offer: IRagfairOffer): boolean { - // Non-presets are always functional + // Non-preset weapons/armor are always functional if (!this.presetHelper.hasPreset(offerRootItem._tpl)) { return true; } @@ -837,4 +875,13 @@ export class RagfairOfferHelper { return true; } + + /** + * Does this offer come from a trader + * @param offer Offer to check + * @returns True = from trader + */ + public offerIsFromTrader(offer: IRagfairOffer) { + return offer.user.memberType === MemberCategory.TRADER; + } }