using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using ServerCore; using ServerBase; namespace ServerBase; public class EntityAggregator where TKey : ITuple where TCounterType : struct { private readonly ConcurrentDictionary m_counters = new(); public EntityAggregator() { } public void reset() { m_counters.Clear(); } public void incCounter(TKey key, TCounterType toIncValue, Func fnUpdate) { var add_counter = delegate (TKey key) { return toIncValue; }; var update_recorder = delegate (TKey key, TCounterType currValue) { return fnUpdate(currValue, toIncValue); }; m_counters.AddOrUpdate(key, add_counter, update_recorder); } public void decCounter(TKey key, TCounterType toDecValue, Func fnUpdate) { var add_counter = delegate (TKey key) { return toDecValue; }; var update_recorder = delegate (TKey key, TCounterType currValue) { return fnUpdate(currValue, toDecValue); }; m_counters.AddOrUpdate(key, add_counter, update_recorder); } public TCounterType getCounter(TKey key, TCounterType defaultValue) { if(false == m_counters.TryGetValue(key, out var found_counter)) { return defaultValue; } return found_counter; } public ConcurrentDictionary getCounters() => m_counters; }