diff --git a/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs b/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs index 4124d570..998d9824 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/RagfairOfferHolder.cs @@ -18,13 +18,28 @@ public class RagfairOfferHolder( ItemHelper _itemHelper ) { - protected readonly Lock _expiredOfferIdsLock = new(); - protected readonly Lock _ragfairOperationLock = new(); + /// + /// Expired offer Ids + /// + private readonly HashSet _expiredOfferIds = []; - protected readonly HashSet _expiredOfferIds = []; - protected readonly ConcurrentDictionary _offersById = new(); - protected readonly ConcurrentDictionary> _offersByTemplate = new(); // key = tplId, value = list of offerIds - protected readonly ConcurrentDictionary> _offersByTrader = new(); // key = traderId, value = list of offerIds + /// + /// Ragfair offer cache, keyed by offer Id + /// + private readonly ConcurrentDictionary _offersById = new(); + + /// + /// Offer Ids keyed by tpl + /// + private readonly ConcurrentDictionary> _offersByTemplate = new(); + + /// + /// Offer ids keyed by trader Id + /// + private readonly ConcurrentDictionary> _offersByTrader = new(); + + private readonly Lock _expiredOfferIdsLock = new(); + private readonly Lock _ragfairOperationLock = new(); /// /// Get a ragfair offer by its id @@ -228,19 +243,26 @@ public class RagfairOfferHolder( /// /// Tpl to store offer against /// Offer to store against tpl - protected void AddOfferByTemplates(string template, string offerId) + /// True - offer was added + protected bool AddOfferByTemplates(string template, string offerId) { + // Look for hashset for tpl first if (_offersByTemplate.TryGetValue(template, out var offerIds)) { offerIds.Add(offerId); - return; + return true; } - if (!_offersByTemplate.TryAdd(template, [offerId])) + // Add new KvP of tpl and offer id in new hashset + if (_offersByTemplate.TryAdd(template, [offerId])) { - _logger.Warning($"Unable to add offer: {offerId} to offersByTemplate"); + return true; } + + _logger.Warning($"Unable to add offer: {offerId} to _offersByTemplate"); + + return false; } /// @@ -248,19 +270,27 @@ public class RagfairOfferHolder( /// /// Trader id to store offer against /// Offer to store against - protected void AddOfferByTrader(string trader, string offerId) + /// True - offer was added + protected bool AddOfferByTrader(string trader, string offerId) { + // Look for hashset for trader first if (_offersByTrader.TryGetValue(trader, out var traderOfferIds)) { traderOfferIds.Add(offerId); - return; + return true; } - if (!_offersByTrader.TryAdd(trader, [offerId])) + // Add new KvP of trader and offer id in new hashset + if (_offersByTrader.TryAdd(trader, [offerId])) { - _logger.Error($"Unable to add offer: {offerId} to offersByTrader"); + return true; } + + _logger.Error($"Unable to add offer: {offerId} to _offersByTrader"); + + return false; + } /// @@ -320,7 +350,7 @@ public class RagfairOfferHolder( continue; } - if (offer?.Items?.Count == 0) + if (offer.Items?.Count == 0) { _logger.Error($"Unable to process expired offer: {expiredOfferId}, it has no items"); continue; @@ -364,7 +394,7 @@ public class RagfairOfferHolder( { if (!_expiredOfferIds.Add(offer.Id)) { - _logger.Warning($"Unable to add offer: {offer.Id} to expired offers"); + _logger.Warning($"Unable to add offer: {offer.Id} to expired offers as it already exists"); } } }