148 lines
No EOL
4.1 KiB
C#
148 lines
No EOL
4.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Text.RegularExpressions;
|
|
using _Hub.Scripts.Network;
|
|
using _Network.Auth;
|
|
using _Network.Shared.Packets.ClientServer;
|
|
using UnityEngine;
|
|
|
|
namespace _Hub.Scripts.UI
|
|
{
|
|
public class LoginUI : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
|
|
private const int _maxUsernameLength = 15;
|
|
private const int _maxPasswordLength = 15;
|
|
[SerializeField] private AuthButton loginButton;
|
|
[SerializeField] private AuthInputField usernameInputField;
|
|
[SerializeField] private AuthInputField passwordInputField;
|
|
|
|
[SerializeField] private Transform loadingUI;
|
|
|
|
|
|
private string _username = String.Empty;
|
|
private string _password = String.Empty;
|
|
private Coroutine _loginCoroutine;
|
|
private bool _isConnected = false;
|
|
|
|
#endregion
|
|
|
|
#region Unity
|
|
|
|
private void OnEnable()
|
|
{
|
|
NetworkClient.OnServerConnected += OnServerConnected;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
NetworkClient.OnServerConnected -= OnServerConnected;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
AddListeners();
|
|
}
|
|
|
|
private void AddListeners()
|
|
{
|
|
loginButton.Button.onClick.AddListener(OnLoginClick);
|
|
usernameInputField.InputField.onValueChanged.AddListener(UpdateUsername);
|
|
passwordInputField.InputField.onValueChanged.AddListener(UpdatePassword);
|
|
}
|
|
|
|
private void OnLoginClick()
|
|
{
|
|
StopCoroutine(nameof(LoginRoutine));
|
|
StartCoroutine(nameof(LoginRoutine));
|
|
}
|
|
|
|
private IEnumerator LoginRoutine()
|
|
{
|
|
EnableLoginButton(false);
|
|
loadingUI.gameObject.SetActive(true);
|
|
NetworkClient.Instance.Connect();
|
|
while (!_isConnected)
|
|
{
|
|
Debug.Log("WAITING");
|
|
yield return null;
|
|
}
|
|
|
|
Debug.Log("Connected to server ehm");
|
|
var authRequest = new NetAuthRequest()
|
|
{
|
|
Username = _username,
|
|
Password = _password
|
|
};
|
|
|
|
NetworkClient.Instance.SendServer(authRequest);
|
|
}
|
|
|
|
private void UpdateUsername(string username)
|
|
{
|
|
_username = username;
|
|
Debug.Log("Username:" + username);
|
|
ValidateAndUpdateUI();
|
|
}
|
|
|
|
private void UpdatePassword(string password)
|
|
{
|
|
_password = password;
|
|
Debug.Log("Password:" + password);
|
|
ValidateAndUpdateUI();
|
|
}
|
|
|
|
private void ValidateAndUpdateUI()
|
|
{
|
|
var usernameRegex = Regex.Match(_username, "^[a-zA-Z0-9]+$");
|
|
var interactable = (!string.IsNullOrWhiteSpace(_username) && !string.IsNullOrWhiteSpace(_password) &&
|
|
(_maxUsernameLength >= _username.Length && _maxPasswordLength >= _password.Length) &&
|
|
usernameRegex.Success);
|
|
EnableLoginButton(interactable);
|
|
|
|
UpdateInputErrors(usernameRegex);
|
|
}
|
|
|
|
private void UpdateInputErrors(Match usernameRegex)
|
|
{
|
|
//Username
|
|
if (string.IsNullOrEmpty(_username))
|
|
{
|
|
usernameInputField.Error(false);
|
|
}
|
|
else
|
|
{
|
|
var usernameTooLong = _username.Length > _maxUsernameLength;
|
|
usernameInputField.Error(usernameTooLong || !usernameRegex.Success);
|
|
}
|
|
|
|
//Password
|
|
if (string.IsNullOrEmpty(_password))
|
|
{
|
|
passwordInputField.Error(false);
|
|
}
|
|
else
|
|
{
|
|
var passwordTooLong = _password.Length > _maxPasswordLength;
|
|
passwordInputField.Error(passwordTooLong);
|
|
}
|
|
}
|
|
|
|
private void EnableLoginButton(bool interactable)
|
|
{
|
|
loginButton.SetInteractable(interactable);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Callbacks
|
|
|
|
private void OnServerConnected()
|
|
{
|
|
_isConnected = true;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |