From 5e408091cbc6aff6aa8b73f464d001e4655728c9 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 22:04:20 +0200 Subject: [PATCH 01/12] the fuck????? --- Client/Client.cs | 17 +- Client/ViewModels/ViewModelGame.cs | 63 +++- Server/Models/ServerClient.cs | 16 +- SharedClientServer/JSONConvert.cs | 475 +++++++++++++++-------------- 4 files changed, 314 insertions(+), 257 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 4044ec1..3dd5f1c 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -10,7 +10,7 @@ using static SharedClientServer.JSONConvert; namespace Client { public delegate void LobbyJoinCallback(bool isHost); - public delegate void CanvasDataReceived(double[] coordinates, Color color); + public delegate void CanvasDataReceived(double[][] coordinates, Color color); public delegate void CanvasReset(); public delegate void LobbyCallback(int id); class Client : ObservableObject @@ -58,7 +58,7 @@ namespace Client private void OnReadComplete(IAsyncResult ar) { int amountReceived = stream.EndRead(ar); - if (totalBufferReceived + amountReceived > 1024) + if (totalBufferReceived + amountReceived > 2048) { throw new OutOfMemoryException("buffer too small"); } @@ -83,7 +83,7 @@ namespace Client Debug.WriteLine($"reduced buffer: {expectedMessageLength}"); expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } - + ar.AsyncWaitHandle.WaitOne(); stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); } @@ -149,18 +149,21 @@ namespace Client case JSONConvert.CANVAS: // canvas data //clientData.CanvasData = JSONConvert.getCoordinates(payload); - CanvasInfo type = JSONConvert.GetCanvasMessageType(payload); + int type = JSONConvert.GetCanvasMessageType(payload); switch (type) { - case CanvasInfo.RESET: + case JSONConvert.CANVAS_RESET: CReset?.Invoke(); break; - case CanvasInfo.DRAWING: + case JSONConvert.CANVAS_WRITING: CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload)); + // we hebben gedrawed, dus stuur dat we weer kunnen drawen + break; } - break; + break; + default: Debug.WriteLine("[CLIENT] Received weird identifier: " + id); diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 9749198..a6bcf68 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -3,9 +3,11 @@ using GalaSoft.MvvmLight.Command; using SharedClientServer; using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; +using System.Timers; using System.Windows; using System.Windows.Input; using System.Windows.Media; @@ -19,7 +21,12 @@ namespace Client.ViewModels private ClientData data = ClientData.Instance; private GameWindow window; private Point currentPoint = new Point(); - private Color color; + public Color color; + public double[][] buffer; + public int pos = 0; + public int maxLines = 1; + public Queue linesQueue; + private Timer queueTimer; public static ObservableCollection Messages { get; } = new ObservableCollection(); @@ -59,6 +66,9 @@ namespace Client.ViewModels //_username = data.User.Username; //Messages.Add($"{data.User.Username}: {Message}"); } + + buffer = new double[maxLines][]; + linesQueue = new Queue(); OnKeyDown = new RelayCommand(ChatBox_KeyDown); ButtonStartGame = new RelayCommand(BeginGame); ButtonResetCanvas = new RelayCommand(CanvasResetLocal); @@ -72,6 +82,10 @@ namespace Client.ViewModels public void BeginGame() { + + queueTimer = new Timer(500); + queueTimer.Start(); + queueTimer.Elapsed += sendArrayFromQueue; data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID)); } @@ -79,7 +93,7 @@ namespace Client.ViewModels private void CanvasResetLocal() { this.window.CanvasForPaint.Children.Clear(); - data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CanvasInfo.RESET)); + data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CANVAS_RESET)); } @@ -109,9 +123,33 @@ namespace Client.ViewModels coordinates[2] = line.X2; coordinates[3] = line.Y2; currentPoint = e.GetPosition(window.CanvasForPaint); + buffer[pos] = coordinates; + pos++; window.CanvasForPaint.Children.Add(line); - data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(JSONConvert.CanvasInfo.DRAWING,coordinates, color)); + if (pos == maxLines) + { + double[][] temp = new double[maxLines][]; + for (int i = 0; i < maxLines; i++) + { + temp[i] = buffer[i]; + } + linesQueue.Enqueue(temp); + Array.Clear(buffer, 0, buffer.Length); + pos = 0; + } + + } + } + + private void sendArrayFromQueue(object sender, ElapsedEventArgs e) + { + + if (linesQueue.Count != 0) + { + Debug.WriteLine("[GAME] sending canvas data..."); + double[][] temp = linesQueue.Dequeue(); + data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.ConstructDrawingCanvasData(temp,color))); } } @@ -125,17 +163,20 @@ namespace Client.ViewModels color = colorSelected; } - private void UpdateCanvasWithNewData(double[] coordinates, Color color) + private void UpdateCanvasWithNewData(double[][] buffer, Color color) { Application.Current.Dispatcher.Invoke(delegate { - Line line = new Line(); - 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); + foreach (double[] arr in buffer) + { + Line line = new Line(); + line.Stroke = new SolidColorBrush(color); + line.X1 = arr[0]; + line.Y1 = arr[1]; + line.X2 = arr[2]; + line.Y2 = arr[3]; + this.window.CanvasForPaint.Children.Add(line); + } }); } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 3561b3a..8bfd142 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -50,7 +50,7 @@ namespace Server.Models { int bytesReceived = this.stream.EndRead(ar); - if (totalBufferReceived + bytesReceived > 1024) + if (totalBufferReceived + bytesReceived > 2048) { throw new OutOfMemoryException("buffer is too small!"); } @@ -149,21 +149,21 @@ namespace Server.Models break; case JSONConvert.CANVAS: - Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); - CanvasInfo typeToCheck = JSONConvert.GetCanvasMessageType(payload); + + int typeToCheck = JSONConvert.GetCanvasMessageType(payload); switch (typeToCheck) { - case CanvasInfo.DRAWING: + case JSONConvert.CANVAS_WRITING: dynamic canvasData = new { - type = JSONConvert.GetCanvasMessageType(payload), - coordinatesLine = JSONConvert.getCoordinates(payload), + canvasType = typeToCheck, + coords = JSONConvert.getCoordinates(payload), color = JSONConvert.getCanvasDrawingColor(payload) }; - serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData)); + serverCom.SendToLobby(serverCom.GetLobbyForUser(User),JSONConvert.GetMessageToSend(JSONConvert.CANVAS,canvasData)); break; - case CanvasInfo.RESET: + case JSONConvert.CANVAS_RESET: dynamic canvasDataForReset = new { type = JSONConvert.GetCanvasMessageType(payload) diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 0b92fb7..5c9ba1d 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -1,239 +1,252 @@ -using Client; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; +using Client; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; using System.Windows.Media; -namespace SharedClientServer -{ - class JSONConvert - { - public const byte LOGIN = 0x01; - public const byte MESSAGE = 0x02; - public const byte LOBBY = 0x03; - public const byte CANVAS = 0x04; - public const byte GAME = 0x05; - - public enum LobbyIdentifier - { - HOST, - JOIN, - JOIN_SUCCESS, - LEAVE, - LIST, - REQUEST - } - - public enum CanvasInfo +namespace SharedClientServer +{ + class JSONConvert + { + public const byte LOGIN = 0x01; + public const byte MESSAGE = 0x02; + public const byte LOBBY = 0x03; + public const byte CANVAS = 0x04; + public const byte GAME = 0x05; + + public const int CANVAS_WRITING = 0; + public const int CANVAS_RESET = 1; + + + public enum LobbyIdentifier { - DRAWING, - RESET - } - - public static (string,string) GetUsernameAndMessage(byte[] json) - { - string msg = Encoding.ASCII.GetString(json); - dynamic payload = JsonConvert.DeserializeObject(msg); - - return (payload.username, payload.message); - } - - public static string GetUsernameLogin(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - return payload.username; - } - - public static byte[] ConstructUsernameMessage(string uName) - { - return GetMessageToSend(LOGIN, new - { - username = uName - }); - } - - #region lobby messages - - public static byte[] ConstructLobbyHostMessage() - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.HOST - }); - } - - public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID) - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.HOST, - id = lobbyID - }) ; - } - - public static byte[] ConstructLobbyRequestMessage() - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.REQUEST - }); - } - - public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList) - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.LIST, - lobbies = lobbiesList - }); - } - - - public static byte[] ConstructLobbyJoinMessage(int lobbyID) - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.JOIN, - id = lobbyID - }); - } - - public static byte[] ConstructLobbyLeaveMessage(int lobbyID) - { - return GetMessageToSend(LOBBY, new - { - identifier = LobbyIdentifier.LEAVE, - id = lobbyID - }); - } - public static LobbyIdentifier GetLobbyIdentifier(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - return payload.identifier; - } - - public static Lobby[] GetLobbiesFromMessage(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - JArray lobbiesArray = payload.lobbies; - Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString()); - Lobby[] lobbiesTemp = lobbiesArray.ToObject(); - Debug.WriteLine("lobbies in array: "); - foreach (Lobby l in lobbiesTemp) - { - Debug.WriteLine("players: " + l.PlayersIn); - } - return lobbiesTemp; - } - - public static int GetLobbyID(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - return payload.id; - } - - public static Lobby GetLobby(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - JObject dynamicAsObject = payload.lobby; - return dynamicAsObject.ToObject(); - } - - public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost) - { - return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS, - host = isHost}); - } - - public static bool GetLobbyJoinIsHost(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - return payload.host; - } - - #endregion - - public static byte[] ConstructCanvasDataSend(CanvasInfo typeToSend, double[] coordinates, Color colorToSend) - { - return GetMessageToSend(CANVAS, new - { - type = typeToSend, - coordinatesLine = coordinates, - color = colorToSend - }); ; - } - - public static CanvasInfo GetCanvasMessageType(byte[] payload) + HOST, + JOIN, + JOIN_SUCCESS, + LEAVE, + LIST, + REQUEST + } + + public static (string,string) GetUsernameAndMessage(byte[] json) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); - CanvasInfo type = json.type; - return type; - } - - public static double[] getCoordinates(byte[] payload) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); - JArray coordinatesArray = json.coordinatesLine; - - double[] coordinates = coordinatesArray.ToObject(); - - return coordinates; - } - + string msg = Encoding.UTF8.GetString(json); + dynamic payload = JsonConvert.DeserializeObject(msg); + + return (payload.username, payload.message); + } + + public static string GetUsernameLogin(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + return payload.username; + } + + public static byte[] ConstructUsernameMessage(string uName) + { + return GetMessageToSend(LOGIN, new + { + username = uName + }); + } + + #region lobby messages + + public static byte[] ConstructLobbyHostMessage() + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.HOST + }); + } + + public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.HOST, + id = lobbyID + }) ; + } + + public static byte[] ConstructLobbyRequestMessage() + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.REQUEST + }); + } + + public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.LIST, + lobbies = lobbiesList + }); + } + + + public static byte[] ConstructLobbyJoinMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.JOIN, + id = lobbyID + }); + } + + public static byte[] ConstructLobbyLeaveMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.LEAVE, + id = lobbyID + }); + } + public static LobbyIdentifier GetLobbyIdentifier(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + return payload.identifier; + } + + public static Lobby[] GetLobbiesFromMessage(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + JArray lobbiesArray = payload.lobbies; + Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString()); + Lobby[] lobbiesTemp = lobbiesArray.ToObject(); + Debug.WriteLine("lobbies in array: "); + foreach (Lobby l in lobbiesTemp) + { + Debug.WriteLine("players: " + l.PlayersIn); + } + return lobbiesTemp; + } + + public static int GetLobbyID(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + return payload.id; + } + + public static Lobby GetLobby(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + JObject dynamicAsObject = payload.lobby; + return dynamicAsObject.ToObject(); + } + + public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost) + { + return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS, + host = isHost}); + } + + public static bool GetLobbyJoinIsHost(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + return payload.host; + } + + #endregion + + public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend) + { + + return GetMessageToSend(CANVAS, new + { + canvasType = typeToSend, + coords = buffer, + color = colorToSend + }); ; + } + + public static byte[] ConstructDrawingCanvasData(double[][] buffer, Color colorToSend) + { + return GetMessageToSend(CANVAS, new + { + canvasType = CANVAS_WRITING, + coords = buffer, + color = colorToSend + }); ; + } + + public static int GetCanvasMessageType(byte[] payload) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); + var temp = Convert.FromBase64String(json); + string fdasf = System.Text.Encoding.UTF8.GetString(temp); + int cType = temp.canvasType; + return cType; + } + + public static double[][] getCoordinates(byte[] payload) + { + Debug.WriteLine("got coords " + Encoding.UTF8.GetString(payload)); + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); + JArray coordinatesArray = json.coords; + + double[][] coordinates = coordinatesArray.ToObject(); + + return coordinates; + } + public static Color getCanvasDrawingColor(byte[] payload) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); Color color = json.color; return color; - } - - public static byte[] ConstructGameStartData(int lobbyID) - { - string startGame = "startGame"; - return GetMessageToSend(GAME, new - { - command = startGame, - lobbyToStart = lobbyID - }); ; - } - - public static string GetGameCommand(byte[] payload) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); - return json.command; - } - - public static int GetStartGameLobbyID(byte[] payload) - { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); - return json.lobbyToStart; - } - - /// - /// constructs a message that can be sent to the clients or server - /// - /// the identifier for what kind of message it is - /// the json payload - /// a byte array containing a message that can be sent to clients or server - public static byte[] GetMessageToSend(byte identifier, dynamic payload) - { - // convert the dynamic to bytes - byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload)); - // make the array that holds the message and copy the payload into it with the first spot containing the identifier - byte[] res = new byte[payloadBytes.Length + 5]; - // put the payload in the res array - Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length); - // put the identifier at the start of the payload part - res[4] = identifier; - // put the length of the payload at the start of the res array - Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); - return res; - } - - - } -} + } + + public static byte[] ConstructGameStartData(int lobbyID) + { + string startGame = "startGame"; + return GetMessageToSend(GAME, new + { + command = startGame, + lobbyToStart = lobbyID + }); ; + } + + public static string GetGameCommand(byte[] payload) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); + return json.command; + } + + public static int GetStartGameLobbyID(byte[] payload) + { + dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); + return json.lobbyToStart; + } + + /// + /// constructs a message that can be sent to the clients or server + /// + /// the identifier for what kind of message it is + /// the json payload + /// a byte array containing a message that can be sent to clients or server + public static byte[] GetMessageToSend(byte identifier, dynamic payload) + { + // convert the dynamic to bytes + string json = JsonConvert.SerializeObject(payload); + byte[] payloadBytes = Encoding.UTF8.GetBytes(json); + // make the array that holds the message and copy the payload into it with the first spot containing the identifier + byte[] res = new byte[payloadBytes.Length + 5]; + // put the payload in the res array + Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length); + // put the identifier at the start of the payload part + res[4] = identifier; + // put the length of the payload at the start of the res array + Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); + return res; + } + + + } +} From 8a490eb39981891b0a537174eb208ee0e30401fe Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 22:22:24 +0200 Subject: [PATCH 02/12] [FIX] AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA --- Client/ViewModels/ViewModelGame.cs | 6 +++--- SharedClientServer/JSONConvert.cs | 11 ++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index a6bcf68..b1b01e3 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -24,7 +24,7 @@ namespace Client.ViewModels public Color color; public double[][] buffer; public int pos = 0; - public int maxLines = 1; + public int maxLines = 50; public Queue linesQueue; private Timer queueTimer; @@ -83,7 +83,7 @@ namespace Client.ViewModels public void BeginGame() { - queueTimer = new Timer(500); + queueTimer = new Timer(50); queueTimer.Start(); queueTimer.Elapsed += sendArrayFromQueue; data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID)); @@ -149,7 +149,7 @@ namespace Client.ViewModels { Debug.WriteLine("[GAME] sending canvas data..."); double[][] temp = linesQueue.Dequeue(); - data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.ConstructDrawingCanvasData(temp,color))); + data.Client.SendMessage(JSONConvert.ConstructDrawingCanvasData(temp,color)); } } diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 5c9ba1d..b24e55f 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -173,16 +173,13 @@ namespace SharedClientServer canvasType = CANVAS_WRITING, coords = buffer, color = colorToSend - }); ; + }); } - public static int GetCanvasMessageType(byte[] payload) + public static int GetCanvasMessageType(byte[] json) { - dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload)); - var temp = Convert.FromBase64String(json); - string fdasf = System.Text.Encoding.UTF8.GetString(temp); - int cType = temp.canvasType; - return cType; + dynamic d = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); + return d.canvasType; } public static double[][] getCoordinates(byte[] payload) From 09f9e227b807fb71608fc700285750cc0ed39825 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 22 Oct 2020 22:35:29 +0200 Subject: [PATCH 03/12] merge stuff --- Client/ViewModels/ViewModelGame.cs | 32 ++++++++++++------------------ Client/Views/GameWindow.xaml | 2 -- Client/Views/GameWindow.xaml.cs | 10 ---------- SharedClientServer/JSONConvert.cs | 1 + 4 files changed, 14 insertions(+), 31 deletions(-) diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 976e24d..e010161 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -1,5 +1,5 @@ -using Client.Views; +using Client.Views; using GalaSoft.MvvmLight.Command; using SharedClientServer; using System; @@ -9,7 +9,6 @@ using System.ComponentModel; using System.Diagnostics; using System.Timers; using System.Windows; -using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; @@ -33,12 +32,6 @@ namespace Client.ViewModels private dynamic _payload; - public static string Word - { - get; - set; - } - public string _username; public string _message; @@ -54,6 +47,12 @@ namespace Client.ViewModels } } + public static string Word + { + get; + set; + } + public bool IsHost { get { return data.User.Host; } @@ -213,10 +212,6 @@ namespace Client.ViewModels data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); } - /* - * MISC make this a callback - * Handles the incoming chat message from another client. - */ public static void HandleIncomingMsg(string username, string message) { Application.Current.Dispatcher.Invoke(delegate @@ -224,21 +219,20 @@ namespace Client.ViewModels Messages.Add($"{username}: {message}"); }); } - public void LeaveGame(object sender, CancelEventArgs e) + public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e) { Debug.WriteLine("Leaving..."); data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID)); } - /* - * MISC make this a callback - * Handles the random word that has been received from the server. - */ public static void HandleRandomWord(string randomWord) { Debug.WriteLine("[CLIENT] Reached the handle random word method!"); - Word = "NegerPik"; + Application.Current.Dispatcher.Invoke(delegate + { + Word = randomWord; + }); } } } - + diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index ab2d3ef..94fe1e3 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -52,8 +52,6 @@