logic
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace CheckersSpielBot
|
||||
{
|
||||
public partial class LoginWindow : Window
|
||||
{
|
||||
private readonly DatabaseService _db = new();
|
||||
private bool _isLoginMode = true;
|
||||
|
||||
public LoginWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
ApplyTabStyle();
|
||||
}
|
||||
|
||||
#region Tab switching
|
||||
private void SwitchToLogin(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_isLoginMode = true;
|
||||
ActionBtn.Content = "Login";
|
||||
ErrorText.Text = string.Empty;
|
||||
PwdBox.Clear();
|
||||
ApplyTabStyle();
|
||||
}
|
||||
|
||||
private void SwitchToRegister(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_isLoginMode = false;
|
||||
ActionBtn.Content = "Register";
|
||||
ErrorText.Text = string.Empty;
|
||||
PwdBox.Clear();
|
||||
ApplyTabStyle();
|
||||
}
|
||||
|
||||
private void ApplyTabStyle()
|
||||
{
|
||||
// Active tab — white text, red underline
|
||||
// Inactive tab — grey text, grey underline
|
||||
if (_isLoginMode)
|
||||
{
|
||||
TabLoginBtn.Foreground = Brushes.White;
|
||||
TabRegisterBtn.Foreground = new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88));
|
||||
}
|
||||
else
|
||||
{
|
||||
TabLoginBtn.Foreground = new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88));
|
||||
TabRegisterBtn.Foreground = Brushes.White;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
private void ActionBtn_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_isLoginMode) HandleLogin();
|
||||
else HandleRegister();
|
||||
}
|
||||
|
||||
private void HandleLogin()
|
||||
{
|
||||
string username = UsernameBox.Text.Trim();
|
||||
string password = PwdBox.Password;
|
||||
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
ShowError("Please enter username and password.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_db.LoginPlayer(username, password, out int playerId, out string error))
|
||||
{
|
||||
ShowError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerSession.PlayerId = playerId;
|
||||
PlayerSession.Username = username;
|
||||
|
||||
var mainWindow = new MainWindow();
|
||||
mainWindow.Show();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void HandleRegister()
|
||||
{
|
||||
string username = UsernameBox.Text.Trim();
|
||||
string password = PwdBox.Password;
|
||||
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
ShowError("Please enter username and password.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (username.Length < 3)
|
||||
{
|
||||
ShowError("Username must be at least 3 characters.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.Length < 6)
|
||||
{
|
||||
ShowError("Password must be at least 6 characters.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_db.RegisterPlayer(username, password, out string error))
|
||||
{
|
||||
ShowError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
ErrorText.Foreground = Brushes.LightGreen;
|
||||
ErrorText.Text = "Registered! You can now login.";
|
||||
SwitchToLogin(null!, null!);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
private void ShowError(string message)
|
||||
{
|
||||
ErrorText.Foreground = Brushes.OrangeRed;
|
||||
ErrorText.Text = message;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user