b69544ae2c
Made `RagfairRequiredItemService` a singleton Made `RagfairRequiredItemService ` store offerIds instead of offer objects, reducing memory footprint Reworked `GetOffersThatRequireItem` to work with `RagfairRequiredItemService` changes Moved `GenerateDynamicOffers` to run after garbage colelction, this means GC will work on memory prior to new offers being added but after stale offers are removed Made `PaymentHelper` a singleton + Store currency tpls in a hashset instead of list Comment improvements
55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Collections.Concurrent;
|
|
using SPTarkov.DI.Annotations;
|
|
using SPTarkov.Server.Core.Helpers;
|
|
|
|
namespace SPTarkov.Server.Core.Services;
|
|
|
|
[Injectable(InjectionType.Singleton)]
|
|
public class RagfairRequiredItemsService(
|
|
RagfairOfferService ragfairOfferService,
|
|
PaymentHelper paymentHelper)
|
|
{
|
|
/// <summary>
|
|
/// Key = tpl
|
|
/// </summary>
|
|
protected readonly ConcurrentDictionary<string, HashSet<string>> _requiredItemsCache = new();
|
|
|
|
/// <summary>
|
|
/// Get the offerId of offers that require the supplied tpl
|
|
/// </summary>
|
|
/// <param name="tpl">Tpl to find offers ids for</param>
|
|
/// <returns></returns>
|
|
public HashSet<string> GetRequiredOffersById(string tpl)
|
|
{
|
|
if (_requiredItemsCache.TryGetValue(tpl, out var offerIds))
|
|
{
|
|
return offerIds;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a cache of requirements to purchase item
|
|
/// </summary>
|
|
public void BuildRequiredItemTable()
|
|
{
|
|
_requiredItemsCache.Clear();
|
|
foreach (var offer in ragfairOfferService.GetOffers())
|
|
foreach (var requirement in offer.Requirements)
|
|
{
|
|
if (paymentHelper.IsMoneyTpl(requirement.Template))
|
|
// This would just be too noisy
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Ensure key is init
|
|
_requiredItemsCache.TryAdd(requirement.Template, []);
|
|
|
|
// Add matching offer
|
|
_requiredItemsCache.GetValueOrDefault(requirement.Template)?.Add(offer.Id);
|
|
}
|
|
}
|
|
}
|