Eintoo/GameServer/Server/Hotfix/Outter/Entity/EntityTimeOutComponentSystem.cs
2025-04-22 15:31:25 +08:00

77 lines
2.1 KiB
C#

using System.Runtime.CompilerServices;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Helper;
namespace Hotfix;
public static class EntityTimeOutComponentSystem
{
public static void SetInterval(this EntityTimeOutComponent self, int interval)
{
self.Interval = interval;
//self.NextTime = TimeHelper.Now + interval;
}
public static bool CheckInterval(this EntityTimeOutComponent self)
{
if (TimeHelper.Now < self.NextTime)
{
Log.Warning("请求过于频繁" + self.NextTime +"TimeOut" + TimeHelper.Now);
return false;
}
self.NextTime = TimeHelper.Now + self.Interval;
return true;
}
public static void TimeOut(this EntityTimeOutComponent self, long time,Func<FTask>? callback = null)
{
var scene = self.Scene;
var parentRunTimeId = self.Parent.RuntimeId;
self.TimeId = scene.TimerComponent.Net.OnceTimer(time, () =>
{
self.Handler(parentRunTimeId,callback).Coroutine();
});
}
public static async FTask Handler(this EntityTimeOutComponent self,long parentRunTimeId , Func<FTask>? callback)
{
var selfParent = self.Parent;
if (self.Parent == null || parentRunTimeId != self.Parent.RuntimeId )
{
return;
}
if (callback != null)
{
await callback();
}
self.TimeId = 0;
selfParent.Dispose();
Log.Debug($"session : {selfParent.RuntimeId} Dispose");
}
}
public class SessionTimeOutComponentAwakeSystem : AwakeSystem<EntityTimeOutComponent>
{
protected override void Awake(EntityTimeOutComponent self)
{
}
}
public class SessionTimeOutComponentDestroySystem : DestroySystem<EntityTimeOutComponent>
{
protected override void Destroy(EntityTimeOutComponent self)
{
if (self.TimeId != 0)
{
self.Scene.TimerComponent.Net.Remove(self.TimeId);
}
self.NextTime = 0;
self.Interval = 0;
}
}