From be87e78ac04967fea2a004513576f5a4edaf8eb8 Mon Sep 17 00:00:00 2001 From: Chomp Date: Wed, 18 Jun 2025 15:02:21 +0100 Subject: [PATCH] Cleaned up caching used inside `HandbookHelper` Removed INT cast when returning value from `FromRUB` Renamed `HydrateLookup` and made protected Made `LookupCollection` protected and moved inside `HandbookHelper --- .../Helpers/HandbookHelper.cs | 138 +++++++++--------- 1 file changed, 73 insertions(+), 65 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/HandbookHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/HandbookHelper.cs index fbdd354c..2139aa56 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/HandbookHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/HandbookHelper.cs @@ -15,15 +15,20 @@ public class HandbookHelper( ICloner _cloner ) { - protected LookupCollection _handbookPriceCache = new(); - protected ItemConfig _itemConfig = _configServer.GetConfig(); - protected bool _lookupCacheGenerated; + private LookupCollection? _handbookPriceCache; + protected virtual LookupCollection HandbookPriceCache + { + get { return _handbookPriceCache ??= HydrateHandbookCache(); } + } + + protected readonly ItemConfig _itemConfig = _configServer.GetConfig(); /// /// Create an in-memory cache of all items with associated handbook price in handbookPriceCache class /// - public void HydrateLookup() + protected LookupCollection HydrateHandbookCache() { + var result = new LookupCollection(); var handbook = _databaseService.GetHandbook(); // Add handbook overrides found in items.json config into db foreach (var (key, priceOverride) in _itemConfig.HandbookPriceOverride) @@ -52,30 +57,32 @@ public class HandbookHelper( var handbookDbClone = _cloner.Clone(handbook); foreach (var handbookItem in handbookDbClone.Items) { - _handbookPriceCache.Items.ById.TryAdd(handbookItem.Id, handbookItem.Price ?? 0); - if (!_handbookPriceCache.Items.ByParent.TryGetValue(handbookItem.ParentId, out _)) + result.Items.ById.TryAdd(handbookItem.Id, handbookItem.Price ?? 0); + if (!result.Items.ByParent.TryGetValue(handbookItem.ParentId, out _)) { - _handbookPriceCache.Items.ByParent.TryAdd(handbookItem.ParentId, []); + result.Items.ByParent.TryAdd(handbookItem.ParentId, []); } - _handbookPriceCache.Items.ByParent.TryGetValue(handbookItem.ParentId, out var array); - array.Add(handbookItem.Id); + result.Items.ByParent.TryGetValue(handbookItem.ParentId, out var itemIds); + itemIds.Add(handbookItem.Id); } foreach (var handbookCategory in handbookDbClone.Categories) { - _handbookPriceCache.Categories.ById.TryAdd(handbookCategory.Id, handbookCategory.ParentId); + result.Categories.ById.TryAdd(handbookCategory.Id, handbookCategory.ParentId); if (handbookCategory.ParentId is not null) { - if (!_handbookPriceCache.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out _)) + if (!result.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out _)) { - _handbookPriceCache.Categories.ByParent.TryAdd(handbookCategory.ParentId, []); + result.Categories.ByParent.TryAdd(handbookCategory.ParentId, []); } - _handbookPriceCache.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out var array); - array.Add(handbookCategory.Id); + result.Categories.ByParent.TryGetValue(handbookCategory.ParentId, out var itemIds); + itemIds.Add(handbookCategory.Id); } } + + return result; } /// @@ -86,15 +93,9 @@ public class HandbookHelper( /// price in roubles public double GetTemplatePrice(string tpl) { - if (!_lookupCacheGenerated) + if (HandbookPriceCache.Items.ById.TryGetValue(tpl, out var itemPrice)) { - HydrateLookup(); - _lookupCacheGenerated = true; - } - - if (_handbookPriceCache.Items.ById.TryGetValue(tpl, out var item)) - { - return item; + return itemPrice; } var handbookItem = _databaseService.GetHandbook().Items?.FirstOrDefault(item => item.Id == tpl); @@ -102,22 +103,29 @@ public class HandbookHelper( { const int newValue = 0; - if (!_handbookPriceCache.Items.ById.TryAdd(tpl, newValue)) + if (!HandbookPriceCache.Items.ById.TryAdd(tpl, newValue)) { - _handbookPriceCache.Items.ById[tpl] = newValue; + // Overwrite + HandbookPriceCache.Items.ById[tpl] = newValue; } return newValue; } - if (!_handbookPriceCache.Items.ById.TryAdd(tpl, handbookItem.Price ?? 0)) + if (!HandbookPriceCache.Items.ById.TryAdd(tpl, handbookItem.Price ?? 0)) { - _handbookPriceCache.Items.ById[tpl] = handbookItem.Price ?? 0; + // Overwrite + HandbookPriceCache.Items.ById[tpl] = handbookItem.Price ?? 0; } return handbookItem.Price.Value; } + /// + /// Sum price of supplied items with handbook prices + /// + /// Items to Sum + /// public double GetTemplatePriceForItems(List items) { var total = 0D; @@ -136,7 +144,7 @@ public class HandbookHelper( /// string array public List TemplatesWithParent(string parentId) { - _handbookPriceCache.Items.ByParent.TryGetValue(parentId, out var template); + HandbookPriceCache.Items.ByParent.TryGetValue(parentId, out var template); return template ?? []; } @@ -148,7 +156,7 @@ public class HandbookHelper( /// true if exists in cache public bool IsCategory(string category) { - return _handbookPriceCache.Categories.ById.TryGetValue(category, out _); + return HandbookPriceCache.Categories.ById.TryGetValue(category, out _); } /// @@ -158,7 +166,7 @@ public class HandbookHelper( /// string array public List ChildrenCategories(string categoryParent) { - _handbookPriceCache.Categories.ByParent.TryGetValue(categoryParent, out var category); + HandbookPriceCache.Categories.ByParent.TryGetValue(categoryParent, out var category); return category ?? []; } @@ -185,7 +193,7 @@ public class HandbookHelper( { if (currencyTypeTo == Money.ROUBLES) { - return (int) roubleCurrencyCount; + return roubleCurrencyCount; } // Get price of currency from handbook @@ -199,46 +207,46 @@ public class HandbookHelper( { return _databaseService.GetHandbook().Categories.FirstOrDefault(category => category.Id == handbookId); } -} -public class LookupItem -{ - public LookupItem() + protected record LookupItem { - ById = new Dictionary(); - ByParent = new Dictionary>(); + public LookupItem() + { + ById = new Dictionary(); + ByParent = new Dictionary>(); + } + + public Dictionary ById + { + get; + set; + } + + public Dictionary> ByParent + { + get; + set; + } } - public Dictionary ById + protected record LookupCollection { - get; - set; - } + public LookupCollection() + { + Items = new LookupItem(); + Categories = new LookupItem(); + } - public Dictionary> ByParent - { - get; - set; - } -} - -public class LookupCollection -{ - public LookupCollection() - { - Items = new LookupItem(); - Categories = new LookupItem(); - } - - public LookupItem Items - { - get; - set; - } - - public LookupItem Categories - { - get; - set; + public LookupItem Items + { + get; + set; + } + + public LookupItem Categories + { + get; + set; + } } }