From fdbbc47e5920a825ef5219548b84e49b5ddf5427 Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Sat, 23 Mar 2024 19:18:14 +0000 Subject: [PATCH] Fix calculations for the flea min, max and avg to more accurately match live (!268) Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/268 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/src/controllers/RagfairController.ts | 36 +++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/project/src/controllers/RagfairController.ts b/project/src/controllers/RagfairController.ts index 3422a1b5..a0ffe20f 100644 --- a/project/src/controllers/RagfairController.ts +++ b/project/src/controllers/RagfairController.ts @@ -342,31 +342,51 @@ export class RagfairController */ public getItemMinAvgMaxFleaPriceValues(getPriceRequest: IGetMarketPriceRequestData): IGetItemPriceResult { - // Get all items of tpl (sort by price) - let offers = this.ragfairOfferService.getOffersOfType(getPriceRequest.templateId); + // Get all items of tpl + const offers = this.ragfairOfferService.getOffersOfType(getPriceRequest.templateId); // Offers exist for item, get averages of what's listed if (typeof offers === "object" && offers.length > 0) { - offers = this.ragfairSortHelper.sortOffers(offers, RagfairSort.PRICE); - const min = offers[0].requirementsCost; // Get first item from array as its pre-sorted - const max = offers.at(-1).requirementsCost; // Get last item from array as its pre-sorted + // These get calculated while iterating through the list below + let min = Number.MAX_VALUE; + let max = 0; // Get the average offer price, excluding barter offers let avgOfferCount = 0; const avg = offers.reduce((sum, offer) => { - // Exclude barter items + // Exclude barter items, they tend to have outrageous equivalent prices if (offer.requirements.some((req) => !this.paymentHelper.isMoneyTpl(req._tpl))) { return sum; } + // Figure out how many items the requirementsCost is applying to, and what the per-item price is + const offerItemCount = Math.max(offer.sellInOnePiece ? offer.items[0].upd?.StackObjectsCount ?? 1 : 1); + const perItemPrice = offer.requirementsCost / offerItemCount; + + // Handle min/max calculations based on the per-item price + if (perItemPrice < min) + { + min = perItemPrice; + } + else if (perItemPrice > max) + { + max = perItemPrice; + } + avgOfferCount++; - return sum + offer.requirementsCost; + return sum + perItemPrice; }, 0) / Math.max(avgOfferCount, 1); - return { avg: avg, min: min, max: max }; + // If no items were actually counted, min will still be MAX_VALUE, so set it to 0 + if (min === Number.MAX_VALUE) + { + min = 0; + } + + return { avg: Math.round(avg), min: min, max: max }; } // No offers listed, get price from live ragfair price list prices.json