using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; using TMPro; public class TradeMenu : MonoBehaviour { public Transform tradeListParent; public GameObject tradeListItem; public Button refreshIndicator; public TMP_Text coinsTxt; public TMP_Text gemsTxt; public List pooledEntries; public List trades; [Header("Selected Trade Data")] public TradeRequest selectedTrade = null; public TMP_Text sellerNameTxt; public GameObject coinsItem; public GameObject gemsItem; public GameObject metalsItem; public GameObject oxygenItem; public Image itemImg; public Sprite defaultFrame; public Sprite mineFrame; public Button acceptBtn; void Start() { pooledEntries = new List(); //pooledEntries.Add(tradeListItem); refreshIndicator.onClick.AddListener(Refresh); acceptBtn.onClick.AddListener(OnAcceptClicked); Refresh(); } // Update is called once per frame void Update() { } public bool refreshing = false; public async void Refresh() { refreshing = true; refreshIndicator.interactable=false; refreshIndicator.GetComponent().enabled = (true); //Clean existing entries foreach (GameObject entry in pooledEntries) { entry.SetActive(false); } coinsTxt.text = DBmanager.Coins.ToString(); gemsTxt.text = DBmanager.Gems.ToString(); selectedTrade = null; // if(selectedTrade==null){ coinsItem.SetActive(false); gemsItem.SetActive(false); metalsItem.SetActive(false); oxygenItem.SetActive(false); acceptBtn.interactable = false; itemImg.sprite = null; //} using (UnityWebRequest www = UnityWebRequest.Get(DBmanager.phpRoot + "get_trades.php")) { var operation = www.SendWebRequest(); while (!operation.isDone) { await Task.Yield(); } if (www.downloadHandler.text.ToLower().Contains("0 results")) { Debug.Log("No trade requests for now"); } else { string[] splitChars = { "" }; string[] data = www.downloadHandler.text.Split(splitChars, System.StringSplitOptions.RemoveEmptyEntries); trades = new List(); foreach (string row in data) { try { TradeRequest trade = JsonConvert.DeserializeObject(row); trades.Add(trade); if(trade.seller == DBmanager.username){ trades[trades.Count-1] = trades[0]; trades[0] = trade; } } catch { } } Debug.Log($"Added {trades.Count} trades from {data.Length} entries"); } } //Populate Trade Entries in list //Fill pool if exceeds int amountToFill = trades.Count - pooledEntries.Count; if (amountToFill > 0) { for (int i = 0; i < amountToFill; i++) { GameObject newEntry = Instantiate(tradeListItem, tradeListParent); newEntry.SetActive(false); pooledEntries.Add(newEntry); } } if(trades.Count > pooledEntries.Count){Debug.LogError("Somethings wrong, I can feel it");} //Set pooled items for (int i = 0; i < trades.Count; i++) { TradeRequest trade = trades[i]; InventoryItem item = Inventory.GetInventoryItem(trade.item); if (item == null) { Debug.LogError("Couldn't find any inventory item for " + trade.item); continue; } pooledEntries[i].SetActive(true); pooledEntries[i].transform.GetChild(0).GetComponent().sprite = item.image; pooledEntries[i].GetComponentInChildren().text = trade.item; pooledEntries[i].GetComponent