improve filtering of required item searches on flea - Fixes locked items being shown in search results

Improve type usage inside `getRequiredItemsById()`
This commit is contained in:
Dev 2024-02-03 10:33:11 +00:00
parent f2cae309ec
commit 47bedcb526
3 changed files with 31 additions and 53 deletions

View File

@ -103,12 +103,6 @@ export class RagfairController
result.categories = this.getSpecificCategories(pmcProfile, searchRequest, result.offers);
}
// Client requested "required search"
if (searchRequest.neededSearchId)
{
this.addRequiredOffersToResult(searchRequest, traderAssorts, pmcProfile, result);
}
this.addIndexValueToOffers(result.offers);
// Sort offers
@ -186,7 +180,7 @@ export class RagfairController
if (searchRequest.neededSearchId?.length > 0)
{
return this.ragfairOfferHelper.getOffersThatRequireItem(searchRequest);
return this.ragfairOfferHelper.getOffersThatRequireItem(searchRequest, pmcProfile);
}
// Searching for general items
@ -213,7 +207,7 @@ export class RagfairController
{
offerPool = offers;
}
else if ((searchRequest.linkedSearchId === "" && searchRequest.neededSearchId === ""))
else if ((!this.isLinkedSearch(searchRequest) && !this.isRequiredSearch(searchRequest)))
{
// Get all categories
offerPool = this.ragfairOfferService.getOffers();
@ -228,30 +222,6 @@ export class RagfairController
return this.ragfairServer.getAllActiveCategories(playerHasFleaUnlocked, searchRequest, offerPool);
}
/**
* Add Required offers to offers result
* @param searchRequest Client search request data
* @param assorts
* @param pmcProfile Player profile
* @param result Result object being sent back to client
*/
protected addRequiredOffersToResult(
searchRequest: ISearchRequestData,
assorts: Record<string, ITraderAssort>,
pmcProfile: IPmcData,
result: IGetOffersResult,
): void
{
const requiredOffers = this.ragfairRequiredItemsService.getRequiredItemsById(searchRequest.neededSearchId);
for (const requiredOffer of requiredOffers)
{
if (this.ragfairOfferHelper.isDisplayableOffer(searchRequest, null, assorts, requiredOffer, pmcProfile))
{
result.offers.push(requiredOffer);
}
}
}
/**
* Add index to all offers passed in (0-indexed)
* @param offers Offers to add index value to

View File

@ -31,6 +31,7 @@ import { LocaleService } from "@spt-aki/services/LocaleService";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
import { MailSendService } from "@spt-aki/services/MailSendService";
import { RagfairOfferService } from "@spt-aki/services/RagfairOfferService";
import { RagfairRequiredItemsService } from "@spt-aki/services/RagfairRequiredItemsService";
import { HashUtil } from "@spt-aki/utils/HashUtil";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
@ -57,6 +58,7 @@ export class RagfairOfferHelper
@inject("RagfairSortHelper") protected ragfairSortHelper: RagfairSortHelper,
@inject("RagfairHelper") protected ragfairHelper: RagfairHelper,
@inject("RagfairOfferService") protected ragfairOfferService: RagfairOfferService,
@inject("RagfairRequiredItemsService") protected ragfairRequiredItemsService: RagfairRequiredItemsService,
@inject("LocaleService") protected localeService: LocaleService,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("MailSendService") protected mailSendService: MailSendService,
@ -82,16 +84,34 @@ export class RagfairOfferHelper
pmcProfile: IPmcData,
): IRagfairOffer[]
{
return this.ragfairOfferService.getOffers().filter((x) =>
this.isDisplayableOffer(searchRequest, itemsToAdd, traderAssorts, x, pmcProfile)
return this.ragfairOfferService.getOffers().filter((offer) =>
this.isDisplayableOffer(searchRequest, itemsToAdd, traderAssorts, offer, pmcProfile)
);
}
public getOffersThatRequireItem(searchRequest: ISearchRequestData): IRagfairOffer[]
/**
* Get matching offers that require the desired item and filter out offers from non traders if player is below ragfair unlock level
* @param searchRequest Search request from client
* @param pmcDataPlayer profile
* @returns Matching IRagfairOffer objects
*/
public getOffersThatRequireItem(searchRequest: ISearchRequestData, pmcData: IPmcData): IRagfairOffer[]
{
return this.ragfairOfferService.getOffers().filter((offer) =>
this.isDisplayableOfferThatNeedsItem(searchRequest, offer)
);
// Get all offers that requre the desired item and filter out offers from non traders if player below ragifar unlock
const requiredOffers = this.ragfairRequiredItemsService.getRequiredItemsById(searchRequest.neededSearchId);
return requiredOffers.filter((offer: IRagfairOffer) =>
{
if (
pmcData.Info.Level < this.databaseServer.getTables().globals.config.RagFair.minUserLevel
&& offer.user.memberType === MemberCategory.DEFAULT
)
{
// Skip item if player is < global unlock level (default is 15) and item is from a dynamically generated source (non-trader)
return false;
}
return true;
});
}
/**
@ -684,19 +704,6 @@ export class RagfairOfferHelper
return true;
}
public isDisplayableOfferThatNeedsItem(
searchRequest: ISearchRequestData,
offer: IRagfairOffer,
): boolean
{
if (offer.requirements.some(requirement => requirement._tpl === searchRequest.neededSearchId))
{
return true;
}
return false;
}
/**
* Does the passed in item have a condition property
* @param item Item to check

View File

@ -1,6 +1,7 @@
import { inject, injectable } from "tsyringe";
import { PaymentHelper } from "@spt-aki/helpers/PaymentHelper";
import { IRagfairOffer } from "@spt-aki/models/eft/ragfair/IRagfairOffer";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { RagfairOfferService } from "@spt-aki/services/RagfairOfferService";
@ -16,9 +17,9 @@ export class RagfairRequiredItemsService
)
{}
public getRequiredItemsById(searchId: string): any
public getRequiredItemsById(searchId: string): IRagfairOffer[]
{
return this.requiredItemsCache[searchId] || [];
return Array.from(this.requiredItemsCache[searchId]) || [];
}
public buildRequiredItemTable(): void