Added lock to GetStaleOfferIds
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
This commit is contained in:
@@ -5,23 +5,28 @@ using SPTarkov.Server.Core.Servers;
|
||||
|
||||
namespace SPTarkov.Server.Core.Helpers;
|
||||
|
||||
[Injectable]
|
||||
public class PaymentHelper(ConfigServer _configServer)
|
||||
[Injectable(InjectionType.Singleton)]
|
||||
public class PaymentHelper(ConfigServer configServer)
|
||||
{
|
||||
protected bool _addedCustomMoney;
|
||||
protected InventoryConfig _inventoryConfig = _configServer.GetConfig<InventoryConfig>();
|
||||
protected List<string> _moneyTpls = [Money.DOLLARS, Money.EUROS, Money.ROUBLES, Money.GP];
|
||||
protected readonly InventoryConfig _inventoryConfig = configServer.GetConfig<InventoryConfig>();
|
||||
protected readonly HashSet<string> _moneyTpls = [Money.DOLLARS, Money.EUROS, Money.ROUBLES, Money.GP];
|
||||
|
||||
/// <summary>
|
||||
/// Is the passed in tpl money (also checks custom currencies in inventoryConfig.customMoneyTpls)
|
||||
/// </summary>
|
||||
/// <param name="tpl"></param>
|
||||
/// <param name="tpl">Item Tpl to check</param>
|
||||
/// <returns></returns>
|
||||
public bool IsMoneyTpl(string tpl)
|
||||
{
|
||||
// Add custom currency first time this method is accessed
|
||||
if (!_addedCustomMoney)
|
||||
{
|
||||
_moneyTpls.AddRange(_inventoryConfig.CustomMoneyTpls);
|
||||
foreach (var customMoney in _inventoryConfig.CustomMoneyTpls)
|
||||
{
|
||||
_moneyTpls.Add(customMoney);
|
||||
}
|
||||
|
||||
_addedCustomMoney = true;
|
||||
}
|
||||
|
||||
@@ -32,7 +37,7 @@ public class PaymentHelper(ConfigServer _configServer)
|
||||
/// Gets currency TPL from TAG
|
||||
/// </summary>
|
||||
/// <param name="currency"></param>
|
||||
/// <returns></returns>
|
||||
/// <returns>Tpl of currency</returns>
|
||||
public string GetCurrency(CurrencyType? currency)
|
||||
{
|
||||
return currency switch
|
||||
@@ -41,7 +46,7 @@ public class PaymentHelper(ConfigServer _configServer)
|
||||
CurrencyType.USD => Money.DOLLARS,
|
||||
CurrencyType.RUB => Money.ROUBLES,
|
||||
CurrencyType.GP => Money.GP,
|
||||
_ => ""
|
||||
_ => string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,31 +168,30 @@ public class RagfairOfferHelper(
|
||||
public List<RagfairOffer> GetOffersThatRequireItem(SearchRequestData searchRequest, PmcData pmcData)
|
||||
{
|
||||
// Get all offers that require the desired item and filter out offers from non traders if player below ragfair unlock
|
||||
var requiredOffers = _ragfairRequiredItemsService.GetRequiredItemsById(searchRequest.NeededSearchId);
|
||||
var offerIDsForItem = _ragfairRequiredItemsService.GetRequiredOffersById(searchRequest.NeededSearchId);
|
||||
|
||||
var tieredFlea = _ragfairConfig.TieredFlea;
|
||||
var tieredFleaLimitTypes = tieredFlea.UnlocksType;
|
||||
return requiredOffers.Where(offer =>
|
||||
{
|
||||
if (!PassesSearchFilterCriteria(searchRequest, offer, pmcData))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tieredFlea.Enabled && !OfferIsFromTrader(offer))
|
||||
{
|
||||
CheckAndLockOfferFromPlayerTieredFlea(
|
||||
tieredFlea,
|
||||
offer,
|
||||
tieredFleaLimitTypes.Keys.ToList(),
|
||||
pmcData.Info.Level.Value
|
||||
);
|
||||
}
|
||||
var result = new List<RagfairOffer>();
|
||||
foreach (var offer in offerIDsForItem
|
||||
.Select(_ragfairOfferService.GetOfferByOfferId)
|
||||
.Where(offer => PassesSearchFilterCriteria(searchRequest, offer, pmcData)))
|
||||
{
|
||||
if (tieredFlea.Enabled && !OfferIsFromTrader(offer))
|
||||
{
|
||||
CheckAndLockOfferFromPlayerTieredFlea(
|
||||
tieredFlea,
|
||||
offer,
|
||||
tieredFleaLimitTypes.Keys.ToList(),
|
||||
pmcData.Info.Level.Value
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
)
|
||||
.ToList();
|
||||
result.Add(offer);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -57,11 +57,13 @@ public class RagfairServer(
|
||||
// Must occur BEFORE "RemoveExpiredOffers"
|
||||
var expiredAssortsWithChildren = _ragfairOfferHolder.GetExpiredOfferItems();
|
||||
|
||||
_ragfairOfferService.RemoveExpiredOffers();
|
||||
|
||||
// Force a cleanup+compact now all the expired offers are gone
|
||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, true, true);
|
||||
|
||||
// Replace the expired offers with new ones
|
||||
_ragfairOfferGenerator.GenerateDynamicOffers(expiredAssortsWithChildren);
|
||||
|
||||
_ragfairOfferService.RemoveExpiredOffers();
|
||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, true, true);
|
||||
}
|
||||
|
||||
_ragfairRequiredItemsService.BuildRequiredItemTable();
|
||||
|
||||
@@ -1,34 +1,44 @@
|
||||
using System.Collections.Concurrent;
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Helpers;
|
||||
using SPTarkov.Server.Core.Models.Eft.Ragfair;
|
||||
|
||||
namespace SPTarkov.Server.Core.Services;
|
||||
|
||||
[Injectable(InjectionType.Singleton)]
|
||||
public class RagfairRequiredItemsService(
|
||||
RagfairOfferService _ragfairOfferService,
|
||||
PaymentHelper _paymentHelper)
|
||||
RagfairOfferService ragfairOfferService,
|
||||
PaymentHelper paymentHelper)
|
||||
{
|
||||
protected ConcurrentDictionary<string, List<RagfairOffer>> _requiredItemsCache = new();
|
||||
/// <summary>
|
||||
/// Key = tpl
|
||||
/// </summary>
|
||||
protected readonly ConcurrentDictionary<string, HashSet<string>> _requiredItemsCache = new();
|
||||
|
||||
public List<RagfairOffer> GetRequiredItemsById(string searchId)
|
||||
/// <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(searchId, out var list))
|
||||
if (_requiredItemsCache.TryGetValue(tpl, out var offerIds))
|
||||
{
|
||||
return list;
|
||||
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 offer in ragfairOfferService.GetOffers())
|
||||
foreach (var requirement in offer.Requirements)
|
||||
{
|
||||
if (_paymentHelper.IsMoneyTpl(requirement.Template))
|
||||
if (paymentHelper.IsMoneyTpl(requirement.Template))
|
||||
// This would just be too noisy
|
||||
{
|
||||
continue;
|
||||
@@ -38,7 +48,7 @@ public class RagfairRequiredItemsService(
|
||||
_requiredItemsCache.TryAdd(requirement.Template, []);
|
||||
|
||||
// Add matching offer
|
||||
_requiredItemsCache.GetValueOrDefault(requirement.Template)?.Add(offer);
|
||||
_requiredItemsCache.GetValueOrDefault(requirement.Template)?.Add(offer.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,10 @@ public class RagfairOfferHolder(
|
||||
/// <returns>RagfairOffer</returns>
|
||||
public HashSet<string> GetStaleOfferIds()
|
||||
{
|
||||
return _expiredOfferIds;
|
||||
lock (_expiredOfferIdsLock)
|
||||
{
|
||||
return _expiredOfferIds;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,7 +93,7 @@ public class RagfairOfferHolder(
|
||||
/// <returns>RagfairOffer list</returns>
|
||||
public List<RagfairOffer> GetOffers()
|
||||
{
|
||||
if (_offersById.Count > 0)
|
||||
if (!_offersById.IsEmpty)
|
||||
{
|
||||
return _offersById.Values.ToList();
|
||||
}
|
||||
@@ -342,7 +345,7 @@ public class RagfairOfferHolder(
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flag offers with an expiry before the passed in timestamp
|
||||
/// Flag offers with a end date set before the passed in timestamp
|
||||
/// </summary>
|
||||
/// <param name="timestamp">Timestamp at point offer is 'expired'</param>
|
||||
public void FlagExpiredOffersAfterDate(long timestamp)
|
||||
|
||||
Reference in New Issue
Block a user