Files
tcg-client/Assets/TcgEngine/Scripts/Menu/PackBuyPanel.cs
2025-10-13 18:30:07 +08:00

91 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using TcgEngine.Client;
using UnityEngine;
using UnityEngine.UI;
namespace TcgEngine.UI
{
/// <summary>
/// 简化卡包购买面板只提供“买1包”和“买2包”按钮保留API购买逻辑
/// </summary>
public class PackBuyPanel : UIPanel
{
public Button buy_five_btn; // 购买1张按钮
public Button buy_ten_btn; // 购买2张按钮
[Header("准备购买数据!")]
public PackData pack; // 当前购买的卡包数据
protected override void Awake()
{
base.Awake();
this.Show();
buy_five_btn.onClick.AddListener(() => OnClickBuy(1));
buy_ten_btn.onClick.AddListener(() => OnClickBuy(2));
}
private void OnDestroy()
{
buy_five_btn.onClick.RemoveAllListeners();
buy_ten_btn.onClick.RemoveAllListeners();
}
/// <summary>
/// id传输
/// </summary>
/// <param name="pack">卡牌id</param>
public void SetPack(string packId)
{
pack = PackData.Get(packId);
}
public void SetPack(PackData pack)
{
this.pack = pack;
Show();
}
private async void BuyPackApi(int quantity)
{
if (pack == null || quantity <= 0) return;
BuyPackRequest req = new BuyPackRequest
{
pack = pack.id,
quantity = quantity
};
string url = ApiClient.ServerURL + "/users/packs/buy/";
string jdata = ApiTool.ToJson(req);
WebResponse res = await ApiClient.Get().SendPostRequest(url, jdata);
if (res.success)
{
if (PackPanel.Get() != null)
{
PackPanel.Get()?.ReloadUserPack();
PackPanel.Get()?.RefreshCurrency();
}
if (HandPackArea.Get() != null)
{
HandPackArea.Get().LoadPacks();
}
}
else
{
Debug.LogError(res.error);
}
}
private void OnClickBuy(int quantity)
{
BuyPackApi(quantity);
}
}
}