[ADDED] the inlog screen and a data singleton for the client

This commit is contained in:
Lars
2020-10-20 16:36:34 +02:00
parent 55425bbec4
commit 84bf1b3e64
9 changed files with 142 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
using System;
using Client.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
@@ -17,9 +18,9 @@ namespace Client
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow startWindow = new MainWindow();
ViewModel VM = new ViewModel();
startWindow.DataContext = VM;
LoginScreen startWindow = new LoginScreen();
//ViewModel VM = new ViewModel();
//startWindow.DataContext = VM;
startWindow.Show();
}

59
Client/ClientData.cs Normal file
View File

@@ -0,0 +1,59 @@
using SharedClientServer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace Client
{
class ClientData
{
private static ClientData _instance;
private static readonly object padlock = new object();
public static ClientData Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new ClientData();
}
return _instance;
}
}
}
private User _user;
private Client _client;
private Lobby _lobby;
private ClientData()
{
}
public User User
{
get { return _user; }
set { _user = value; }
}
public Client Client
{
get { return _client; }
set { _client = value; }
}
public Lobby Lobby
{
get { return _lobby; }
set { _lobby = value; }
}
}
}

View File

@@ -18,7 +18,7 @@ namespace Client
ButtonCommand = new RelayCommand(() =>
{
Client client = new Client();
});
}, true);
_lobbies = new List<Lobby>();

View File

@@ -18,7 +18,7 @@ namespace Client.Views
/// </summary>
public partial class GameWindow : Window
{
ClientData data = ClientData.Instance;
public GameWindow()
{
DataContext = new ViewModelGame();
@@ -110,7 +110,7 @@ namespace Client.Views
*/
private void WriteToChat(string message)
{
string user = "Monkey";
string user = data.User.Username;
SentMessage.AppendText($"{user}: {message}\n");
}
}

View File

@@ -0,0 +1,29 @@
<Window x:Class="Client.Views.LoginScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client.Views"
mc:Ignorable="d"
Title="LoginScreen" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" FontSize="40" Content="Welcome to Scrubl.io"/>
<Label Grid.Row="1" FontSize="30" Content="Enter a username:"/>
<Label Grid.Row="2" FontSize="15" Content="(max amount of characters for the username is 10)"/>
<TextBox Name="usernameTextbox" Grid.Row="1" Grid.Column="1" MaxLength="10" FontSize="30" VerticalAlignment="Center" HorizontalAlignment="Left" Width="250"/>
<Button Content="ENTER" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Width="100" Height="40" Click="Button_EnterUsername"/>
</Grid>
</Window>

View File

@@ -0,0 +1,40 @@
using SharedClientServer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Client.Views
{
/// <summary>
/// Interaction logic for LoginScreen.xaml
/// </summary>
public partial class LoginScreen : Window
{
ClientData data = ClientData.Instance;
public LoginScreen()
{
InitializeComponent();
}
private void Button_EnterUsername(object sender, RoutedEventArgs e)
{
User user = new User(usernameTextbox.Text, 0, false);
Client client = new Client();
data.User = user;
data.Client = client;
MainWindow startWindow = new MainWindow();
startWindow.Show();
this.Close();
}
}
}

View File

@@ -21,9 +21,13 @@ namespace Client
/// </summary>
public partial class MainWindow : Window
{
ClientData data = ClientData.Instance;
public MainWindow()
{
this.DataContext = new ViewModel();
InitializeComponent();
usernameTextbox.Text = data.User.Username;
}
private void Button_Click(object sender, RoutedEventArgs e)

View File

@@ -132,7 +132,7 @@ namespace Server.Models
{
foreach(ServerClient sc in serverClients)
{
if (sc.Username == username)
if (sc.Username == user.Username)
{
serverClientsInlobbies[l].Add(sc);
break;

View File

@@ -41,7 +41,7 @@ namespace Client
succes = false;
if (_users.Count < _maxPlayers)
{
_users.Add(new User(username, 0));
_users.Add(new User(username, 0, false));
succes = true;
}
}