Files
back_cantanBuilding/Assets/Scripts/Services/RTServiceBgFgManager.cs
2026-05-26 16:15:54 +08:00

91 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using asap.core;
using game;
using GameCore;
using UnityEngine;
public class RTServiceBgFgManager : MonoBehaviour
{
public static bool keepAlive = false;
private async void OnApplicationFocus(bool focus)
{
if (focus)
{
var rtService = GContext.container.Resolve<IRTService>();
var userService = GContext.container.Resolve<IUserService>();
var mailService = GContext.container.Resolve<IInGameMailService>();
if(rtService != null && userService != null && userService.IsLogin)
{
if(await NeedReboot())
{
RebootAlert();
}
else {
try
{
await rtService.Connect();
}
catch (Exception ex)
{
Debug.LogException(ex);
}
await mailService.FetchMail();
}
}
}
}
private void OnApplicationPause(bool pause)
{
if (pause && !keepAlive)
{
var rtService = GContext.container.Resolve<IRTService>();
var userService = GContext.container.Resolve<IUserService>();
if(rtService != null && userService != null && userService.IsLogin)
{
rtService.Disconnect();
}
}
}
private async void RebootAlert()
{
GameObject go = await UIManager.Instance.ShowUI(UITypes.NoticeConfirmPopupPanel);
var noticeConfirmPopupPanel = go.GetComponent<NoticeConfirmPopupPanel>();
string title = LocalizationMgr.GetText("UI_NoticePopupPanel_3");
string info = LocalizationMgr.GetText("UI_NoticePopupPanel_4");
string label = LocalizationMgr.GetText("UI_COMMON_confirm");
noticeConfirmPopupPanel.Init(1, title, info, onClickLeft: RootCtx.ExitGame, onClose: RootCtx.ExitGame, btn_left_text: label);
}
private async Task<bool> NeedReboot()
{
var userService = GContext.container.Resolve<IUserService>();
var config = GContext.container.Resolve<IConfig>();
if(userService == null || !userService.IsLogin)
{
return false;
}
if (config == null) return false;
var loginTime = userService.LoginTime;
var duration = ZZTimeHelper.UtcNow() - loginTime;
if(duration.Days >=1)
{
var localVer = VersionTool.FromLocal();
var remoteConfig = await ConfigExtension.GetRemoteConfig();
if(remoteConfig == null) return false;
var remoteVerStr = remoteConfig[GConstant.K_Ver];
var remoteVer = VersionTool.FromString(remoteVerStr);
return remoteVer > localVer || remoteVer.Build != localVer.Build;
}
return false;
}
}