Rewrote getCategoriesFromOffers() to make use of filter.reduce instaed of a giant loop

This commit is contained in:
Dev 2023-12-07 11:57:28 +00:00
parent ef9ecfd51d
commit 3f46cffcfe

View File

@ -24,48 +24,44 @@ export class RagfairCategoriesService
*/ */
public getCategoriesFromOffers(offers: IRagfairOffer[], searchRequestData: ISearchRequestData, fleaUnlocked: boolean): Record<string, number> public getCategoriesFromOffers(offers: IRagfairOffer[], searchRequestData: ISearchRequestData, fleaUnlocked: boolean): Record<string, number>
{ {
const validOffersForPlayerToSee = {}; // Get offers valid for search request, then reduce them down to just the counts
for (const offer of offers) return offers.filter(offer =>
{ {
const isTraderOffer = offer.user.memberType === MemberCategory.TRADER; const isTraderOffer = offer.user.memberType === MemberCategory.TRADER;
// Not level 15 and offer is from player, skip // Not level 15 and offer is from player, skip
if (!fleaUnlocked && !isTraderOffer) if (!fleaUnlocked && !isTraderOffer)
{ {
continue; return false;
} }
// Remove items not for money when `removeBartering` is enabled // Remove items not for money when `removeBartering` is enabled
if (searchRequestData.removeBartering && (offer.requirements.length > 1 || !this.paymentHelper.isMoneyTpl(offer.requirements[0]._tpl))) if (searchRequestData.removeBartering && (offer.requirements.length > 1 || !this.paymentHelper.isMoneyTpl(offer.requirements[0]._tpl)))
{ {
continue; return false;
} }
// Remove when filter set to players only + offer is from trader // Remove when filter set to players only + offer is from trader
if (searchRequestData.offerOwnerType === OfferOwnerType.PLAYEROWNERTYPE && isTraderOffer) if (searchRequestData.offerOwnerType === OfferOwnerType.PLAYEROWNERTYPE && isTraderOffer)
{ {
continue; return false;
} }
// Remove when filter set to traders only + offer is not from trader // Remove when filter set to traders only + offer is not from trader
if (searchRequestData.offerOwnerType === OfferOwnerType.TRADEROWNERTYPE && !isTraderOffer) if (searchRequestData.offerOwnerType === OfferOwnerType.TRADEROWNERTYPE && !isTraderOffer)
{ {
continue; return false;
} }
const itemTpl = offer.items[0]._tpl // Passed checks, its a valid offer to process
return true;
if (!validOffersForPlayerToSee[itemTpl]) }).reduce((acc, offer) =>
{ {
validOffersForPlayerToSee[itemTpl] = 1; const itemTpl = offer.items[0]._tpl;
} // Increment the category or add if doesnt exist
else acc[itemTpl] = (acc[itemTpl] || 0) + 1;
{
validOffersForPlayerToSee[itemTpl]++;
}
}
return validOffersForPlayerToSee;
return acc;
}, {});
} }
} }