_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Conversion/ConversionItem.cs
舒荣森 c1ae0f6e2e add
2024-11-26 21:53:31 +08:00

112 lines
3.4 KiB
C#

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
public class ConversionItem : BaseUIPanel
{
public TMP_InputField inputField;
public TextMeshProUGUI TextMeshProUGUI;
public Button button1;
public Button button2;
public Image Image1;
public Image Image2;
public Sprite shellSprite;
public Sprite eggSprite;
public mode mymode = mode.ShellToEgg;
public class coinsConvertVolut
{
public float voluteCoin;
public int convertType;
}
public class coinsConvertBeans
{
public float beansCoin;
public int convertType;
}
private async void Start()
{
inputField.onValueChanged.AddListener(GetInputData);
button1.onClick.AddListener(() => CancelOnClick(button1));
button2.onClick.AddListener(() => ConfirmOnClick(button2));
TextMeshProUGUI.text = "0";
//testhead = await testLogo();//꿎桿되쩌
}
public enum mode
{
ShellToEgg = 0,//卦옳瘻卦뎔
EggToShell = 1//卦뎔瘻卦옳
}
private void FixedUpdate()
{
if (mymode == mode.ShellToEgg)
{
Image1.sprite = shellSprite;
Image2.sprite = eggSprite;
}
else
{
Image2.sprite = shellSprite;
Image1.sprite = eggSprite;
}
}
public async void ConfirmOnClick(Button button)//횅땍객큐
{
await ButtonClickAnimationAsync(button.gameObject);
if (IsGreaterThanZeroDecimal(inputField.text))
{
coinsConvertVolut body = new coinsConvertVolut();
string send = "";
if (mymode == mode.ShellToEgg)
send = "{\"convertType\":" + ((int)mymode).ToString() + ",\"voluteCoin\":" + inputField.text + "}";
else
send = "{\"convertType\":" + ((int)mymode).ToString() + ",\"beansCoin\":" + inputField.text + "}";
body.convertType = (int)mymode;
Debug.Log(send);
//string response = await web.SendRequest(web.URL + "/snail/user/coinsConvert", "POST", send, testhead);//꿎桿
string response = await web.SendRequest(web.URL + "/snail/user/coinsConvert", "POST", send, Global.global.CreateHeaders());//攣駕쌈흙
Debug.Log("흙꽝:"+ JsonUtility.ToJson(body) + " 1.8럿쀼: " + response);
ServerResponse18 serverResponse = JsonConvert.DeserializeObject<ServerResponse18>(response);
if (serverResponse.code != 200)
{
addEventPopUp(serverResponse.message);
}
Destroy(gameObject);
}
else
{
addEventPopUp("헝渴흙攣횅돨鑒좆");
}
}
public void GetInputData(string userInput)
{
if (string.IsNullOrWhiteSpace(userInput) || !IsGreaterThanZeroDecimal(inputField.text)) {
userInput = "0";
}
TextMeshProUGUI.text = "0";
if (mymode == mode.ShellToEgg)
TextMeshProUGUI.text = (float.Parse(userInput) * 3).ToString("F2");
if (mymode == mode.EggToShell)
TextMeshProUGUI.text = (float.Parse(userInput) / 3).ToString("F2");
ButtonClickAnimationAsync(TextMeshProUGUI.gameObject);
}
}