using LinePutScript; using System; using System.Collections.Generic; namespace VPet_Simulator.Windows.Interface { /// /// 统计 /// public class Statistics : IGetOBJ { public Statistics() { } public Statistics(IEnumerable subs) { AddRange(subs); } public void AddRange(IEnumerable subs) { foreach (var sub in subs) { Data.Add(sub.Name, sub.info); } } /// /// 统计变化通知事件 /// /// 发送的统计(this) /// 变动的名称 /// 变动的值 public delegate void StatisticChangedEventHandler(Statistics sender, string name, SetObject value); public event StatisticChangedEventHandler StatisticChanged; /// /// 统计数据字典 /// public SortedDictionary Data = new SortedDictionary(); #region IGetOBJ public DateTime this[gdat subName] { get => GetDateTime((string)subName); set => SetDateTime((string)subName, value); } public FInt64 this[gflt subName] { get => GetFloat((string)subName); set => SetFloat((string)subName, value); } public double this[gdbe subName] { get => GetDouble((string)subName); set => SetDouble((string)subName, value); } public long this[gi64 subName] { get => GetInt64((string)subName); set => SetInt64((string)subName, value); } public int this[gint subName] { get => GetInt((string)subName); set => SetInt((string)subName, value); } public bool this[gbol subName] { get => GetBool((string)subName); set => SetBool((string)subName, value); } public string this[gstr subName] { get => GetString((string)subName); set => SetString((string)subName, value); } public SetObject this[string subName] { get => Find(subName) ?? new SetObject(); set => Set(subName, value); } public SetObject Find(string subName) { if (Data.TryGetValue(subName, out SetObject value)) return value; else return null; } public void Set(string subName, SetObject value) { StatisticChanged?.Invoke(this, subName, value); Data[subName] = value; } /// /// 输出统计数据 /// public List ToSubs() { List subs = new List(); foreach (var item in Data) { subs.Add(new Sub(item.Key, item.Value)); } return subs; } public bool GetBool(string subName) => Find(subName)?.GetBoolean() ?? false; public void SetBool(string subName, bool value) => Set(subName, value); public int GetInt(string subName, int defaultvalue = 0) => Find(subName)?.GetInteger() ?? defaultvalue; public void SetInt(string subName, int value) => Set(subName, value); public long GetInt64(string subName, long defaultvalue = 0) => Find(subName)?.GetInteger64() ?? defaultvalue; public void SetInt64(string subName, long value) => Set(subName, value); public FInt64 GetFloat(string subName, FInt64 defaultvalue = default) => Find(subName)?.GetFloat() ?? defaultvalue; public void SetFloat(string subName, FInt64 value) => Set(subName, new SetObject(value)); public DateTime GetDateTime(string subName, DateTime defaultvalue = default) => Find(subName)?.GetDateTime() ?? defaultvalue; public void SetDateTime(string subName, DateTime value) => Set(subName, value); public string GetString(string subName, string defaultvalue = null) => Find(subName)?.GetString() ?? defaultvalue; public void SetString(string subName, string value) => Set(subName, value); public double GetDouble(string subName, double defaultvalue = 0) => Find(subName)?.GetDouble() ?? defaultvalue; public void SetDouble(string subName, double value) => Set(subName, value); #endregion } }