diff --git a/Libraries/Core/Services/InMemoryCacheService.cs b/Libraries/Core/Services/InMemoryCacheService.cs new file mode 100644 index 00000000..2ac9e015 --- /dev/null +++ b/Libraries/Core/Services/InMemoryCacheService.cs @@ -0,0 +1,54 @@ +using Core.Utils.Cloners; +using SptCommon.Annotations; + +namespace Core.Services; + +[Injectable(InjectionType.Singleton)] +public class InMemoryCacheService( + ICloner _cloner +) +{ + protected Dictionary _cacheData = new(); + + // Store data into an in-memory object + // key to store data against + // Data to store in cache + public void StoreByKey(string key, T dataToCache) + { + _cacheData[key] = _cloner.Clone(dataToCache); + } + + // Retrieve data stored by a key + // key + // Stored data + public T? GetDataByKey(string key) + { + if (_cacheData.ContainsKey(key)) + { + return (T) _cacheData[key]; + } + + return default; + } + + // Does data exist against the provided key + // Key to check for data against + // true if exists + public bool HasStoredDataByKey(string key) + { + return _cacheData.ContainsKey(key); + } + + // Remove data stored against key + // Key to remove data against + public void ClearDataStoredByKey(string key) + { + _cacheData.Remove(key); + } + + // Remove all data stored + public void ClearCache() + { + _cacheData.Clear(); + } +} diff --git a/Libraries/Core/Services/ItemFilterService.cs b/Libraries/Core/Services/ItemFilterService.cs new file mode 100644 index 00000000..3a3067be --- /dev/null +++ b/Libraries/Core/Services/ItemFilterService.cs @@ -0,0 +1,161 @@ +using Core.Models.Spt.Config; +using Core.Models.Utils; +using Core.Servers; +using Core.Utils.Cloners; +using SptCommon.Annotations; + +namespace Core.Services; + +[Injectable(InjectionType.Singleton)] +public class ItemFilterService( + ISptLogger _logger, + ICloner _cloner, + ConfigServer _configServer +) +{ + protected HashSet? _itemBlacklistCache = []; + protected ItemConfig _itemConfig = _configServer.GetConfig(); + + protected HashSet? _lootableItemBlacklistCache = []; + + /** + * Check if the provided template id is blacklisted in config/item.json/blacklist + * @param tpl template id + * @returns true if blacklisted + */ + public bool ItemBlacklisted(string tpl) + { + if (_itemBlacklistCache.Count == 0) + { + _itemBlacklistCache.UnionWith(_itemConfig.Blacklist); + } + + return _itemBlacklistCache.Contains(tpl); + } + + /** + * Check if item is blacklisted from being a reward for player + * @param tpl item tpl to check is on blacklist + * @returns True when blacklisted + */ + public bool ItemRewardBlacklisted(string tpl) + { + return _itemConfig.RewardItemBlacklist.Contains(tpl); + } + + /** + * Get an array of items that should never be given as a reward to player + * @returns string array of item tpls + */ + public HashSet GetItemRewardBlacklist() + { + return _cloner.Clone(_itemConfig.RewardItemBlacklist); + } + + /** + * Get an array of item types that should never be given as a reward to player + * @returns string array of item base ids + */ + public HashSet GetItemRewardBaseTypeBlacklist() + { + return _cloner.Clone(_itemConfig.RewardItemTypeBlacklist); + } + + /** + * Return every template id blacklisted in config/item.json + * @returns string array of blacklisted template ids + */ + public HashSet GetBlacklistedItems() + { + return _cloner.Clone(_itemConfig.Blacklist); + } + + /** + * Return every template id blacklisted in config/item.json/lootableItemBlacklist + * @returns string array of blacklisted template ids + */ + public HashSet GetBlacklistedLootableItems() + { + return _cloner.Clone(_itemConfig.LootableItemBlacklist); + } + + /** + * Check if the provided template id is boss item in config/item.json + * @param tpl template id + * @returns true if boss item + */ + public bool BossItem(string tpl) + { + return _itemConfig.BossItems.Contains(tpl); + } + + /** + * Return boss items in config/item.json + * @returns string array of boss item template ids + */ + public HashSet GetBossItems() + { + return _cloner.Clone(_itemConfig.BossItems).ToHashSet(); + } + + /** + * Check if the provided template id is blacklisted in config/item.json/lootableItemBlacklist + * @param tpl template id + * @returns true if blacklisted + */ + public bool IsLootableItemBlacklisted(string itemKey) + { + if (!_lootableItemBlacklistCache.Any()) + { + HydrateLootableItemBlacklist(); + } + + return _lootableItemBlacklistCache.Contains(itemKey); + } + + public bool IsItemBlacklisted(string tpl) + { + if (!_itemBlacklistCache.Any()) + { + HydrateBlacklist(); + } + + return _itemBlacklistCache.Contains(tpl); + } + + protected void HydrateLootableItemBlacklist() + { + foreach (var item in _itemConfig.LootableItemBlacklist) + { + _lootableItemBlacklistCache.Add(item); + } + } + + protected void HydrateBlacklist() + { + foreach (var item in _itemConfig.Blacklist) + { + _itemBlacklistCache.Add(item); + } + } + + /** + * Check if the provided template id is boss item in config/item.json + * @param tpl template id + * @returns true if boss item + */ + public bool IsBossItem(string tpl) + { + return _itemConfig.BossItems.Contains(tpl); + } + + /** + * Check if item is blacklisted from being a reward for player + * @param tpl item tpl to check is on blacklist + * @returns True when blacklisted + */ + public bool IsItemRewardBlacklisted(string tpl) + { + return _itemConfig.RewardItemBlacklist.Contains(tpl); + } +} diff --git a/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs b/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs index 02d5ef4c..f87ee979 100644 --- a/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/LocationLifecycleService.cs @@ -322,6 +322,12 @@ public class LocationLifecycleService } } + /// + /// Generate a maps base location (cloned) and loot + /// + /// Map name + /// OPTIONAL - Should loot be generated for the map before being returned + /// LocationBase /// LocationBase /// OPTIONAL - Should loot be generated for the map before being returned /// Map name @@ -1178,8 +1184,8 @@ public class LocationLifecycleService } /// - /// /// Is the player dead after a raid - dead = anything other than "survived" / "runner" + /// /// Post raid request /// True if dead protected bool IsPlayerDead(EndRaidResult results)