Instant login done
This commit is contained in:
113
Assets/Game/Scripts/NewLoginManager.cs
Normal file
113
Assets/Game/Scripts/NewLoginManager.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class NewLoginManager : MonoBehaviour
|
||||
{
|
||||
public GameObject loadingPanel;
|
||||
public GameObject newUsernamePanel;
|
||||
public GameObject usernameWarning;
|
||||
public TMP_InputField usernameInput;
|
||||
public Button btn_login;
|
||||
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()
|
||||
{
|
||||
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 >4){
|
||||
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);
|
||||
}
|
||||
|
||||
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{
|
||||
statusTxt.text = "Failed";
|
||||
MessageDialog.instance.ShowMessage("Failed!", "Failed to register you into the game.\nReason: " + id);
|
||||
newUsernamePanel.SetActive(true);
|
||||
usernameInput.text = "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Login(){
|
||||
StartCoroutine(login());
|
||||
}
|
||||
|
||||
IEnumerator login(){
|
||||
statusTxt.text = "Logging you in...";
|
||||
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", savedUsername);
|
||||
form.AddField("password", savedPassword);
|
||||
WWW www = new WWW(DBmanager.phpRoot + "login.php",form);
|
||||
|
||||
yield return www;
|
||||
Debug.Log(www.text);
|
||||
|
||||
if(DBmanager.Login(www.text)==0){
|
||||
SceneManager.LoadScene("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);
|
||||
}
|
||||
}
|
||||
|
||||
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 OnUsernameInputChanged(string newValue){
|
||||
btn_login.interactable= (usernameInput.text.Length > 4);
|
||||
usernameWarning.SetActive(usernameInput.text.Length<= 4);
|
||||
}
|
||||
|
||||
void OnRegisterButton(){
|
||||
newUsernamePanel.SetActive(false);
|
||||
Register();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user