Files
ft/Client/Assets/Scripts/EventRecharge/HomeBtnRecharge.cs
2026-06-29 21:18:33 +08:00

196 lines
6.0 KiB
C#

using asap.core;
using cfg;
using game;
using GameCore;
using System;
using System.Collections.Generic;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class HomeBtnRecharge : EventButtonResource
{
string EventRechargeBenefitsPanel = "EventRechargeBenefitsPanel";
private TMP_Text textTimer;
Button btn_potion;
Image icon;
string iconUrl;
private DateTime endDateTime;
Tables tables;
PlayerShopData playerShopData;
EventTotalRecharge eventTotalRecharge;
protected CompositeDisposable disposables = new CompositeDisposable();
public GameObject redPoint;
public GameObject redpoint_big;
public TMP_Text text_redpoint_big;
TotalRechargeManager totalRechargeManager;
TotalRechargeData totalRechargeData;
void Awake()
{
totalRechargeManager = new TotalRechargeManager();
textTimer = transform.Find("text_time").GetComponent<TMP_Text>();
btn_potion = GetComponent<Button>();
icon = transform.Find("icon").GetComponent<Image>();
}
// Start is called before the first frame update
void Start()
{
if (!totalRechargeManager.CheckTotalRechargeOpen())
{
gameObject.SetActive(false);
return;
}
totalRechargeData = totalRechargeManager.GetEventData();
int fishingEventID = totalRechargeData.eventID;
GContext.container.Resolve<IFaceUIService>().AddGiftFaceUI(fishingEventID, new UIType(EventRechargeBenefitsPanel));
tables = GContext.container.Resolve<Tables>();
FishingEvent fishingEvent = tables.TbFishingEvent[fishingEventID];
endDateTime = DateTime.Parse((fishingEvent.TimeDefinition as LimitedTime).EndTime);
eventTotalRecharge = tables.TbEventTotalRecharge[fishingEvent.RedirectID];
iconUrl = eventTotalRecharge.Icon;
TimeSpan all = GetRemainingTime(endDateTime);
double seconds = all.TotalSeconds - 1;
if (seconds < 1)
{
gameObject.SetActive(false);
return;
}
textTimer.text = ConvertTools.ConvertTime2(all);
Observable.Interval(TimeSpan.FromSeconds(1.0f)).Subscribe(_ =>
{
TimeSpan tmpAll = GetRemainingTime(endDateTime);
textTimer.text = ConvertTools.ConvertTime2(tmpAll);
if (tmpAll.TotalSeconds <= 0)
Dispose();
}).AddTo(disposables);
GContext.OnEvent<RewardPanelClose>().Subscribe(RewardPanelClose).AddTo(disposables);
GContext.OnEvent<ConditionTypeEvent>().Where(e => e.type == ConditionType.RechargeAmount)
.Subscribe(ConditionTypeEvent).AddTo(disposables);
btn_potion.onClick.AddListener(OnEnter);
List<string> resList = new List<string>
{
EventRechargeBenefitsPanel,
iconUrl
};
GetRed();
CheckResource(resList);
}
void ConditionTypeEvent(ConditionTypeEvent e)
{
UpdateData();
}
void RewardPanelClose(RewardPanelClose rewardPanelClose)
{
UpdateData();
}
private void UpdateData()
{
if (!totalRechargeManager.CheckTotalRechargeOpen())
{
Dispose();
return;
}
totalRechargeData = totalRechargeManager.GetEventData();
GetRed();
}
void GetRed()
{
if (eventTotalRecharge != null)
{
int count = 0;
var taskIds = eventTotalRecharge.TaskList;
var vip = totalRechargeData.vip;
int oldProgress = totalRechargeData.oldProgress;
int newProgress = totalRechargeData.progress;
int taskCount = taskIds.Count;
for (int i = 0; i < taskCount; i++)
{
int taskId = taskIds[i];
CommonTask commonTask = tables.TbCommonTask.GetOrDefault(taskId);
if (commonTask == null)
{
continue;
}
int iapid = commonTask.TaskParam[vip];
var iap = tables.TbIAPItemList.GetOrDefault(iapid);
if (oldProgress < iap.VIPPoint && newProgress >= iap.VIPPoint)
{
count++;
}
}
SetRedPointState(count);
}
}
void SetRedPointState(int count)
{
bool state = count > 0;
if (redpoint_big != null && text_redpoint_big != null)
{
if (!state)
{
redpoint_big.SetActive(false);
}
else
{
if (count > 1)
{
redpoint_big.SetActive(true);
if (count > 99)
{
text_redpoint_big.text = "99+";
}
else
{
text_redpoint_big.text = count.ToString();
}
state = false;
}
else
{
redpoint_big.SetActive(false);
}
}
}
if (redPoint != null)
{
redPoint.SetActive(state);
}
}
void OnEnter()
{
UIManager.Instance.ShowUI(new UIType(EventRechargeBenefitsPanel));
}
private TimeSpan GetRemainingTime(DateTime endDateTime)
{
return endDateTime - ZZTimeHelper.UtcNow();
}
void Dispose()
{
disposables?.Dispose();
disposables = null;
gameObject.SetActive(false);
}
protected override void OnLoadEventResource()
{
TimeSpan all = GetRemainingTime(endDateTime);
double seconds = all.TotalSeconds - 1;
if (seconds > 1)
{
GContext.container.Resolve<IUIService>().SetImageSprite(icon, iconUrl);
gameObject.SetActive(true);
}
}
protected override void OnDestroy()
{
disposables?.Dispose();
disposables = null;
base.OnDestroy();
}
}