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.
169 lines
4.6 KiB
169 lines
4.6 KiB
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()
|
|
{
|
|
UserReg user = new UserReg(
|
|
_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
|
|
{
|
|
ErrorResult errorResult = JsonUtility.FromJson<ErrorResult>(data);
|
|
string message = string.IsNullOrEmpty(errorResult?.error) ? request.error : errorResult.error;
|
|
ShowRegError(message);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void CodeConfirm()
|
|
{
|
|
_panelCode.SetActive(false);
|
|
FormMain();
|
|
}
|
|
|
|
public void Login()
|
|
{
|
|
UserForLogin user = new UserForLogin()
|
|
{
|
|
login = _loginName.Text,
|
|
password = _loginPassword.Text
|
|
};
|
|
|
|
Debug.Log(JsonUtility.ToJson(user));
|
|
|
|
ShowLoading();
|
|
WebRequest.Post(this, "Login", JsonUtility.ToJson(user), (UnityWebRequest request, string data) =>
|
|
{
|
|
HideLoading();
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
UserLoginResult userLogin = JsonUtility.FromJson<UserLoginResult>(data);
|
|
LocalPlayerData.Set(userLogin);
|
|
|
|
if (userLogin.init)
|
|
{
|
|
SceneManager.LoadScene("Gameplay");
|
|
}
|
|
else
|
|
{
|
|
SceneManager.LoadScene("AccountInit");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ErrorResult errorResult = JsonUtility.FromJson<ErrorResult>(data);
|
|
string message = string.IsNullOrEmpty(errorResult?.error) ? request.error : errorResult.error;
|
|
ShowLoginError(message);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void QuickStart()
|
|
{
|
|
SceneManager.LoadScene("Gameplay");
|
|
|
|
}
|
|
}
|
|
|