162 lines
5.4 KiB
C#
162 lines
5.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using Debug = CustomLogger.Debug;
|
|
|
|
public class NewLoginManager : MonoBehaviour
|
|
{
|
|
public GameObject loadingPanel;
|
|
public GameObject newUsernamePanel;
|
|
public GameObject usernameWarning;
|
|
public TMP_InputField usernameInput;
|
|
public Button btn_login;
|
|
public Button btn_login_google;
|
|
public TMP_Text statusTxt;
|
|
public static bool loginSaved {get{return PlayerPrefs.HasKey("username") && PlayerPrefs.HasKey("password");}}
|
|
public static string savedUsername{get{return (loginSaved)? PlayerPrefs.GetString("username") : "";}}
|
|
public static string savedPassword{get{return (loginSaved)? PlayerPrefs.GetString("password") : "";}}
|
|
|
|
void Start()
|
|
{
|
|
// Feedbacks.Send("Test", "test", "test", CustomLogger.Debug.loggedText);
|
|
|
|
statusTxt.text = "Checking credentials";
|
|
btn_login.onClick.AddListener(OnRegisterButton);
|
|
loadingPanel.SetActive(true);
|
|
if(!loginSaved){
|
|
Register();
|
|
}else{
|
|
TutorialManager.justRegistered=false;
|
|
Login();
|
|
}
|
|
}
|
|
|
|
public void Register(){
|
|
if(usernameInput.text.Length >=2){
|
|
StartCoroutine(register());
|
|
loadingPanel.SetActive(true);
|
|
}else{
|
|
newUsernamePanel.SetActive(true);
|
|
loadingPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
IEnumerator register(){
|
|
statusTxt.text = "Registering you in...";
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
|
|
form.AddField("username", usernameInput.text);
|
|
|
|
WWW www = new WWW(DBmanager.phpRoot + "register_instant.php",form);
|
|
yield return www;
|
|
|
|
OnRegister(www.text);
|
|
}
|
|
|
|
IEnumerator registerGoogle(string email){
|
|
statusTxt.text = "Registering you in...";
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
|
|
form.AddField("name", email + "#0");
|
|
form.AddField("display_name", PlayerPrefs.GetString("displayname"));
|
|
form.AddField("password", email + "#0");
|
|
|
|
WWW www = new WWW(DBmanager.phpRoot + "register.php",form);
|
|
yield return www;
|
|
|
|
PlayerPrefs.SetString("username",email+"#0");
|
|
PlayerPrefs.SetString("password", email+"#0");
|
|
PlayerPrefs.Save();
|
|
|
|
Login(true);
|
|
}
|
|
|
|
void OnRegister(string id){
|
|
try{
|
|
// TutorialManager.justRegistered=true;
|
|
int new_id = int.Parse(id);
|
|
PlayerPrefs.SetString("username", usernameInput.text+"#"+ id);
|
|
PlayerPrefs.SetString("password", id);
|
|
PlayerPrefs.Save();
|
|
|
|
Login();
|
|
}catch (Exception e){
|
|
statusTxt.text = "Failed";
|
|
MessageDialog.instance.ShowMessage("Failed!", "Failed to register you into the game.\nReason: " + id);
|
|
newUsernamePanel.SetActive(true);
|
|
usernameInput.text = "";
|
|
|
|
Feedbacks.Send("Register Failed", e.Message, e.StackTrace, CustomLogger.Debug.loggedText);
|
|
}
|
|
|
|
}
|
|
|
|
void Login(bool justRegistered = false){
|
|
StartCoroutine(login(justRegistered));
|
|
}
|
|
|
|
IEnumerator login(bool googleSigned = false, bool justRegistered = false){
|
|
statusTxt.text = "Logging you in...";
|
|
|
|
WWWForm form = new WWWForm();
|
|
yield return new WaitForSeconds(1);
|
|
form.AddField("name", savedUsername);
|
|
form.AddField("password", savedPassword);
|
|
form.AddField("google", googleSigned ? 1 : 0);
|
|
WWW www = new WWW(DBmanager.phpRoot + "login.php",form);
|
|
|
|
yield return www;
|
|
Debug.Log(www.text);
|
|
|
|
if(DBmanager.Login(www.text)==0){
|
|
if(justRegistered){
|
|
LoadingScreen.instance.LoadLevel("Minigame");
|
|
}else{
|
|
LoadingScreen.instance.LoadLevel("GameScene");
|
|
|
|
}
|
|
|
|
// if(TutorialManager.justRegistered){
|
|
// // SceneManager.LoadScene("MinigameTutorial");
|
|
// LoadingScreen.instance.LoadLevel("MinigameTutorial");
|
|
|
|
// // SceneManager.LoadScene("GameScene");
|
|
// }else{
|
|
// // SceneManager.LoadScene("GameScene");
|
|
// // LoadingScreen.instance.LoadLevel("GameScene");
|
|
|
|
|
|
// }
|
|
}else{
|
|
statusTxt.text = "Failed!";
|
|
|
|
MessageDialog.instance.ShowQuestion("Error",
|
|
"Failed to log you in using saved credentials.\nDo you want to open game with guest account?",OnYes:Register, OnNo:OnLoginFailed);
|
|
Feedbacks.Send("Login Failed", $"Bad credentials, username:{savedUsername}, password:{savedPassword}, isGoogle:{googleSigned}", www.text, CustomLogger.Debug.loggedText);
|
|
|
|
}
|
|
}
|
|
|
|
void OnLoginFailed(){
|
|
MessageDialog.instance.ShowQuestion("Sorry", "Looks like there's been an issue logging you in\nPress yes to close the app and Try again later.", OnYes:()=>{Application.Quit();},onlyYes:true,OnNo:()=>{});
|
|
}
|
|
|
|
public void OnGoogleSignComplete(){
|
|
StartCoroutine(registerGoogle(GoogleLoginManager.loggedEmail));
|
|
}
|
|
|
|
public void OnUsernameInputChanged(string newValue){
|
|
btn_login.interactable= (usernameInput.text.Length > 2);
|
|
usernameWarning.SetActive(usernameInput.text.Length< 2);
|
|
}
|
|
|
|
void OnRegisterButton(){
|
|
newUsernamePanel.SetActive(false);
|
|
Register();
|
|
}
|
|
}
|