168 lines
6.5 KiB
C#
168 lines
6.5 KiB
C#
using UnityEngine;
|
||
using GameCore;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
using UnityEngine.Assertions;
|
||
using asap.core;
|
||
using cfg;
|
||
using System.Threading.Tasks;
|
||
|
||
public class EventNeoNormalPackPanel : MonoBehaviour
|
||
{
|
||
[SerializeField] private RewardItemNew[] itemListViewLeft, itemListViewRight;
|
||
[SerializeField] private Button btnLeft, btnRight;
|
||
[SerializeField] private TMP_Text textPriceLeft, textPriceRight, textPriceOrigin, textDiscount;
|
||
[SerializeField] private BingoTimer timer;
|
||
[SerializeField] private CloseSelfPanel closeSelfPanel;
|
||
private PlayerItemData _playerItemData;
|
||
private IAPItemList _iapItemListLeft, _iapItemListRight;
|
||
private int _dropIdLeft, _dropIdRight, _eventId;
|
||
|
||
public void Start()
|
||
{
|
||
btnLeft.onClick.AddListener(async () => { await OnClickBuy(_dropIdLeft, _iapItemListLeft); });
|
||
btnRight.onClick.AddListener(async () => { await OnClickBuy(_dropIdRight, _iapItemListRight); });
|
||
}
|
||
|
||
public void Init(EventNeoNormalPackData data)
|
||
{
|
||
var tables = GContext.container.Resolve<Tables>();
|
||
var res = tables.TbEventPackManager.DataMap.TryGetValue(data.PackId, out var epm);
|
||
Assert.IsTrue(res, $"[NeoNormalPack] Event pack {data.PackId} not found in event pack manager table.");
|
||
var packIdList = epm.VIPPackList[GContext.container.Resolve<PlayerData>().PriceLv];
|
||
try
|
||
{
|
||
(var startTime, var expiryTime) = FtUtils.GetEventTimeLimit(data.EventId);
|
||
InitPanelWithData(new NeoNormalPackDataInternal(packIdList.ToArray(), data.EventId, expiryTime));
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
Debug.LogError(e);
|
||
}
|
||
}
|
||
|
||
protected virtual void InitPanelWithData(NeoNormalPackDataInternal data)
|
||
{
|
||
_playerItemData = GContext.container.Resolve<PlayerItemData>();
|
||
Assert.IsTrue(itemListViewLeft.Length == 2 && itemListViewRight.Length == 2, "Inconsistent item count, expect 2.");
|
||
_playerItemData.ResolveIapId(data.IapIdLeft, out _iapItemListLeft, textPriceLeft);
|
||
_playerItemData.ResolveIapId(data.IapIdRight, out _iapItemListRight, textPriceRight);
|
||
_dropIdLeft = data.DropIdLeft;
|
||
_dropIdRight = data.DropIdRight;
|
||
SetItemListView(data.DropIdLeft, itemListViewLeft);
|
||
SetItemListView(data.DropIdRight, itemListViewRight);
|
||
textPriceOrigin.text = data.PriceOrigin;
|
||
textDiscount.text = ConvertTools.GetDiscountStringActual(data.Discount);
|
||
timer.Init(new TimerContext
|
||
{
|
||
StartTime = ZZTimeHelper.UtcNow() - System.TimeSpan.FromSeconds(5),
|
||
ExpiryTime = data.ExpiryTime,
|
||
OnExpire = closeSelfPanel.OnClickClose
|
||
});
|
||
_eventId = data.EventId;
|
||
}
|
||
|
||
private void SetItemListView(int dropId, RewardItemNew[] itemListView)
|
||
{
|
||
var itemList = _playerItemData.GetItemDataByDropIdLureInflation(null, dropId);
|
||
Assert.IsTrue(itemList.Count == itemListView.Length, "Inconsistent drop count with item view count.");
|
||
for (int i = 0; i < itemList.Count; i++)
|
||
itemListView[i].SetData(itemList[i]);
|
||
}
|
||
|
||
private async Task OnClickBuy(int dropId, IAPItemList iAPItemList)
|
||
{
|
||
bool res = await GContext.container.Resolve<PlayerShopData>().OnBuy(dropId,
|
||
new ShopBuyTypeData { type = ShopBuyType.None, ID = _eventId }, iAPItemList,
|
||
_playerItemData.GetItemDataByDropIdLureInflation(null, dropId));
|
||
if (res)
|
||
{
|
||
GContext.Publish(new EventTicketUpdate());
|
||
closeSelfPanel.OnClickClose();
|
||
}
|
||
}
|
||
|
||
protected internal class NeoNormalPackDataInternal
|
||
{
|
||
public int EventId { get; set; }
|
||
public int IapIdLeft { get; set; }
|
||
public int IapIdRight { get; set; }
|
||
public int DropIdLeft { get; set; }
|
||
public int DropIdRight { get; set; }
|
||
public string PriceOrigin { get; set; }
|
||
public float Discount { get; set; }
|
||
public System.DateTime ExpiryTime { get; set; }
|
||
|
||
public NeoNormalPackDataInternal(int[] packIdList, int eventId, System.DateTime expiryTime)
|
||
{
|
||
Assert.IsTrue(packIdList.Length == 3, $"[NeoNormalPack] Event pack has {packIdList.Length} pack ids, not 3.");
|
||
var packList = new Pack[3];
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
var res = GContext.container.Resolve<Tables>().TbPack.DataMap.TryGetValue(packIdList[i], out packList[i]);
|
||
Assert.IsTrue(res, $"[NeoNormalPack] Event pack {packIdList[i]} not found in pack table.");
|
||
}
|
||
IapIdLeft = packList[0].IAPID;
|
||
IapIdRight = packList[1].IAPID;
|
||
DropIdLeft = packList[0].DropID;
|
||
DropIdRight = packList[1].DropID;
|
||
GContext.container.Resolve<PlayerItemData>().ResolveIapId(packList[2].IAPID, out _, out var price);
|
||
PriceOrigin = price;
|
||
Discount = packList[1].Rebate;
|
||
EventId = eventId;
|
||
ExpiryTime = expiryTime;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开 Neo 普通礼包界面:优先使用 data.Panel,否则从 TbEventPackManager[PackId].Panel 解析。
|
||
/// </summary>
|
||
public static async Task<bool> TryShowAsync(EventNeoNormalPackData data)
|
||
{
|
||
if (data == null)
|
||
{
|
||
Debug.LogError("[NeoNormalPack] EventNeoNormalPackData is null.");
|
||
return false;
|
||
}
|
||
|
||
var panelPath = data.GetResolvedPanelPath();
|
||
if (string.IsNullOrEmpty(panelPath))
|
||
{
|
||
Debug.LogError($"[NeoNormalPack] Panel path empty, PackId={data.PackId}, EventId={data.EventId}");
|
||
return false;
|
||
}
|
||
|
||
var uiType = new UIType(panelPath);
|
||
var go = await UIManager.Instance.ShowUINotLoading(uiType);
|
||
if (go == null)
|
||
return false;
|
||
|
||
var panel = go.GetOrAddComponent<EventNeoNormalPackPanel>();
|
||
if (panel == null)
|
||
{
|
||
Debug.LogError("[NeoNormalPack] EventNeoNormalPackPanel component missing.");
|
||
return false;
|
||
}
|
||
|
||
panel.Init(data);
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public class EventNeoNormalPackData
|
||
{
|
||
public int EventId;
|
||
public int PackId;
|
||
/// <summary>
|
||
/// 界面资源路径;若为空则从 TbEventPackManager[PackId].Panel 解析。
|
||
/// </summary>
|
||
public string Panel;
|
||
|
||
public string GetResolvedPanelPath()
|
||
{
|
||
if (!string.IsNullOrEmpty(Panel))
|
||
return Panel;
|
||
var epm = GContext.container.Resolve<Tables>().TbEventPackManager.GetOrDefault(PackId);
|
||
return epm?.Panel;
|
||
}
|
||
} |