149 lines
4.5 KiB
C#
149 lines
4.5 KiB
C#
using Assets.MyGame.Scripts.Net;
|
|
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
|
|
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UIHome_BY_JJJPanel : SubUIBase
|
|
{
|
|
public Button btn_Receive;
|
|
public Image img_DisReceive;
|
|
public TextMeshProUGUI txt_coinsNum;
|
|
public TextMeshProUGUI txt_TimeNum;
|
|
public TextMeshProUGUI txt_JieShao;
|
|
|
|
[Header("救济金次数项")]
|
|
public Transform tra_itemParent;
|
|
public JJJItem jJJItemPrefab;
|
|
|
|
private ReliefConfig reliefCfg;
|
|
private Coroutine _countdownCoroutine;
|
|
private string _introTemplate; // 前两行固定文案模板
|
|
|
|
public override void Awake()
|
|
{
|
|
base.Awake();
|
|
btn_Receive.onClick.AddListener(() =>
|
|
{
|
|
APIRequestList.GetRelief((callback) =>
|
|
{
|
|
UIManager.Instance.ShowBoardError("救济金领取成功");
|
|
// 先同步用户数据(更新 reliefGoldCount 和 gold),再刷新面板
|
|
APIRequestList.GetUser(user =>
|
|
{
|
|
GameManager.Instance.UpdataUserData(user);
|
|
RefreshPanel();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
protected override void OnShow(object args = null)
|
|
{
|
|
base.OnShow(args);
|
|
RefreshPanel();
|
|
}
|
|
|
|
protected override void OnHide()
|
|
{
|
|
base.OnHide();
|
|
if (_countdownCoroutine != null)
|
|
{
|
|
StopCoroutine(_countdownCoroutine);
|
|
_countdownCoroutine = null;
|
|
}
|
|
}
|
|
|
|
private void RefreshPanel()
|
|
{
|
|
APIRequestList.GetReliefConfig(jjjData =>
|
|
{
|
|
reliefCfg = jjjData;
|
|
var user = GameManager.Instance.user;
|
|
long userGold = user.assets.gold;
|
|
int totalCount = reliefCfg.count;
|
|
int remaining = user.@static.reliefGoldCount;
|
|
int claimed = totalCount - remaining;
|
|
|
|
// 固定前两行模板,第三行由协程实时更新
|
|
_introTemplate = $"1.金币低于{reliefCfg.limit}可以领取{reliefCfg.gold}\r\n2.每天可以领取{reliefCfg.count}次\r\n";
|
|
|
|
txt_coinsNum.text = "金币" + reliefCfg.gold.ToString();
|
|
txt_TimeNum.text = $"剩余次数:{remaining}";
|
|
|
|
bool canReceive = reliefCfg.isOpen
|
|
&& remaining > 0
|
|
&& userGold < reliefCfg.limit;
|
|
|
|
img_DisReceive.gameObject.SetActive(!canReceive);
|
|
btn_Receive.interactable = canReceive;
|
|
|
|
BuildTimesList(totalCount, claimed, canReceive);
|
|
|
|
// 启动倒计时协程
|
|
StartCountdown();
|
|
});
|
|
}
|
|
|
|
private void BuildTimesList(int totalCount, int claimedCount, bool canReceive)
|
|
{
|
|
if (tra_itemParent == null) return;
|
|
for (int i = tra_itemParent.childCount - 1; i >= 0; i--)
|
|
Destroy(tra_itemParent.GetChild(i).gameObject);
|
|
|
|
for (int i = 0; i < totalCount; i++)
|
|
{
|
|
var item = Instantiate(jJJItemPrefab, tra_itemParent);
|
|
item.gameObject.SetActive(true);
|
|
|
|
bool isClaimed = i < claimedCount;
|
|
bool isCanClaim = !isClaimed && i == claimedCount && canReceive;
|
|
bool isLocked = !isClaimed && !isCanClaim;
|
|
|
|
item.Init(isClaimed, isCanClaim, isLocked, reliefCfg.gold);
|
|
}
|
|
}
|
|
|
|
// ==================== 实时倒计时 ====================
|
|
|
|
private void StartCountdown()
|
|
{
|
|
if (_countdownCoroutine != null)
|
|
StopCoroutine(_countdownCoroutine);
|
|
_countdownCoroutine = StartCoroutine(CountdownLoop());
|
|
}
|
|
|
|
private IEnumerator CountdownLoop()
|
|
{
|
|
var wait = new WaitForSeconds(1f);
|
|
|
|
while (true)
|
|
{
|
|
UpdateCountdownText();
|
|
yield return wait;
|
|
}
|
|
}
|
|
|
|
private void UpdateCountdownText()
|
|
{
|
|
if (txt_JieShao == null || string.IsNullOrEmpty(_introTemplate)) return;
|
|
|
|
// 计算到次日 00:00:00 的剩余时间
|
|
System.DateTime now = System.DateTime.Now;
|
|
System.DateTime tomorrow = now.Date.AddDays(1);
|
|
System.TimeSpan remaining = tomorrow - now;
|
|
|
|
// 跨天检测:刷新面板
|
|
if (remaining.TotalSeconds <= 0)
|
|
{
|
|
txt_JieShao.text = _introTemplate + "3.剩余00:00:00刷新领取次数";
|
|
RefreshPanel();
|
|
return;
|
|
}
|
|
|
|
string countdown = $"{remaining.Hours:D2}:{remaining.Minutes:D2}:{remaining.Seconds:D2}";
|
|
txt_JieShao.text = _introTemplate + $"3.剩余{countdown}刷新领取次数";
|
|
}
|
|
}
|