186 lines
6.0 KiB
C#
186 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using asap.core;
|
|
using cfg;
|
|
using GameCore;
|
|
using EventBoostPack.Data;
|
|
using TMPro;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace EventBoostPack.Panel
|
|
{
|
|
public class EventBoostPackPanel : BasePanel
|
|
{
|
|
private Button _btnClose;
|
|
private Button _btnBuy;
|
|
private TMP_Text _textPrice;
|
|
private TMP_Text _textRemaining;
|
|
private TMP_Text _textTime;
|
|
private GameObject _tagMore;
|
|
private TMP_Text _textBest;
|
|
private GiftRewardAttached _rewardAttached;
|
|
|
|
private EventBoostPackManager _manager;
|
|
private EventBoostPackManager Manager => _manager ??= Activity.ActivityResolver.Resolve<EventBoostPackManager>();
|
|
private EventBoostPackData Data => Manager?.LoadData();
|
|
|
|
private Pack _pack;
|
|
private IAPItemList _iapItem;
|
|
private List<ItemData> _itemDatas;
|
|
|
|
private void Awake()
|
|
{
|
|
_btnClose = transform.Find("root/btn_close").GetComponent<Button>();
|
|
_btnBuy = transform.Find("root/btn_buy/btn_purchase").GetComponent<Button>();
|
|
_textPrice = transform.Find("root/btn_buy/btn_purchase/Ani_Container/p_text").GetComponent<TMP_Text>();
|
|
_textTime = transform.Find("root/text_time").GetComponent<TMP_Text>();
|
|
_rewardAttached = transform.Find("root/reward_4").GetComponent<GiftRewardAttached>();
|
|
_tagMore = transform.Find("root/tag_more")?.gameObject;
|
|
_textBest = transform.Find("root/tag_more/text_best")?.GetComponent<TMP_Text>();
|
|
_textRemaining = transform.Find("root/btn_buy/btn_purchase/Ani_Container/text_remaining").GetComponent<TMP_Text>();
|
|
_btnClose?.onClick.AddListener(OnClickClose);
|
|
_btnBuy?.onClick.AddListener(OnClickBuy);
|
|
}
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
InitData();
|
|
GContext.container.Resolve<TimeManager.ITimeTickService>()?.SecondTick?
|
|
.Subscribe(_ => UpdateTimer()).AddTo(disposables);
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
_btnClose?.onClick.RemoveAllListeners();
|
|
_btnBuy?.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
private void InitData()
|
|
{
|
|
var data = Data;
|
|
if (data == null || !data.IsActive)
|
|
{
|
|
OnClickClose();
|
|
return;
|
|
}
|
|
|
|
_pack = Manager.GetCurrentPack(data);
|
|
if (_pack == null)
|
|
{
|
|
Debug.LogError("[EventBoostPack] Pack config not found");
|
|
OnClickClose();
|
|
return;
|
|
}
|
|
|
|
var tables = GContext.container.Resolve<Tables>();
|
|
_iapItem = tables.TbIAPItemList.GetOrDefault(_pack.IAPID);
|
|
|
|
var playerItemData = GContext.container.Resolve<PlayerItemData>();
|
|
_itemDatas = playerItemData.GetItemDataByDropIdAndType(_pack.DropID);
|
|
|
|
SetRewardItems();
|
|
SetPrice();
|
|
SetRebate();
|
|
RefreshBuyState();
|
|
UpdateTimer();
|
|
}
|
|
|
|
private void SetRewardItems()
|
|
{
|
|
if (_itemDatas == null || _itemDatas.Count == 0) return;
|
|
_rewardAttached.gameObject.SetActive(true);
|
|
_rewardAttached.SetData(_itemDatas);
|
|
}
|
|
|
|
private void SetPrice()
|
|
{
|
|
var sku = new SKUDetailDataEvent(_iapItem);
|
|
GContext.Publish(sku);
|
|
if (_textPrice != null)
|
|
_textPrice.text = sku.price;
|
|
}
|
|
|
|
private void SetRebate()
|
|
{
|
|
if (_tagMore != null)
|
|
_tagMore.SetActive(_pack.Rebate > 0.01);
|
|
if (_textBest != null && _pack.Rebate > 0.01)
|
|
_textBest.text = LocalizationMgr.GetFormatTextValue(
|
|
"UI_FishingShopPanel_2", (_pack.Rebate * 100).ToPercentageString());
|
|
}
|
|
|
|
private void RefreshBuyState()
|
|
{
|
|
var data = Data;
|
|
if (Manager == null || data == null) return;
|
|
int remaining = data.GetRemainingBuyCount();
|
|
int max = 2;
|
|
if (_textRemaining != null)
|
|
_textRemaining.text = LocalizationMgr.GetFormatTextValue(
|
|
"UI_FishingShopPanel_1", remaining, max);
|
|
if (_btnBuy != null)
|
|
_btnBuy.interactable = remaining > 0;
|
|
}
|
|
|
|
private void UpdateTimer()
|
|
{
|
|
var data = Data;
|
|
if (data == null || _textTime == null) return;
|
|
var remaining = data.RemainingTime;
|
|
_textTime.text = ConvertTools.ConvertTime2(remaining);
|
|
}
|
|
|
|
|
|
private async void OnClickBuy()
|
|
{
|
|
var data = Data;
|
|
if (Manager == null || data == null || _pack == null) return;
|
|
if (data.GetRemainingBuyCount() <= 0) return;
|
|
|
|
_btnBuy.enabled = false;
|
|
try
|
|
{
|
|
if (_iapItem == null) return;
|
|
var buyType = new ShopBuyTypeData
|
|
{
|
|
type = ShopBuyType.ShopPackDailyBuy,
|
|
ID = data.EventID,
|
|
key = data.GetDailyBuyCountKey(),
|
|
endDay = ZZTimeHelper.UtcNow().UtcNowOffset().Day
|
|
};
|
|
bool result = await GContext.container.Resolve<PlayerShopData>()
|
|
.OnBuy(_pack.DropID, buyType, _iapItem, _itemDatas);
|
|
if (result)
|
|
OnBuySuccess();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
finally
|
|
{
|
|
if (_btnBuy != null)
|
|
_btnBuy.enabled = true;
|
|
}
|
|
}
|
|
|
|
private void OnBuySuccess()
|
|
{
|
|
var data = Data;
|
|
RefreshBuyState();
|
|
|
|
if (data.GetRemainingBuyCount() <= 0)
|
|
OnClickClose();
|
|
}
|
|
|
|
private void OnClickClose()
|
|
{
|
|
UIManager.Instance.DestroyUI(gameObject.name);
|
|
}
|
|
}
|
|
}
|