198 lines
5.8 KiB
C#
198 lines
5.8 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using SessionZeroClient;
|
|
using SessionZeroClient.API;
|
|
|
|
public partial class AccountScreenLogic : Node
|
|
{
|
|
[ExportGroup("UI Elements")]
|
|
// Selection Screen
|
|
[ExportSubgroup("Selection Screen")]
|
|
[Export] private VBoxContainer SelectionScreenVboxContainer { get; set; }
|
|
[Export] private Button RegisterSelectionButton { get; set; }
|
|
[Export] private Button LoginSelectionButton { get; set; }
|
|
[Export] private LineEdit UrlLineEdit { get; set; }
|
|
|
|
// Register Screen
|
|
[ExportSubgroup("Register Screen")]
|
|
[Export] private VBoxContainer RegisterScreenVboxContainer { get; set; }
|
|
[Export] private LineEdit RegisterUsernameLineEdit { get; set; }
|
|
[Export] private LineEdit RegisterEmailLineEdit { get; set; }
|
|
[Export] private LineEdit RegisterPasswordLineEdit { get; set; }
|
|
[Export] private Button RegisterButton { get; set; }
|
|
|
|
// Login Screen
|
|
[ExportSubgroup("Login Screen")]
|
|
[Export] private VBoxContainer LoginScreenVboxContainer { get; set; }
|
|
[Export] private LineEdit LoginUsernameLineEdit { get; set; }
|
|
[Export] private LineEdit LoginPasswordLineEdit { get; set; }
|
|
[Export] private Button LoginButton { get; set; }
|
|
|
|
// Account Screen
|
|
[ExportSubgroup("Account Screen")]
|
|
[Export] private VBoxContainer AccountScreenVboxContainer { get; set; }
|
|
[Export] private Label AccountScreenGreetingLabel { get; set; }
|
|
[Export] private Button LogoutButton { get; set; }
|
|
|
|
// Other
|
|
[ExportSubgroup("Other")]
|
|
[Export] Button BackButton { get; set; }
|
|
|
|
|
|
private IApiHandler _apiHandler;
|
|
private AccountManager _accountManager;
|
|
private string _url = "";
|
|
|
|
public void Init(IApiHandler apiHandler, AccountManager accountManager)
|
|
{
|
|
_apiHandler = apiHandler;
|
|
_accountManager = accountManager;
|
|
SetupSignals();
|
|
InitialState();
|
|
}
|
|
|
|
private void InitialState()
|
|
{
|
|
SelectionScreenVboxContainer.Visible = true;
|
|
RegisterScreenVboxContainer.Visible = false;
|
|
LoginScreenVboxContainer.Visible = false;
|
|
AccountScreenVboxContainer.Visible = false;
|
|
BackButton.Visible = false;
|
|
}
|
|
|
|
private void RegisterState()
|
|
{
|
|
SelectionScreenVboxContainer.Visible = false;
|
|
RegisterScreenVboxContainer.Visible = true;
|
|
LoginScreenVboxContainer.Visible = false;
|
|
AccountScreenVboxContainer.Visible = false;
|
|
BackButton.Visible = true;
|
|
}
|
|
|
|
private void LoginState()
|
|
{
|
|
SelectionScreenVboxContainer.Visible = false;
|
|
RegisterScreenVboxContainer.Visible = false;
|
|
LoginScreenVboxContainer.Visible = true;
|
|
AccountScreenVboxContainer.Visible = false;
|
|
BackButton.Visible = true;
|
|
}
|
|
|
|
private void AccountSuccessState()
|
|
{
|
|
SelectionScreenVboxContainer.Visible = false;
|
|
RegisterScreenVboxContainer.Visible = false;
|
|
LoginScreenVboxContainer.Visible = false;
|
|
AccountScreenVboxContainer.Visible = true;
|
|
BackButton.Visible = false;
|
|
|
|
AccountScreenGreetingLabel.Text = $"Welcome, {_accountManager.Username}!";
|
|
}
|
|
|
|
private void SetupSignals()
|
|
{
|
|
// Selection Screen
|
|
RegisterSelectionButton.Pressed += RegisterSelectionButtonPressed;
|
|
LoginSelectionButton.Pressed += LoginSelectionButtonPressed;
|
|
|
|
// Register Screen
|
|
RegisterButton.Pressed += RegisterButtonPressed;
|
|
|
|
// Login Screen
|
|
LoginButton.Pressed += LoginButtonPressed;
|
|
|
|
// Account Screen
|
|
LogoutButton.Pressed += Logout;
|
|
|
|
// Other
|
|
BackButton.Pressed += BackButtonPressed;
|
|
}
|
|
|
|
private async void RegisterSelectionButtonPressed()
|
|
{
|
|
bool isValidServer = await ValidateServerUrl(UrlLineEdit.Text);
|
|
if (isValidServer)
|
|
{
|
|
_url = UrlLineEdit.Text;
|
|
RegisterState();
|
|
}
|
|
}
|
|
|
|
private async void LoginSelectionButtonPressed()
|
|
{
|
|
bool isValidServer = await ValidateServerUrl(UrlLineEdit.Text);
|
|
if (isValidServer)
|
|
{
|
|
_url = UrlLineEdit.Text;
|
|
LoginState();
|
|
}
|
|
}
|
|
|
|
private async void RegisterButtonPressed()
|
|
{
|
|
var username = RegisterUsernameLineEdit.Text;
|
|
var email = RegisterEmailLineEdit.Text;
|
|
var password = RegisterPasswordLineEdit.Text;
|
|
|
|
var success = await _accountManager.Register(_url, username, email, password);
|
|
|
|
GD.Print($"Register success: {success}");
|
|
|
|
if (success)
|
|
{
|
|
var loginSuccess = await _accountManager.Login(_url, username, password);
|
|
|
|
GD.Print($"Login success: {loginSuccess}");
|
|
|
|
if (loginSuccess)
|
|
{
|
|
AccountSuccessState();
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void LoginButtonPressed()
|
|
{
|
|
var username = LoginUsernameLineEdit.Text;
|
|
var password = LoginPasswordLineEdit.Text;
|
|
var success = await _accountManager.Login(_url, username, password);
|
|
|
|
GD.Print($"Login success: {success}");
|
|
|
|
if (success)
|
|
{
|
|
AccountSuccessState();
|
|
}
|
|
}
|
|
|
|
private async Task<bool> ValidateServerUrl(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
return await _apiHandler.ValidateServerUrl(url);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GD.PrintErr($"Server validation error: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void Logout()
|
|
{
|
|
_accountManager.Logout();
|
|
InitialState();
|
|
}
|
|
|
|
private void BackButtonPressed()
|
|
{
|
|
InitialState();
|
|
}
|
|
}
|