diff --git a/Client/App.xaml.cs b/Client/App.xaml.cs index 30a075c..9f2324b 100644 --- a/Client/App.xaml.cs +++ b/Client/App.xaml.cs @@ -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(); } diff --git a/Client/Client.cs b/Client/Client.cs index 17551de..6ba9b16 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -16,11 +16,12 @@ namespace Client private int totalBufferReceived = 0; public int Port = 5555; public bool Connected = false; - //TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username) - public string Username { get; } + private string username; + public Callback OnSuccessfullConnect; - public Client() + public Client(string username) { + this.username = username; this.tcpClient = new TcpClient(); Debug.WriteLine("Starting connect to server"); tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null); @@ -31,6 +32,8 @@ namespace Client Debug.Write("finished connecting to server"); this.tcpClient.EndConnect(ar); this.stream = tcpClient.GetStream(); + OnSuccessfullConnect?.Invoke(); + SendMessage(JSONConvert.ConstructUsernameMessage(username)); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); } @@ -72,10 +75,10 @@ namespace Client Array.Copy(message, 1, payload, 0, message.Length - 1); switch (id) { - case 0x01: + case JSONConvert.LOGIN: // json log in username data break; - case 0x02: + case JSONConvert.MESSAGE: // json message data (string, string) combo = JSONConvert.GetUsernameAndMessage(payload); string textUsername = combo.Item1; @@ -84,11 +87,11 @@ namespace Client break; - case 0x03: + case JSONConvert.LOBBY: // lobby data //TODO fill lobby with the data received break; - case 0x04: + case JSONConvert.CANVAS: // canvas data break; default: @@ -100,11 +103,13 @@ namespace Client public void SendMessage(byte[] message) { + Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message)); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null); } private void OnWriteComplete(IAsyncResult ar) { + Debug.WriteLine("[CLIENT] finished writing"); stream.EndWrite(ar); } } diff --git a/Client/ClientData.cs b/Client/ClientData.cs new file mode 100644 index 0000000..9442e8d --- /dev/null +++ b/Client/ClientData.cs @@ -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; } + } + + } +} diff --git a/Client/Models/Model.cs b/Client/Models/Model.cs index 59b3577..9e50229 100644 --- a/Client/Models/Model.cs +++ b/Client/Models/Model.cs @@ -11,6 +11,7 @@ namespace Client private int _numbers; private bool _status; + private bool _canStartGame; //Test code public int Numbers @@ -37,11 +38,18 @@ namespace Client } } + public bool CanStartGame + { + get { return _canStartGame; } + set { _canStartGame = value; } + } + public Model() { _status = false; _numbers = 0; + _canStartGame = true; } } diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 241b520..be55690 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -5,6 +5,10 @@ using System.ComponentModel; using System.Text; using System.Windows.Input; using SharedClientServer; +using System.Diagnostics; +using System.Windows; +using System.Collections.ObjectModel; +using Client.Views; namespace Client { @@ -12,19 +16,38 @@ namespace Client { public event PropertyChangedEventHandler PropertyChanged; + public ICommand OnHostButtonClick { get; set; } + public ICommand JoinSelectedLobby { get; set; } + + public Lobby SelectedLobby { get; set; } + public ViewModel() { _model = new Model(); - ButtonCommand = new RelayCommand(() => - { - Client client = new Client(); - }); - - _lobbies = new List(); + _lobbies = new ObservableCollection(); _lobbies.Add(new Lobby(50, 3, 8)); _lobbies.Add(new Lobby(69, 1, 9)); _lobbies.Add(new Lobby(420, 7, 7)); + + OnHostButtonClick = new RelayCommand(() => + { + Debug.WriteLine("Host button clicked"); + }); + + JoinSelectedLobby = new RelayCommand(startGameInLobby, true); + } + + private void startGameInLobby() + { + if (SelectedLobby != null) + { + ClientData.Instance.Lobby = SelectedLobby; + + _model.CanStartGame = false; + GameWindow window = new GameWindow(); + window.Show(); + } } private void ClickCheck() @@ -35,17 +58,12 @@ namespace Client _model.Numbers = _model.Numbers + 5; } - public ICommand ButtonCommand { get; set; } - private Model _model; public Model Model { get { - if (_model == null) - _model = new Model(); - return _model; } @@ -55,8 +73,8 @@ namespace Client } } - private List _lobbies; - public List Lobbies + private ObservableCollection _lobbies; + public ObservableCollection Lobbies { get { return _lobbies; } set { _lobbies = value; } diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index 0b609ba..aacc67e 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -18,7 +18,7 @@ namespace Client.Views /// 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"); } } diff --git a/Client/Views/LoginScreen.xaml b/Client/Views/LoginScreen.xaml new file mode 100644 index 0000000..e011780 --- /dev/null +++ b/Client/Views/LoginScreen.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + +