From eae05d17dfd778070dc30d3a59320fed19bc9862 Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 22 Oct 2020 17:30:40 +0200 Subject: [PATCH] [ADDED] Color can now we transfered, still need to get fixed with the buffer -_- --- Client/Client.cs | 30 ++++++++++++++++------- Client/ViewModels/ViewModel.cs | 2 +- Client/ViewModels/ViewModelGame.cs | 34 ++++++++++++++++---------- Client/Views/GameWindow.xaml | 2 +- Client/Views/MainWindow.xaml | 2 +- Server/Models/ServerClient.cs | 26 ++++++++++++++++---- Server/Models/ServerCommunication.cs | 17 ++++++++++++- SharedClientServer/JSONConvert.cs | 36 +++++++++++++++++++++++----- SharedClientServer/Lobby.cs | 10 ++++---- 9 files changed, 120 insertions(+), 39 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index fda6892..4044ec1 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -4,12 +4,14 @@ using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Text; +using System.Windows.Media; using static SharedClientServer.JSONConvert; namespace Client { public delegate void LobbyJoinCallback(bool isHost); - public delegate void CanvasDataReceived(double[] coordinates); + public delegate void CanvasDataReceived(double[] coordinates, Color color); + public delegate void CanvasReset(); public delegate void LobbyCallback(int id); class Client : ObservableObject { @@ -32,6 +34,7 @@ namespace Client public LobbyCallback OnLobbyLeave; private ClientData data = ClientData.Instance; public CanvasDataReceived CanvasDataReceived; + public CanvasReset CReset; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -55,13 +58,14 @@ namespace Client private void OnReadComplete(IAsyncResult ar) { int amountReceived = stream.EndRead(ar); - - if (totalBufferReceived > 2048) + if (totalBufferReceived + amountReceived > 1024) { throw new OutOfMemoryException("buffer too small"); - } - - Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); + } + + // copy the received bytes into the buffer + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); + // add the bytes we received to the total amount totalBufferReceived += amountReceived; int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); @@ -144,8 +148,18 @@ namespace Client case JSONConvert.CANVAS: // canvas data - //clientData.CanvasData = JSONConvert.getCoordinates(payload); - CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload)); + //clientData.CanvasData = JSONConvert.getCoordinates(payload); + CanvasInfo type = JSONConvert.GetCanvasMessageType(payload); + switch (type) + { + case CanvasInfo.RESET: + CReset?.Invoke(); + break; + + case CanvasInfo.DRAWING: + CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload)); + break; + } break; default: diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 829efda..fe9cb35 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -92,7 +92,7 @@ namespace Client // nieuwe binnengekregen lobby toevoegen if (SelectedLobby != null) { - if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoineble) + if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoinable) { return; } diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index d9b8eaf..9749198 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -2,6 +2,7 @@ using Client.Views; using GalaSoft.MvvmLight.Command; using SharedClientServer; +using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; @@ -39,13 +40,9 @@ namespace Client.ViewModels } } - public User User + public bool IsHost { - get { return data.User; } - set - { - data.User = value; - } + get { return data.User.Host; } } public ViewModelGame(GameWindow window) @@ -64,18 +61,28 @@ namespace Client.ViewModels } OnKeyDown = new RelayCommand(ChatBox_KeyDown); ButtonStartGame = new RelayCommand(BeginGame); + ButtonResetCanvas = new RelayCommand(CanvasResetLocal); data.Client.CanvasDataReceived = UpdateCanvasWithNewData; + data.Client.CReset = CanvasResetData; } - public ObservableCollection Messages { get; } = new ObservableCollection(); public ICommand OnKeyDown { get; set; } public ICommand ButtonStartGame { get; set; } + public ICommand ButtonResetCanvas { get; set; } public void BeginGame() { data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID)); } + + private void CanvasResetLocal() + { + this.window.CanvasForPaint.Children.Clear(); + data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CanvasInfo.RESET)); + } + + public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window) { if (e.ButtonState == MouseButtonState.Pressed) @@ -104,7 +111,7 @@ namespace Client.ViewModels currentPoint = e.GetPosition(window.CanvasForPaint); window.CanvasForPaint.Children.Add(line); - data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(coordinates)); + data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(JSONConvert.CanvasInfo.DRAWING,coordinates, color)); } } @@ -118,20 +125,23 @@ namespace Client.ViewModels color = colorSelected; } - private void UpdateCanvasWithNewData(double[] coordinates) + private void UpdateCanvasWithNewData(double[] coordinates, Color color) { Application.Current.Dispatcher.Invoke(delegate { Line line = new Line(); - line.Stroke = new SolidColorBrush(Colors.Black); + line.Stroke = new SolidColorBrush(color); line.X1 = coordinates[0]; line.Y1 = coordinates[1]; line.X2 = coordinates[2]; line.Y2 = coordinates[3]; - this.window.CanvasForPaint.Children.Add(line); }); - + } + + private void CanvasResetData() + { + this.window.CanvasForPaint.Children.Clear(); } private void ChatBox_KeyDown() diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index dbd544d..607b4a1 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -49,7 +49,7 @@