You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.5 KiB
177 lines
4.5 KiB
using Mono.Cecil.Cil;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class LoginManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject loading;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private GameObject formMain;
|
|
[SerializeField] private GameObject formLogin;
|
|
[SerializeField] private GameObject formReg;
|
|
//[SerializeField] private GameObject formLogin;
|
|
|
|
|
|
[Header("Login Data")]
|
|
[SerializeField] private RS_InputField _loginName;
|
|
[SerializeField] private RS_InputField _loginPassword;
|
|
[SerializeField] private RS_LabeledComponent _loginError;
|
|
|
|
[Header("Reg Data")]
|
|
[SerializeField] private RS_InputField _regLogin;
|
|
[SerializeField] private RS_InputField _regEmail;
|
|
[SerializeField] private RS_InputField _regPassword;
|
|
[SerializeField] private RS_InputField _regPassword2;
|
|
[SerializeField] private RS_LabeledComponent _regError;
|
|
|
|
[Header("Code")]
|
|
[SerializeField] private GameObject _panelCode;
|
|
[SerializeField] private RS_InputField _regCode;
|
|
[SerializeField] private RS_LabeledComponent _regCodeError;
|
|
|
|
|
|
private void ClearFormLogin()
|
|
{
|
|
_loginName.Clear();
|
|
_loginPassword.Clear();
|
|
}
|
|
|
|
private void ClearFormRegister()
|
|
{
|
|
_regLogin.Clear();
|
|
_regEmail.Clear();
|
|
_regPassword.Clear();
|
|
_regPassword2.Clear();
|
|
}
|
|
|
|
public void FormLogin()
|
|
{
|
|
formReg.SetActive(false);
|
|
formMain.SetActive(false);
|
|
formLogin.SetActive(true);
|
|
}
|
|
|
|
public void FormRegister()
|
|
{
|
|
formLogin.SetActive(false);
|
|
formMain.SetActive(false);
|
|
formReg.SetActive(true);
|
|
}
|
|
|
|
public void FormMain()
|
|
{
|
|
ClearFormLogin();
|
|
ClearFormRegister();
|
|
formLogin.SetActive(false);
|
|
formReg.SetActive(false);
|
|
formMain.SetActive(true);
|
|
}
|
|
|
|
private void ShowError(RS_LabeledComponent error, string text)
|
|
{
|
|
error.gameObject.SetActive(true);
|
|
error.SetLabel(text);
|
|
}
|
|
|
|
private void ShowRegError(string text) => ShowError(_regError, text);
|
|
private void ShowLoginError(string text) => ShowError(_loginError, text);
|
|
|
|
private void ShowLoading() => loading.SetActive(true);
|
|
private void HideLoading() => loading.SetActive(false);
|
|
|
|
public void Register()
|
|
{
|
|
User user = new User(
|
|
_regLogin.Text,
|
|
_regEmail.Text,
|
|
_regPassword.Text
|
|
);
|
|
|
|
if (user.IsAnyEmpty())
|
|
{
|
|
ShowRegError("Fill all fields!");
|
|
return;
|
|
}
|
|
|
|
if (_regPassword.Text != _regPassword2.Text)
|
|
{
|
|
ShowRegError("Passwords do not match!");
|
|
return;
|
|
}
|
|
|
|
ShowLoading();
|
|
WebRequest.Post(this, "RegUser", JsonUtility.ToJson(user), (UnityWebRequest request, string data) =>
|
|
{
|
|
HideLoading();
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
_panelCode.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
ShowRegError(request.error);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void CodeConfirm()
|
|
{
|
|
var code = new { code = _regCode.Text };
|
|
|
|
ShowLoading();
|
|
WebRequest.Post(this, "Code", JsonUtility.ToJson(code), (UnityWebRequest request, string data) =>
|
|
{
|
|
HideLoading();
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
_panelCode.SetActive(false);
|
|
FormLogin();
|
|
}
|
|
else
|
|
{
|
|
ShowError(_regCodeError, "Invalid Code");
|
|
}
|
|
});
|
|
}
|
|
|
|
public void Login()
|
|
{
|
|
var user = new
|
|
{
|
|
login = _loginName.Text,
|
|
password = _loginPassword.Text
|
|
};
|
|
|
|
ShowLoading();
|
|
WebRequest.Post(this, "Login", JsonUtility.ToJson(user), (UnityWebRequest request, string data) =>
|
|
{
|
|
HideLoading();
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
bool isFirst = true;
|
|
|
|
if (isFirst)
|
|
{
|
|
SceneManager.LoadScene("AccountInit");
|
|
}
|
|
else
|
|
{
|
|
SceneManager.LoadScene("Game");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ShowLoginError(request.error);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
|