138 lines
4.2 KiB
C#
Executable File
138 lines
4.2 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using Debug = CustomLogger.Debug;
|
|
using System;
|
|
|
|
public class TradingPost : MonoBehaviour
|
|
{
|
|
public Building building;
|
|
|
|
public TMP_Text txtMetalAmount;
|
|
public TMP_Text txtGoldAmount;
|
|
public Slider metalSlider;
|
|
// public Slider metalSlider;
|
|
public TMP_Text warningTxt;
|
|
public Button tradeButton;
|
|
void Start()
|
|
{
|
|
metalSlider.onValueChanged.AddListener(OnGoldChanged);
|
|
tradeButton.onClick.AddListener(OnTrade);
|
|
Refresh();
|
|
}
|
|
|
|
public void Show(){
|
|
string stackTrace="";
|
|
foreach(Building building in BuildingManager.instance.buildings){
|
|
stackTrace += building.ToString() +"\n";
|
|
}
|
|
checkTradePostBug(stackTrace);
|
|
gameObject.SetActive(true);
|
|
|
|
try{
|
|
Refresh();
|
|
}catch(Exception e){
|
|
Feedbacks.Send("Trading Post Error", e.Message, e.StackTrace, e.Source);
|
|
}
|
|
|
|
}
|
|
|
|
async void checkTradePostBug(string stackTrace){
|
|
await System.Threading.Tasks.Task.Delay(2000);
|
|
if(goldCount < 5){
|
|
Feedbacks.Send("Trading Post bug", $"gpm:{goldPerMetal}, metal:{metalCount}, goldCount:{goldCount}",stackTrace:stackTrace,additionalData:CustomLogger.Debug.loggedText);
|
|
}
|
|
}
|
|
|
|
public void Hide(){
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
|
|
int metalCount = 10;
|
|
public int goldCount => goldPerMetal * metalCount /10;
|
|
// public int goldPerMetal => (int)(float.Parse(building.buildingData.levels[building.curLevel].stats[0].value) * 10);
|
|
public int goldPerMetal {
|
|
get{
|
|
string _rate =building.buildingData.levels[building.curLevel].stats[0].value;
|
|
Debug.Log(_rate);
|
|
|
|
float rate = 0.2f;
|
|
if(_rate.Contains("0.1")){
|
|
rate = 0.1f;
|
|
}else if(_rate.Contains("0.2")){
|
|
rate = 0.2f;
|
|
}else if(_rate.Contains("0.3")){
|
|
rate = 0.3f;
|
|
}else if(_rate.Contains("0.4")){
|
|
rate = 0.4f;
|
|
}else if(_rate.Contains("0.5")){
|
|
rate = 0.5f;
|
|
}else{
|
|
Feedbacks.Send("Trading Post Error", "String: " + _rate,building.ToString(), Debug.loggedText);
|
|
}
|
|
|
|
// try{
|
|
// rate = float.Parse(_rate);
|
|
// }catch(Exception e){
|
|
// Feedbacks.Send("Trading Post Error", e.Message + " String: " + _rate, e.StackTrace, building.ToString() + "LOG\n\n" + Debug.loggedText);
|
|
// }
|
|
return (int)(rate * 10);
|
|
}
|
|
}
|
|
|
|
void OnGoldChanged(float value){
|
|
Refresh();
|
|
}
|
|
|
|
void OnTrade(){
|
|
MessageDialog.instance.ShowQuestion("Are you sure?", $"You are about to trade <b>{metalCount} Energy</b> for <b>{goldCount} Coins</b>", OnYes:OnTradeConfirmed, OnNo:()=>{});
|
|
}
|
|
|
|
void OnTradeConfirmed(){
|
|
if(Refresh()==1){
|
|
MessageDialog.instance.ShowMessage("Error", "You need to have atleast 10 golds to trade.");
|
|
return;
|
|
}
|
|
Hide();
|
|
DBmanager.SetCoins(DBmanager.Coins + goldCount);
|
|
DBmanager.SetMetal(DBmanager.Metal - metalCount);
|
|
}
|
|
|
|
public int Refresh(){
|
|
if(DBmanager.Metal < 10){
|
|
warningTxt.text = "You need atleast 10 golds to trade";
|
|
tradeButton.interactable = false;
|
|
metalSlider.interactable = false;
|
|
txtGoldAmount.text = "0";
|
|
txtMetalAmount.text = "0";
|
|
return 1;
|
|
}
|
|
// throw new NullReferenceException();
|
|
tradeButton.interactable = true;
|
|
metalSlider.interactable = true;
|
|
warningTxt.text = "";
|
|
|
|
float selectedPart = (metalSlider.value/metalSlider.maxValue) * DBmanager.Metal;
|
|
metalCount = ((int)selectedPart).RoundOff();
|
|
|
|
|
|
if(metalCount > DBmanager.Coins){
|
|
metalCount -= 10;
|
|
}
|
|
if(metalCount <= 0){
|
|
metalCount =10;
|
|
}
|
|
txtMetalAmount.text = metalCount.ToString();
|
|
|
|
txtGoldAmount.text = (goldCount).ToString();
|
|
|
|
Debug.Log(building.curLevel);
|
|
|
|
return 0;
|
|
}
|
|
|
|
}
|