85 lines
2.2 KiB
C#
85 lines
2.2 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using tysdk;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
public class HeartBeat
|
|
{
|
|
[Inject]
|
|
public IStageService stageService {get; set;}
|
|
|
|
[Inject]
|
|
public IConfig config {get; set;}
|
|
|
|
private IDisposable disposable;
|
|
|
|
private CancellationTokenSource tokenSource;
|
|
|
|
public void Init()
|
|
{
|
|
//UnityEngine.Debug.Log("HeartBeat Init");
|
|
disposable = GContext.OnEvent<StageLoadEvent>()
|
|
.Where(e => e.state == EStageLoadState.Loaded && e.stageId == "FishingStage")
|
|
.Subscribe(OnFishingStageLoad);
|
|
Application.focusChanged += OnFocusChanged;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
disposable.Dispose();
|
|
Application.focusChanged -= OnFocusChanged;
|
|
if(tokenSource != null)
|
|
{
|
|
tokenSource.Cancel();
|
|
tokenSource = null;
|
|
}
|
|
}
|
|
|
|
private void OnFocusChanged(bool focused)
|
|
{
|
|
if(focused)
|
|
{
|
|
if(tokenSource == null && stageService.GetStage("FishingStage") != null)
|
|
{
|
|
tokenSource = new CancellationTokenSource();
|
|
StartHeartBeat(tokenSource.Token);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(tokenSource != null)
|
|
{
|
|
tokenSource.Cancel();
|
|
tokenSource = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnFishingStageLoad(StageLoadEvent @event)
|
|
{
|
|
disposable.Dispose();
|
|
tokenSource = new CancellationTokenSource();
|
|
StartHeartBeat(tokenSource.Token);
|
|
//UnityEngine.Debug.Log("HeartBeat Start");
|
|
}
|
|
|
|
private async void StartHeartBeat(CancellationToken token)
|
|
{
|
|
int interval =(int) config.GetFloat("HeartBeat", 60);
|
|
try{
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
using(var e = GEvent.TackEvent("HeartBeat", gaSend : false)){
|
|
e.AddContent("FPS", game.RootCtx.fps);
|
|
}
|
|
await Task.Delay(interval * 1000);
|
|
//UnityEngine.Debug.Log("HeartBeat");
|
|
}
|
|
}
|
|
catch (System.Exception){}
|
|
}
|
|
}
|