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

174 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using asap.core;
using cfg;
using game;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PackItem : PanelItemBase<PackData>
{
public Image banner;
public RewardItemNew[] rewardItems;
public TMP_Text text_limit;
//倒计时
public TMP_Text text_time;
//购买按钮
public Button btn_buy;
public TMP_Text text_buy;
TMP_Text txt_name;
GameObject go_soldOut;
Timer timer;
IAPItemList iAPItemList;
List<ItemData> itemDataGift;
Daily daily;
private void Awake()
{
banner = transform.Find("reward_panel/bg").GetComponent<Image>();
rewardItems = transform.Find("reward_panel").GetComponentsInChildren<RewardItemNew>();
txt_name = transform.Find("reward_panel/text_title").GetComponent<TMP_Text>();
text_limit = transform.Find("reward_panel/text_left_time").GetComponent<TMP_Text>();
text_time = transform.Find("reward_panel/text_time").GetComponent<TMP_Text>();
btn_buy = transform.Find("reward_panel/btn_purchase/btn_green").GetComponent<Button>();
text_buy = transform.Find("reward_panel/btn_purchase/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
go_soldOut = transform.Find("reward_panel/soldout").gameObject;
}
private void Start()
{
btn_buy.onClick.AddListener(OnClickBuy);
}
public override void OnInit()
{
SetTimer();
SetRewardItems();
SetBuy();
}
void SetRewardItems()
{
itemDataGift = GContext.container.Resolve<PlayerItemData>().GetItemDataByDropIdAndType(data.pack.DropID);
for (int i = 0; i < rewardItems.Length; i++)
{
if (i < itemDataGift.Count)
{
rewardItems[i].SetData(itemDataGift[i], isCanClick: true);
}
else
{
rewardItems[i].gameObject.SetActive(false);
}
}
}
void SetBuy()
{
if (data != null)
{
txt_name.text = LocalizationMgr.GetText(data.pack.Title_l10n_key);
btn_buy.enabled = true;
int remainder = data.packM.MaxPurchaseNew - data.num;
if (remainder <= 0)
{
remainder = 0;
}
text_limit.text = LocalizationMgr.GetFormatTextValue("UI_FishingShopPanel_1", remainder, data.packM.MaxPurchaseNew);
iAPItemList = GContext.container.Resolve<Tables>().TbIAPItemList.GetOrDefault(data.pack.IAPID);
if (data.num >= data.packM.MaxPurchaseNew)
{
btn_buy.transform.parent.gameObject.SetActive(false);
go_soldOut.SetActive(true);
return;
}
else
{
btn_buy.transform.parent.gameObject.SetActive(true);
go_soldOut.SetActive(false);
}
SKUDetailDataEvent sKUDetailDataEvent = new SKUDetailDataEvent(iAPItemList);
GContext.Publish(sKUDetailDataEvent);
text_buy.text = sKUDetailDataEvent.price;
}
}
async void OnClickBuy()
{
if (data == null)
{
return;
}
btn_buy.enabled = false;
ShopBuyTypeData shopBuyTypeData = new ShopBuyTypeData();
shopBuyTypeData.type = ShopBuyType.ShopPackDailyBuy;
shopBuyTypeData.key = data.packM.ID.ToString();
shopBuyTypeData.endDay = ZZTimeHelper.UtcNow().UtcNowOffset().Day;
bool Result = await GContext.container.Resolve<PlayerShopData>().OnBuy(data.pack.DropID, shopBuyTypeData, iAPItemList, itemDataGift);
if (Result)
{
OnBuySuccess();
}
btn_buy.enabled = true;
}
void OnBuySuccess()
{
data.num += 1;
//GContext.container.Resolve<PlayerShopData>().AddShopPackDailyBuyCount(data.packM.ID, 1);
}
void SetTimer()
{
DateTime now = ZZTimeHelper.UtcNow().UtcNowOffset();
daily = data.packM.TimeDefinition as Daily;
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, ReStart,
(elapsed) =>
{
now = ZZTimeHelper.UtcNow().UtcNowOffset();
ts = TimeSpan.FromSeconds(daily.EndTime) - now.TimeOfDay;
text_time.text = ConvertTools.ConvertTime2(0, ts.Hours, ts.Minutes, ts.Seconds);
if ((float)ts.TotalSeconds < 0.0001f)
{
ReStart();
}
}, useRealTime: true);
}
}
void ReStart()
{
timer.Cancel();
timer = null;
GContext.container.Resolve<PlayerShopData>().InitShopPackData();
data.num = 0;
SetBuy();
SetTimer();
}
public override void OffsetX(float _offsetX, float width)
{
if (Mathf.Abs(_offsetX) < width / 2)
{
//即将选中
data.OnCenter(data);
}
}
}
public class PackData
{
public ShopPackManager packM;
public Pack pack;
public int num;
public GameObject select;
public Action<PackData> OnCenter;
}