From 8d4f72c09daff24d54f64e6a04b59aaff1ba56d4 Mon Sep 17 00:00:00 2001 From: Dogukan Date: Fri, 23 Oct 2020 12:32:11 +0200 Subject: [PATCH] [CHANGED] Static function for handling to callbacks --- Client/Client.cs | 8 ++++++-- Client/ViewModels/ViewModelGame.cs | 11 ++++++----- SharedClientServer/Lobby.cs | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 00357e0..2ff63c0 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -13,6 +13,8 @@ namespace Client public delegate void LobbyCallback(int id); public delegate void LobbyJoinCallback(bool isHost); public delegate void RandomWord(string word); + public delegate void HandleIncomingMsg(string username, string msg); + internal delegate void HandleIncomingPlayer(Lobby lobby); class Client : ObservableObject { private TcpClient tcpClient; @@ -32,6 +34,8 @@ namespace Client public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; public RandomWord RandomWord; + public HandleIncomingMsg IncomingMsg; + public HandleIncomingPlayer IncomingPlayer; private ClientData data = ClientData.Instance; public Lobby[] Lobbies { get; set; } @@ -124,7 +128,7 @@ namespace Client if(textUsername != data.User.Username) { - ViewModels.ViewModelGame.HandleIncomingMsg(textUsername, textMsg); + IncomingMsg?.Invoke(textUsername, textMsg); } //TODO display username and message in chat window @@ -160,7 +164,7 @@ namespace Client { Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count); if (item.ID == data.Lobby.ID) - ViewModels.ViewModelGame.HandleIncomingPlayer(item); + IncomingPlayer(item); } }; diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index d2848b2..0346a7e 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -24,8 +24,8 @@ namespace Client.ViewModels private Point currentPoint = new Point(); private Color color; - public static ObservableCollection Messages { get; } = new ObservableCollection(); - public static ObservableCollection Players { get; } = new ObservableCollection(); + public ObservableCollection Messages { get; } = new ObservableCollection(); + public ObservableCollection Players { get; } = new ObservableCollection(); private dynamic _payload; @@ -99,6 +99,8 @@ namespace Client.ViewModels { OnKeyDown = new RelayCommand(ChatBox_KeyDown); data.Client.RandomWord = HandleRandomWord; + data.Client.IncomingMsg = HandleIncomingMsg; + data.Client.IncomingPlayer = HandleIncomingPlayer; } private void ChatBox_KeyDown() @@ -125,7 +127,7 @@ namespace Client.ViewModels * MISC make this a callback * Handles the incoming chat message from another client. */ - public static void HandleIncomingMsg(string username, string message) + public void HandleIncomingMsg(string username, string message) { Application.Current.Dispatcher.Invoke(delegate { @@ -143,7 +145,6 @@ namespace Client.ViewModels } /* - * MISC make this a callback * Handles the random word that has been received from the server. */ public void HandleRandomWord(string randomWord) @@ -152,7 +153,7 @@ namespace Client.ViewModels Debug.WriteLine($"[CLIENT] The random word is: {_randomWord}"); } - public static void HandleIncomingPlayer(Lobby lobby) + public void HandleIncomingPlayer(Lobby lobby) { Application.Current.Dispatcher.Invoke(delegate { diff --git a/SharedClientServer/Lobby.cs b/SharedClientServer/Lobby.cs index 2ddf501..7e7b88c 100644 --- a/SharedClientServer/Lobby.cs +++ b/SharedClientServer/Lobby.cs @@ -6,7 +6,7 @@ using System.Text; namespace Client { - class Lobby : INotifyPropertyChanged + internal class Lobby : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged;