diff --git a/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs b/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs new file mode 100644 index 00000000..5c639e5a --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs @@ -0,0 +1,37 @@ +namespace SPTarkov.Server.Core.Extensions +{ + public static class DictionaryExtensions + { + /// + /// Add a value by key to a dictionary, if the key doesn't exist, create it + /// + /// Dictionary key type + /// Dictionary to add/update + /// Key to update by + /// Value to add to key + public static void AddOrUpdate(this IDictionary dict, T key, double value) + where T : notnull + { + if (!dict.TryAdd(key, value)) + { + dict[key] += value; + } + } + + /// + /// Add a value by key to a dictionary, if the key doesn't exist, create it + /// + /// Dictionary key type + /// Dictionary to add/update + /// Key to update by + /// Value to add to key + public static void AddOrUpdate(this IDictionary dict, T key, int value) + where T : notnull + { + if (!dict.TryAdd(key, value)) + { + dict[key] += value; + } + } + } +}