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

168 lines
7.1 KiB
C#

using asap.core;
using cfg;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using System;
using game;
public class GiftSelectionPackPanel : BasePanel
{
#region UI
private Button _btnBuy, _btnClose;
private TMP_Text _textTimer, _textPrice, _textRemain;
private GameObject _reward3, _reward4, _rewards;
#endregion
#region config
private Tables _tables;
private SelectionPackData _data;
private TbSpecialPack _spp;
private PlayerItemData _playerItemData;
#endregion
private IAPItemList _iap;
private EventAggregator _eventAggregator = new EventAggregator();
private int _rewardSlotsCount;
private void Awake()
{
#region UI
_btnClose = transform.Find("root/btn_close").GetComponent<Button>();
_btnBuy = transform.Find("root/btn_buy/btn_green").GetComponent<Button>();
_reward4 = transform.Find("root/reward_4").gameObject;
_reward3 = transform.Find("root/reward_3").gameObject;
_textTimer = transform.Find("root/text_time").GetComponent<TMP_Text>();
_textPrice = transform.Find("root/btn_buy/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
_textRemain = transform.Find("root/text_remaining").GetComponent<TMP_Text>();
#endregion
#region config
_tables = GContext.container.Resolve<Tables>();
_data = GContext.container.Resolve<SelectionPackData>();
_playerItemData = GContext.container.Resolve<PlayerItemData>();
_spp = GContext.container.Resolve<Tables>().TbSpecialPack;
#endregion
}
protected override void Start()
{
_btnClose.onClick.AddListener(() => UIManager.Instance.DestroyUI(UITypes.GiftSelectionPanel));
_textTimer.text = ConvertTools.ConvertTime2(_data.RemainingTime);
Observable.Interval(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ => { _textTimer.text = ConvertTools.ConvertTime2(_data.RemainingTime); })
.AddTo(this);
_eventAggregator.GetEvent<EventSelectionPackItemConfirm>().Subscribe(HandleItemConfirm).AddTo(this);
_rewardSlotsCount = _data.DropIDList.Count;
_reward4.SetActive(_rewardSlotsCount == 4);
_reward3.SetActive(_rewardSlotsCount == 3);
_rewards = _rewardSlotsCount == 4 ? _reward4 : _reward3;
for (int i = 0; i < _rewardSlotsCount; i++)
{
GiftSelectionPackSlot slot = _rewards.transform.Find($"gift{i + 1}")
.GetComponent<GiftSelectionPackSlot>();
if (_data.Selections[i] == -1)
{
slot.SetState();
}
else
{
slot.SetState(GetItemData(i, _data.Selections[i]));
}
int index = i;
_rewards.transform.Find($"gift{i + 1}/empty").GetComponent<Button>().onClick
.AddListener(async () => await OnClickSlot(index));
_rewards.transform.Find($"gift{i + 1}/reward_1/btn_selection").GetComponent<Button>().onClick
.AddListener(async () => await OnClickSlot(index));
}
_textRemain.text = LocalizationMgr.GetFormatTextValue("UI_FishingShopPanel_1",
_data.MaxPurchaseCount - _data.PurchaseCount, _data.MaxPurchaseCount);
//_textRemain.text = $"Remaining: {_data.MaxPurchaseCount - _data.PurchaseCount}/{_data.MaxPurchaseCount}";
_iap = _tables.TbIAPItemList.GetOrDefault(_data.IAPID);
//Debug.Log($"_iap: {_iap}");
SKUDetailDataEvent sdde = new SKUDetailDataEvent(_iap);
GContext.Publish(sdde);
_textPrice.text = sdde.price;
_btnBuy.onClick.AddListener(async () => await OnClickBuy());
oldPanelName = panelName;
base.Start();
}
private int _currentSlotSelected = -1;
private async System.Threading.Tasks.Task OnClickSlot(int idx)
{
GiftSelectionPackCustomizePanel customPanel = (await UIManager.Instance
.ShowUI(UITypes.GiftSelectPackCustomizePanel)).GetComponent<GiftSelectionPackCustomizePanel>();
DropPackageList d = _tables.TbDrop[_data.DropIDList[idx]].DropList;
int count = d.DropIDList.Count;
if (count != 3 && count != 4)
Debug.LogWarning($"Item count {count} on slot {idx} is probably not an expected number.");
ItemData[] items = new ItemData[count];
for (int i = 0; i < count; i++)
{
if (d.DropIDList[i] == 1002)
items[i] = new ItemData(d.DropIDList[i], _playerItemData.GetExtraCoinMag(d.DropCountList[i]));
else
items[i] = new ItemData(d.DropIDList[i], d.DropCountList[i]);
}
customPanel.Init(items, _data.Selections[idx], _eventAggregator);
_currentSlotSelected = idx;
}
private async System.Threading.Tasks.Task OnClickBuy()
{
if (!_data.IsFullySelected)
{
ToastPanel.Show(LocalizationMgr.GetFormatTextValue("UI_ToastPanel_79"));
return;
}
ShopBuyTypeData shopBuyTypeData = new ShopBuyTypeData();
shopBuyTypeData.type = ShopBuyType.EventPack;
shopBuyTypeData.ID = _data.CurrentEventID;
int dropID = 0;
bool res = await GContext.container.Resolve<PlayerShopData>().OnBuy(dropID, shopBuyTypeData, _iap, _data.RewardSelected);
if (res)
{
//Debug.Log($"<color=red>{}</color>")
//GContext.Publish(new EventHomeBuffPanelRefresh());
//_data.AddPurchase();
UIManager.Instance.DestroyUI(UITypes.GiftSelectionPanel);
}
}
private void HandleItemConfirm(EventSelectionPackItemConfirm e)
{
if (_currentSlotSelected == -1)
{
Debug.LogWarning("ConfirmButton pressed when no slot is selected.");
return;
}
_rewards.transform.Find($"gift{_currentSlotSelected + 1}").GetComponent<GiftSelectionPackSlot>()
.SetState(GetItemData(_currentSlotSelected, e.SelectedRewardIdx));
_data.UpdateSelection(_currentSlotSelected, e.SelectedRewardIdx);
_currentSlotSelected = -1;
}
private ItemData GetItemData(int slotIdx, int rewardIdx)
{
if (slotIdx < 0 || slotIdx >= _spp[_data.PackID].DropID.Count)
{
Debug.LogError($"SlotIndex {slotIdx} is out of expected range" +
$"(0, {_spp[_data.PackID].DropID.Count}).");
return null;
}
DropPackageList d = _tables.TbDrop[_spp[_data.PackID].DropID[slotIdx]].DropList;
if (rewardIdx < 0 || rewardIdx >= d.DropIDList.Count)
{
Debug.LogError($"PickIndex {rewardIdx} is out of expected range (0, {d.DropIDList.Count}).");
return null;
}
int itemID = d.DropIDList[rewardIdx];
int itemCount = d.DropCountList[rewardIdx];
if (itemID == 1002)
itemCount = _playerItemData.GetExtraCoinMag(itemCount);
return new ItemData(itemID, itemCount);
}
}
public class EventSelectionPackItemConfirm
{
public int SelectedRewardIdx;
public EventSelectionPackItemConfirm(int selectedRewardIdx)
{
SelectedRewardIdx = selectedRewardIdx;
}
}