using System; using MongoDB.Driver.Core.Events; namespace ET { public class UnitDBSaveComponentAwakeSystem : AwakeSystem { public override void Awake(UnitDBSaveComponent self) { //self.Timer = TimerComponent.Instance.NewRepeatedTimer(1000, TimerType.SaveChangeDBData, self); int dt = RandomHelper.RandomNumber(10000, 60000); self.SaveUnitTimer = TimerComponent.Instance.NewRepeatedTimer(dt, TimerType.SaveUnitTimer, self); } } public class UnitDBSaveComponentDestroySystem : DestroySystem { public override void Destroy(UnitDBSaveComponent self) { TimerComponent.Instance.Remove(ref self.Timer); } } public class UnitAddComponentSystem : AddComponentSystem { public override void AddComponent(Unit self, Entity component) { Type type = component.GetType(); if (!(typeof (IUnitCache)).IsAssignableFrom(type)) { return; } self.GetComponent()?.AddChange(type); } } public class UnitGetComponentSystem : GetComponentSystem { public override void GetComponent(Unit self, Entity component) { Type type = component.GetType(); if (!(typeof (IUnitCache)).IsAssignableFrom(type)) { return; } self.GetComponent()?.AddChange(type); } } [Timer(TimerType.SaveChangeDBData)] public class UnitDBSaveComponentTimer: ATimer { public override void Run(UnitDBSaveComponent self) { try { if (self.IsDisposed || self.Parent == null) { return; } if (self.DomainScene() == null) { return; } self.SaveChange(); } catch (Exception e) { Log.Error(e.ToString()); } } } [Timer(TimerType.SaveUnitTimer)] public class UnitDBSaveUnitTimer: ATimer { public override void Run(UnitDBSaveComponent self) { try { if (self.IsDisposed || self.Parent == null) { return; } if (self.DomainScene() == null) { return; } self.Save(); } catch (Exception e) { Log.Error(e.ToString()); } } } public static class UnitDBSaveComponentSystem { public static void AddChange(this UnitDBSaveComponent self, Type t) { self.EntityChangeTypeSet.Add(t); } public static void SaveChange(this UnitDBSaveComponent self) { if (self.EntityChangeTypeSet.Count <= 0) { return; } Unit unit = self.GetParent(); Other2UnitCache_AddOrUpdateUnit message = new Other2UnitCache_AddOrUpdateUnit() { UnitId = unit.Id }; message.EntityTypes.Add(unit.GetType().FullName); message.EntityBytes.Add(MongoHelper.ToBson(unit)); foreach (var type in self.EntityChangeTypeSet) { Entity entity = unit.GetComponent(type); if (entity == null || entity.IsDisposed) { continue; } Log.Debug("开始保存变化部分的Entity数据:" + type.FullName); message.EntityTypes.Add(type.FullName); message.EntityBytes.Add(MongoHelper.ToBson(entity)); //处理建筑的属性 } self.EntityChangeTypeSet.Clear(); MessageHelper.CallActor(StartSceneConfigCategory.Instance.GetUnitCacheConfig(unit.Id).InstanceId, message).Coroutine(); } public static void Save(this UnitDBSaveComponent self) { DBManagerComponent.Instance.GetZoneDB(self.DomainZone()).Save((Unit)self.Parent, "Unit").Coroutine(); } } }