Files
ft/Client/Assets/Scripts/UI/OfferStoryChain/OfferStoryChainScrollSlotView.cs
2026-06-29 21:18:33 +08:00

160 lines
5.6 KiB
C#

using asap.core;
using cfg;
using GameCore;
using OfferStoryChain;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(RectTransform))]
public class OfferStoryChainScrollSlotView : MonoBehaviour
{
[SerializeField] private RewardItemNew[] rewardItemList;
[SerializeField] private GameObject goBuy, goDone, goLock, fxLock, goSelected;
[SerializeField] private TMP_Text textPrice/* , textDiscount */;
[SerializeField] private Button btnBuy;
[SerializeField] private RectTransform triggerItem;
private IAPItemList _iapItemList;
private readonly PlayerItemData _playerItemData = GContext.container.Resolve<PlayerItemData>();
private ScrollerItemViewState _state;
public RectTransform RectTransform => GetComponent<RectTransform>();
public RectTransform TriggerItem => triggerItem;
private int _idx;
public void Init(ScrollerItemViewData data)
{
var itemDataList = _playerItemData.GetItemDataByDropIdLureInflation(null, data.DropId);
for (int i = 0; i < rewardItemList.Length; i++)
{
if (i < itemDataList.Count)
rewardItemList[i].SetData(itemDataList[i]);
else
rewardItemList[i].gameObject.SetActive(false);
}
SetState(data.State);
if (data.IapId != 0)
{
_playerItemData.ResolveIapId(data.IapId, out _iapItemList, out var priceString);
textPrice.text = priceString;
btnBuy.onClick.AddListener(() => OnClickBuy(data.DropId, _iapItemList, data.EventId));
}
else
{
textPrice.text = LocalizationMgr.GetText("UI_HomePanel_101013");
btnBuy.onClick.AddListener(() => OnClickBuy(data.DropId));
}
_idx = data.Index;
fxLock.SetActive(false);
}
private async void OnClickBuy(int dropId, IAPItemList iapItemList, int eventId)
{
try
{
switch(_state)
{
case ScrollerItemViewState.Locked:
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_30"));
return;
case ScrollerItemViewState.Bought:
return;
case ScrollerItemViewState.Available:
break;
}
var roundCount = GContext.container.Resolve<Manager>().RoundCount;
(var res, var rewards) = await GContext.container.Resolve<Manager>().GrantReward(dropId, iapItemList, eventId);
if (res)
{
var pc = new OfferStoryChainPurchaseContext
{
Index = _idx,
RoundCount = roundCount,
RewardsGiven = rewards
};
GContext.container.Resolve<OfferStoryChain.Manager>().EventAggregator.Publish(pc);
}
else
{
// Something?
}
}
catch (System.Exception e)
{
Debug.LogError(e);
}
}
private async void OnClickBuy(int dropId)
{
try
{
switch(_state)
{
case ScrollerItemViewState.Locked:
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_30"));
return;
case ScrollerItemViewState.Bought:
return;
case ScrollerItemViewState.Available:
break;
}
// if (_state != ScrollerItemViewState.Available)
// return;
var roundCount = GContext.container.Resolve<Manager>().RoundCount;
var rewards = GContext.container.Resolve<Manager>().GrantReward(dropId);
var pc = new OfferStoryChainPurchaseContext
{
Index = _idx,
RoundCount = roundCount,
RewardsGiven = rewards
};
GContext.container.Resolve<OfferStoryChain.Manager>().EventAggregator.Publish(pc);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
}
// public void OnBuySuccess()
// {
// GContext.container.Resolve<OfferStoryChain.Manager>()?.MoveToNextSlotAndTryGrantFinalReward();
// }
public void SetState(ScrollerItemViewState state)
{
_state = state;
goBuy.SetActive(state != ScrollerItemViewState.Bought);
goDone.SetActive(state == ScrollerItemViewState.Bought);
goLock.SetActive(state == ScrollerItemViewState.Locked);
goSelected.SetActive(state == ScrollerItemViewState.Available);
}
private void Reset()
{
rewardItemList = transform.Find("Item/reward").GetComponentsInChildren<RewardItemNew>();
goBuy = transform.Find("Item/btn_buy").gameObject;
goDone = transform.Find("Item/done").gameObject;
goLock = transform.Find("Item/btn_buy/btn_green/lock").gameObject;
textPrice = transform.Find("Item/btn_buy/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
// textDiscount = transform.Find("Item/tag_more/text_more").GetComponent<TMP_Text>();
btnBuy = transform.Find("Item/btn_buy/btn_green").GetComponent<Button>();
triggerItem = transform.Find("Item/extra_reward/icon").GetComponent<RectTransform>();
fxLock = transform.Find("Item/btn_buy/btn_green/fx_ui_battlepasspanel_unlock").gameObject;
goSelected = transform.Find("Item/current").gameObject;
}
public void OnPurchaseDisplay()
{
SetState(ScrollerItemViewState.Bought);
}
public void PlayUnlockFx()
{
//TODO: fxLock support
fxLock.SetActive(true);
}
}