init
This commit is contained in:
235
Assets/TcgEngine/Scripts/Menu/CardZoomPanel.cs
Normal file
235
Assets/TcgEngine/Scripts/Menu/CardZoomPanel.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace TcgEngine.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// When clicking on a card in menu, a box will appear with additional game info
|
||||
/// You can also buy cards in this panel
|
||||
/// </summary>
|
||||
|
||||
public class CardZoomPanel : UIPanel
|
||||
{
|
||||
public CardUI card_ui;
|
||||
public Text desc;
|
||||
public Image quantity_bar;
|
||||
public Text quantity_txt;
|
||||
|
||||
public GameObject trade_area;
|
||||
public InputField trade_quantity;
|
||||
public Text buy_cost;
|
||||
public Text sell_cost;
|
||||
public Text trade_error;
|
||||
|
||||
private CardData card;
|
||||
private VariantData variant;
|
||||
|
||||
private static CardZoomPanel instance;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
instance = this;
|
||||
|
||||
TabButton.onClickAny += OnClickTab;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
TabButton.onClickAny -= OnClickTab;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (card != null)
|
||||
{
|
||||
int quantity = GetBuyQuantity();
|
||||
int cost = quantity * card.cost * variant.cost_factor;
|
||||
buy_cost.text = cost.ToString();
|
||||
sell_cost.text = Mathf.RoundToInt(cost * GameplayData.Get().sell_ratio).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowCard(CardData card, VariantData variant)
|
||||
{
|
||||
this.card = card;
|
||||
this.variant = variant;
|
||||
|
||||
UserData udata = Authenticator.Get().UserData;
|
||||
int quantity = udata.GetCardQuantity(card, variant);
|
||||
quantity_txt.text = quantity.ToString();
|
||||
quantity_txt.enabled = quantity > 0;
|
||||
quantity_bar.enabled = quantity > 0;
|
||||
trade_quantity.text = "1";
|
||||
trade_error.text = "";
|
||||
trade_area?.SetActive(card.deckbuilding && card.cost > 0);
|
||||
|
||||
card_ui.SetCard(card, variant);
|
||||
string desc = card.GetDesc();
|
||||
string adesc = card.GetAbilitiesDesc();
|
||||
if(!string.IsNullOrWhiteSpace(desc))
|
||||
this.desc.text = desc + "\n\n" + adesc;
|
||||
else
|
||||
this.desc.text = adesc;
|
||||
|
||||
Show();
|
||||
}
|
||||
|
||||
public void RefreshCard()
|
||||
{
|
||||
ShowCard(card, variant);
|
||||
}
|
||||
|
||||
private async void BuyCardTest()
|
||||
{
|
||||
int quantity = GetBuyQuantity();
|
||||
int cost = (quantity * card.cost * variant.cost_factor);
|
||||
if (quantity <= 0)
|
||||
return;
|
||||
|
||||
UserData udata = Authenticator.Get().UserData;
|
||||
if (udata.coins < cost)
|
||||
return;
|
||||
|
||||
udata.AddCard(card.id, variant.id, quantity);
|
||||
udata.coins -= cost;
|
||||
await Authenticator.Get().SaveUserData();
|
||||
CollectionPanel.Get().ReloadUser();
|
||||
Hide();
|
||||
}
|
||||
|
||||
private async void BuyCardApi()
|
||||
{
|
||||
BuyCardRequest req = new BuyCardRequest();
|
||||
req.card = card.id;
|
||||
req.variant = variant.id;
|
||||
req.quantity = GetBuyQuantity();
|
||||
|
||||
if (req.quantity <= 0)
|
||||
return;
|
||||
|
||||
string url = ApiClient.ServerURL + "/users/cards/buy/";
|
||||
string jdata = ApiTool.ToJson(req);
|
||||
trade_error.text = "";
|
||||
|
||||
WebResponse res = await ApiClient.Get().SendPostRequest(url, jdata);
|
||||
if (res.success)
|
||||
{
|
||||
CollectionPanel.Get().ReloadUser();
|
||||
Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
trade_error.text = res.error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async void SellCardTest()
|
||||
{
|
||||
int quantity = GetBuyQuantity();
|
||||
int cost = Mathf.RoundToInt(quantity * card.cost * variant.cost_factor * GameplayData.Get().sell_ratio);
|
||||
if (quantity <= 0)
|
||||
return;
|
||||
|
||||
UserData udata = Authenticator.Get().UserData;
|
||||
if (!udata.HasCard(card.id, variant.id, quantity))
|
||||
return;
|
||||
|
||||
udata.AddCard(card.id, variant.id, -quantity);
|
||||
udata.coins += cost;
|
||||
await Authenticator.Get().SaveUserData();
|
||||
CollectionPanel.Get().ReloadUser();
|
||||
MainMenu.Get().RefreshDeckList();
|
||||
Hide();
|
||||
}
|
||||
|
||||
private async void SellCardApi()
|
||||
{
|
||||
BuyCardRequest req = new BuyCardRequest();
|
||||
req.card = card.id;
|
||||
req.variant = variant.id;
|
||||
req.quantity = GetBuyQuantity();
|
||||
|
||||
if (req.quantity <= 0)
|
||||
return;
|
||||
|
||||
string url = ApiClient.ServerURL + "/users/cards/sell/";
|
||||
string jdata = ApiTool.ToJson(req);
|
||||
trade_error.text = "";
|
||||
|
||||
WebResponse res = await ApiClient.Get().SendPostRequest(url, jdata);
|
||||
if (res.success)
|
||||
{
|
||||
CollectionPanel.Get().ReloadUser();
|
||||
Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
trade_error.text = res.error;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClickBuy()
|
||||
{
|
||||
if (Authenticator.Get().IsTest())
|
||||
{
|
||||
BuyCardTest();
|
||||
}
|
||||
if (Authenticator.Get().IsApi())
|
||||
{
|
||||
BuyCardApi();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClickSell()
|
||||
{
|
||||
if (Authenticator.Get().IsTest())
|
||||
{
|
||||
SellCardTest();
|
||||
}
|
||||
if (Authenticator.Get().IsApi())
|
||||
{
|
||||
SellCardApi();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClickTab(TabButton btn)
|
||||
{
|
||||
if (btn.group == "menu")
|
||||
Hide();
|
||||
}
|
||||
|
||||
public int GetBuyQuantity()
|
||||
{
|
||||
bool success = int.TryParse(trade_quantity.text, out int quantity);
|
||||
if (success)
|
||||
return quantity;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public CardData GetCard()
|
||||
{
|
||||
return card;
|
||||
}
|
||||
|
||||
public string GetCardId()
|
||||
{
|
||||
return card.id;
|
||||
}
|
||||
|
||||
public string GetCardVariant()
|
||||
{
|
||||
return variant.id;
|
||||
}
|
||||
|
||||
public static CardZoomPanel Get()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user