You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.4 KiB
149 lines
4.4 KiB
3 years ago
|
using System;
|
||
|
using MongoDB.Driver.Core.Events;
|
||
|
|
||
|
namespace ET
|
||
|
{
|
||
|
public class UnitDBSaveComponentAwakeSystem : AwakeSystem<UnitDBSaveComponent>
|
||
|
{
|
||
|
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<UnitDBSaveComponent>
|
||
|
{
|
||
|
public override void Destroy(UnitDBSaveComponent self)
|
||
|
{
|
||
|
TimerComponent.Instance.Remove(ref self.Timer);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class UnitAddComponentSystem : AddComponentSystem<Unit>
|
||
|
{
|
||
|
public override void AddComponent(Unit self, Entity component)
|
||
|
{
|
||
|
Type type = component.GetType();
|
||
|
if (!(typeof (IUnitCache)).IsAssignableFrom(type))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
self.GetComponent<UnitDBSaveComponent>()?.AddChange(type);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class UnitGetComponentSystem : GetComponentSystem<Unit>
|
||
|
{
|
||
|
public override void GetComponent(Unit self, Entity component)
|
||
|
{
|
||
|
Type type = component.GetType();
|
||
|
if (!(typeof (IUnitCache)).IsAssignableFrom(type))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
self.GetComponent<UnitDBSaveComponent>()?.AddChange(type);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Timer(TimerType.SaveChangeDBData)]
|
||
|
public class UnitDBSaveComponentTimer: ATimer<UnitDBSaveComponent>
|
||
|
{
|
||
|
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<UnitDBSaveComponent>
|
||
|
{
|
||
|
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<Unit>();
|
||
|
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>((Unit)self.Parent, "Unit").Coroutine();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|