using ET.EventType; using System; using System.Collections.Generic; using System.Text; using static ET.StatisticComponent; namespace ET { public class StatisticComponentAwakeSystem : AwakeSystem { public override void Awake(StatisticComponent self) { StatisticComponent.instance = self; //从数据库拿 self.Awake().Coroutine(); } } public static class StatisticComponentSystem { class StatisticPrintPerDay : AEvent { public override async ETTask Run(UpdatePer1DayOfMonth args) { StatisticComponent.instance.Clear(); await ETTask.CompletedTask; } } class StatisticPrintPer5Minute : AEvent { public override async ETTask Run(UpdatePer5MinuteOfDay args) { await StatisticComponent.instance.Save(); await ETTask.CompletedTask; } } public static async ETVoid Awake(this StatisticComponent self) { DateTime time = DateTime.UtcNow; long Id = time.Year * 10000 + time.Month * 100 + time.Day; StatisticComponent statisticComponent = await DBComponent.Instance.Query(Id); if (statisticComponent != null) { self.gemDic = statisticComponent.gemDic; self.coinDic = statisticComponent.coinDic; self.energyCostDic = statisticComponent.energyCostDic; self.statisticDic = statisticComponent.statisticDic; } } public static void AddEnergyCost(this StatisticComponent self, long id, string costType, int count) { if (!self.energyCostDic.TryGetValue(id, out Dictionary dic)) { self.energyCostDic[id] = dic = new(); } if(!dic.TryGetValue(costType,out int value)) { dic[costType] = value = 0; } dic[costType] = value + count; } public static void AddInfo(this StatisticComponent self, long id, StatisticType name,string sources, long count) { if (name == StatisticType.Coin) self.AddInfo(id,self.coinDic,sources, count/1000); else if (name == StatisticType.Gem) self.AddInfo(id, self.gemDic, sources, count/1000); } private static void AddInfo(this StatisticComponent self, long id,Dictionary> dic, string sources, long add) { if (!dic.TryGetValue(id, out Dictionary sourcesDic)) { dic[id] = sourcesDic = new(); } if (!sourcesDic.TryGetValue(sources, out long count)) { sourcesDic[sources] = 0; } sourcesDic[sources] = count + add; } public static void Clear(this StatisticComponent self) { self.coinDic.Clear(); self.gemDic.Clear(); self.energyCostDic.Clear(); self.statisticDic.Clear(); } public static async ETTask Save(this StatisticComponent self) { foreach ((long id, Dictionary dic) in self.gemDic) { User user = await UserComponent.Instance.Query(id); foreach ((string sources, long count) in dic) { if(sources == StatisticsTypes.GemSources_MainStory) { int energy = 0; if(self.energyCostDic.TryGetValue(id,out Dictionary costDic)){ costDic.TryGetValue(StatisticsTypes.EnergyCostType_MainStory, out energy); } string key = $"{id},{user?.NickName.Replace(".","_")}"; if(!self.statisticDic.TryGetValue(key,out Dictionary statisticDic)) { self.statisticDic[key] = statisticDic = new(); } statisticDic[sources] = $"[消耗:{energy}] [{count}] [{count * 1f / energy}]"; } } } foreach ((long id, Dictionary dic) in self.coinDic) { User user = await UserComponent.Instance.Query(id); foreach ((string sources, long count) in dic) { if (sources == StatisticsTypes.CoinSources_IdleBattle) { int energy = 0; if (self.energyCostDic.TryGetValue(id, out Dictionary costDic)) { costDic.TryGetValue(StatisticsTypes.EnergyCostType_IdleBattle, out energy); } string key = $"{id},{user?.NickName}"; if (!self.statisticDic.TryGetValue(key, out Dictionary statisticDic)) { self.statisticDic[key] = statisticDic = new(); } statisticDic[sources] = $"[消耗:{energy}] [{count}] [{count * 1f / energy}]"; } } } DateTime time = DateTime.UtcNow; //20210323 self.Id = time.Year*10000+time.Month*100+time.Day; await DBComponent.Instance.Save(self); } } }