Files
back_cantanBuilding/Assets/Scripts/UI/Home/HomeBtnAdvert.cs
2026-05-26 16:15:54 +08:00

120 lines
3.8 KiB
C#

using asap.core;
using cfg;
using GameCore;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class HomeBtnAdvert : MonoBehaviour, IUIRedPoint
{
public const string redKey = "Home.Advert";
PlayerShopData shopData;
Button btn_advert;
TMP_Text text_time;
GameObject redpoint;
GameObject text_task;
Timer timer;
private void Awake()
{
shopData = GContext.container.Resolve<PlayerShopData>();
btn_advert = GetComponent<Button>();
redpoint = transform.Find("redpoint").gameObject;
text_time = transform.Find("text_time").GetComponent<TMP_Text>();
text_task = transform.Find("text_task").gameObject;
gameObject.SetActive(shopData.IsOpenAdPack());
}
private void Start()
{
btn_advert.onClick.AddListener(OnClickAdvert);
}
void OnEnable()
{
RedPointManager.Instance.AddRedPoint(redKey, this);
SetRedPointState(RedPointManager.Instance.GetRedPointState(redKey));
}
private async void OnClickAdvert()
{
GameObject go = await UIManager.Instance.ShowUILoad(UITypes.AdvertPanel);
if (go != null)
{
GContext.Publish(new HideHomePanelEvent());
}
}
public void SetRedPointState(bool state)
{
redpoint.SetActive(state);
if (!state && shopData.IsOpenAdPack() && shopData.IsShowVIPAdTimer())
{
StartTimer();
return;
}
if (timer != null)
{
timer.Cancel();
timer = null;
}
SetState(true);
}
void StartTimer()
{
DateTime now = ZZTimeHelper.UtcNow().UtcNowOffset();
var id = GContext.container.Resolve<PlayerData>().priceLevel.DailyGiftPackList[0];
var packM = shopData.TbPackManagerGetOrDefault(id);
var pack = GContext.container.Resolve<Tables>().TbPack.GetOrDefault(packM.PackID[0]);
var daily = packM.TimeDefinition as Daily;
var DailyGiftPackList = GContext.container.Resolve<PlayerData>().priceLevel.DailyGiftPackList;
for (int i = 0; i < DailyGiftPackList.Count; i++)
{
id = DailyGiftPackList[i];
ShopPackManager localPackM = shopData.TbPackManagerGetOrDefault(id);
if (localPackM == null) break;
daily = localPackM.TimeDefinition as Daily;
if (TimeSpan.FromSeconds(daily.StartTime) <= now.TimeOfDay && TimeSpan.FromSeconds(daily.EndTime) > now.TimeOfDay)
{
packM = localPackM;
break;
}
}
if (shopData.GetShopPackBuyCount(packM.ID) < 2)
{
SetState(true);
return;
}
SetState(false);
TimeSpan ts = TimeSpan.FromSeconds(daily.EndTime) - now.TimeOfDay;
text_time.text = ConvertTools.ConvertTime2(0, ts.Hours, ts.Minutes, ts.Seconds);
if (timer == null)
{
timer = this.AttachTimer((float)ts.TotalSeconds, EndTimer,
(elapsed) =>
{
now = ZZTimeHelper.UtcNow().UtcNowOffset();
ts = TimeSpan.FromSeconds(daily.EndTime) - now.TimeOfDay;
text_time.text = ConvertTools.ConvertTime2(0, ts.Hours, ts.Minutes, ts.Seconds);
}, useRealTime: true);
}
}
void EndTimer()
{
SetRedPointState(RedPointManager.Instance.GetRedPointState(redKey));
}
void SetState(bool value)
{
text_time.gameObject.SetActive(!value);
text_task.SetActive(value);
}
private void OnDisable()
{
RedPointManager.Instance.RemoveRedPoint(redKey);
}
private void OnDestroy()
{
if (timer != null)
{
timer.Cancel();
timer = null;
}
}
}