130 lines
4.8 KiB
C#
Executable File
130 lines
4.8 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class AccountSettings : MonoBehaviour
|
|
{
|
|
public TMP_Text txt_status;
|
|
public Button btn_link;
|
|
public Button btn_signout;
|
|
public GameObject panel;
|
|
[Header("Link")]
|
|
public GameObject linkPanel;
|
|
public TMP_InputField usernameInput;
|
|
public TMP_InputField passwordInput;
|
|
public Button btn_link_register;
|
|
public GameObject loadingScreen;
|
|
void Awake(){
|
|
btn_link.onClick.AddListener(OnLink);
|
|
btn_link_register.onClick.AddListener(OnLinkReg);
|
|
|
|
}
|
|
|
|
public void Show(){
|
|
Refresh();
|
|
panel.SetActive(true);
|
|
}
|
|
|
|
public void Hide(){
|
|
panel.SetActive(false);
|
|
}
|
|
|
|
public void Refresh(){
|
|
btn_link.interactable = DBmanager.isGuest;
|
|
txt_status.text = (DBmanager.isGuest) ? "Account is Not Linked" : "Account is Linked";
|
|
txt_status.color = (DBmanager.isGuest) ? Color.red : Color.green;
|
|
}
|
|
|
|
public void OnInfo(){
|
|
string msg = "Your account is not Linked.\n Which means your progress will be\nlost if you signout or uninstall the game.\n\nTo Secure your account,\nClick on Link button to link your progress to an online account";
|
|
if(!DBmanager.isGuest){
|
|
msg = "Your account is Linked to an Online account.\nYour progress is bound to this account";
|
|
}
|
|
MessageDialog.instance.ShowMessage("Link Status",msg);
|
|
}
|
|
|
|
void OnLink(){
|
|
linkPanel.SetActive(true);
|
|
btn_link_register.interactable =false;
|
|
usernameInput.text=passwordInput.text ="";
|
|
|
|
AudioManager.instnace.UIClick();
|
|
}
|
|
|
|
async void OnLinkReg(){
|
|
if(usernameInput.text.Length <= 2 || passwordInput.text.Length <= 2){
|
|
MessageDialog.instance.ShowMessage("Error", "Username and password must have\nmore than 2 characters.");
|
|
return;
|
|
}
|
|
|
|
string results= await DBmanager.LinkAccount(usernameInput.text, passwordInput.text);
|
|
HandleResult(results,usernameInput.text, passwordInput.text);
|
|
}
|
|
|
|
public void GoogleLogin(){
|
|
GoogleLoginManager googleLogin= GetComponent<GoogleLoginManager>();
|
|
googleLogin.SignInWithGoogle();
|
|
}
|
|
|
|
void HandleResult(string results,string username,string password){
|
|
if(results == "0"){
|
|
MessageDialog.instance.ShowQuestion("Link Successful!", "Your Account has been successfully linked.\n\nThe game needs to be restarted.\nPress Yes to restart",OnYes:()=>{SceneManager.LoadScene("Login");}, OnNo:()=>{}, onlyYes:true);
|
|
}else{
|
|
int level = -1;
|
|
try{
|
|
int xp= int.Parse(results);
|
|
level = Mathf.CeilToInt(DBmanager.GetLevelFromXP(xp));
|
|
}catch{
|
|
MessageDialog.instance.ShowMessage("Error", "Couldn't link your account\nReason: " + results);
|
|
|
|
}
|
|
if(level > 0){
|
|
string msg = $"We've found a Level {level} Profile for this account. \nDo you want to use that profile?\n\nPS: Selecting that profile will destroy your local Profile";
|
|
MessageDialog.instance.ShowQuestion("Online Account Found!",msg,OnYes:OnMerge, OnNo:OnMergeCancelled);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async void GoogleSignComplete(){
|
|
loadingScreen.SetActive(true);
|
|
|
|
string results= await DBmanager.LinkAccount(GoogleLoginManager.loggedEmail+"#0",GoogleLoginManager.loggedEmail+"#0");
|
|
HandleResult(results,GoogleLoginManager.loggedEmail+"#0",GoogleLoginManager.loggedEmail+"#0");
|
|
loadingScreen.SetActive(false);
|
|
}
|
|
|
|
public async void OnMerge(){
|
|
PlayerPrefs.SetString("username", usernameInput.text);
|
|
PlayerPrefs.SetString("password", passwordInput.text);
|
|
PlayerPrefs.Save();
|
|
// MessageDialog.instance.ShowMessage("Test","works");
|
|
await Task.Delay(100);
|
|
Debug.Log("Accounts Merged");
|
|
MessageDialog.instance.ShowQuestion("Link Successful!", "Your Account has been successfully linked.\n\nThe game needs to be restarted.\nPress Yes to restart",OnYes:()=>{SceneManager.LoadScene("Login");}, OnNo:()=>{}, onlyYes:true);
|
|
}
|
|
|
|
public async void OnMergeCancelled(){
|
|
Debug.Log("Cancelled");
|
|
await Task.Delay(100);
|
|
|
|
MessageDialog.instance.ShowMessage("Link Failed!", "You chose not to merge profiles.\nLink process has been cancelled.");
|
|
}
|
|
|
|
public void ValidateLinkCredentials(){
|
|
if(usernameInput.text.Length <=2){
|
|
btn_link_register.interactable = false;
|
|
return;
|
|
}
|
|
if(passwordInput.text.Length <=2){
|
|
btn_link_register.interactable = false;
|
|
return;
|
|
}
|
|
|
|
btn_link_register.interactable = true;
|
|
}
|
|
}
|