UPF/Assets/Game/Scripts/ChangeUsername.cs
2023-02-24 22:14:55 +05:30

94 lines
2.7 KiB
C#
Executable File

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using CustomLogger;
using Debug = CustomLogger.Debug;
public class ChangeUsername : MonoBehaviour
{
public GameObject panel;
public TMP_InputField newNameInput;
public Button changeBtn;
bool hasChanged = true;
void Awake(){
changeBtn.onClick.AddListener(OnChangeClicked);
}
public void Show(){
if(DBmanager.isGuest){
MessageDialog.instance.ShowMessage("Failed", "You can't change username with a guest account.\nLink your account to proceed");
return;
}
Refresh();
panel.SetActive(true);
}
public void Hide(){
panel.SetActive(false);
}
async void Refresh(){
Disable();
newNameInput.text = "";
hasChanged = await DBmanager.GetUsernameChanged();
if(hasChanged){
//Not free
changeBtn.transform.GetChild(1).GetComponentInChildren<TMP_Text>().text = "10";
}else{
changeBtn.transform.GetChild(1).GetComponentInChildren<TMP_Text>().text = "0";
MessageDialog.instance.ShowMessage("Notice", "You can change your username FREE for the first time.\nAfter that changing name Costs 10 Gems");
}
Enable();
}
async void OnChangeClicked(){
if(!changeBtn.interactable){
return;
}
if(newNameInput.text.Length < 3){
MessageDialog.instance.ShowMessage("Failed", "Username requires at-least 3 characters");
}
if(hasChanged){
if(DBmanager.Gems < 10){
MessageDialog.instance.ShowMessage("Failed", "Not enough gems to change name");
return;
}
}
string response = await DBmanager.ChangeName(newNameInput.text);
if(response.Contains("username already exists")){
MessageDialog.instance.ShowMessage("Failed", "Name you entered already exists, Please enter a different name");
}
if(response == "0"){
GameManager.Refresh();
if(hasChanged){
DBmanager.SetGems(DBmanager.Gems -10);
}
MessageDialog.instance.ShowMessage("Success","Username successfully changed!");
Hide();
}else{
Debug.Log("Couldn't change username:" + response);
MessageDialog.instance.ShowMessage("Failed", "Failed to change username");
Feedbacks.Send("Change username", response, "",Debug.loggedText);
}
}
void Disable(){
newNameInput.interactable = changeBtn.interactable = false;
}
void Enable(){
newNameInput.interactable = changeBtn.interactable = true;
}
}