Files
tcg-client/Assets/TcgEngine/Scripts/Menu/PackBuyPanel.cs
2025-10-13 22:27:22 +08:00

125 lines
3.6 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
{
[Header("五连抽")]
public Button buy_five_btn; // 购买1张按钮
public Text buy_five_cove;
public Image five_coin_img;
public Image five_crystal_img;
[Header("十连抽")]
public Button buy_ten_btn; // 购买2张按钮
public Text buy_ten_cove;
public Image ten_coin_img;
public Image ten_crystal_img;
[Header("准备购买数据!")]
private 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);
SetBuyMessage();
}
public void SetPack(PackData pack)
{
this.pack = pack;
Show();
}
private void OnClickBuy(int quantity)
{
BuyPackApi(quantity);
}
private void SetBuyMessage()
{
// 设置图标
switch (pack.tradeCurrency)
{
case TradeCurrency.Coin:
five_coin_img.gameObject.SetActive(true);
ten_coin_img.gameObject.SetActive(true);
five_crystal_img.gameObject.SetActive(false);
ten_crystal_img.gameObject.SetActive(false);
break;
case TradeCurrency.Crystal:
five_coin_img.gameObject.SetActive(false);
ten_coin_img.gameObject.SetActive(false);
five_crystal_img.gameObject.SetActive(true);
ten_crystal_img.gameObject.SetActive(true);
break;
}
buy_five_cove.text = pack.cost.ToString();
buy_ten_cove.text = (pack.cost * 2).ToString();
}
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);
}
}
}
}