From c339746a82ef7ce961458eda6e1341ed92a9877b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 21:05:04 +0200 Subject: [PATCH 1/6] [ADD] add closing method --- Client/Views/GameWindow.xaml | 1 + Client/Views/GameWindow.xaml.cs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index 6614197..7e8c71c 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -6,6 +6,7 @@ xmlns:local="clr-namespace:Client.Views" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" mc:Ignorable="d" + Closing="Window_Closing" Title="Scrubl.io" Height="600" Width="1200"> diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index 9d90635..55e8be0 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -58,5 +58,10 @@ namespace Client.Views { viewModel.Color_Picker(e, this); } + + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + + } } } -- 2.47.2 From 442cdccc49433895505c133144735e2aa96ed095 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 21:40:35 +0200 Subject: [PATCH 2/6] [ADD] add updating lobbies on exiting window --- Client/ViewModels/ViewModelGame.cs | 7 ++++++ Client/Views/GameWindow.xaml | 2 -- Client/Views/GameWindow.xaml.cs | 14 ++--------- Server/Models/ServerClient.cs | 5 ++++ Server/Models/ServerCommunication.cs | 36 ++++++++++++++++++++++++++++ SharedClientServer/JSONConvert.cs | 1 - 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 65fd353..d82df09 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -4,6 +4,7 @@ using GalaSoft.MvvmLight.Command; using SharedClientServer; using System.Collections.ObjectModel; using System.ComponentModel; +using System.Diagnostics; using System.Windows; using System.Windows.Input; using System.Windows.Media; @@ -120,6 +121,12 @@ namespace Client.ViewModels data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); } + public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e) + { + Debug.WriteLine("Leaving..."); + data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID)); + } + } } diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index 7e8c71c..1599e1f 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -3,10 +3,8 @@ 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" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" mc:Ignorable="d" - Closing="Window_Closing" Title="Scrubl.io" Height="600" Width="1200"> diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index 55e8be0..5947c52 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -1,15 +1,8 @@ using Client.ViewModels; -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 { @@ -24,6 +17,7 @@ namespace Client.Views { this.viewModel = new ViewModelGame(); DataContext = this.viewModel; + Closing += this.viewModel.LeaveGame; InitializeComponent(); } @@ -59,9 +53,5 @@ namespace Client.Views viewModel.Color_Picker(e, this); } - private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) - { - - } } } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 90eab73..c6090da 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -166,6 +166,11 @@ namespace Server.Models sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; + case LobbyIdentifier.LEAVE: + id = JSONConvert.GetLobbyID(payload); + ServerCommunication.INSTANCE.LeaveLobby(User, id); + ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); + break; } } diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 497f901..511f190 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -161,6 +161,31 @@ namespace Server.Models } } + public void RemoveFromLobby(Lobby lobby, User user) + { + foreach (Lobby l in lobbies) + { + if (l == lobby) + { + if (lobby.Users.Contains(user)) + { + Debug.WriteLine("[SERVERCOMM] removed user from lobby!"); + lobby.Users.Remove(user); + foreach (ServerClient sc in serverClients) + { + if (sc.User.Username == user.Username) + { + serverClientsInlobbies[l].Remove(sc); + break; + } + } + } + + } + break; + } + } + public int HostForLobby(User user) { Lobby lobby = new Lobby( lobbies.Count + 1,0, 8); @@ -183,5 +208,16 @@ namespace Server.Models } } } + + public void LeaveLobby(User user, int id) + { + foreach (Lobby l in lobbies) + { + if (l.ID == id) + { + RemoveFromLobby(l, user); + } + } + } } } diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index dd967e8..9c96f2d 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -139,7 +139,6 @@ namespace SharedClientServer } #endregion - /// /// constructs a message that can be sent to the clients or server /// -- 2.47.2 From cc5c71a30fd7c2ed0b5b946852a5cae9131a6a55 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 21:55:28 +0200 Subject: [PATCH 3/6] [ADD] added buttons reenabled when leaving --- Client/Client.cs | 9 +++++++-- Client/ViewModels/ViewModel.cs | 21 ++++++++------------- Server/Models/ServerClient.cs | 1 + 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 5346d92..88278ad 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert; namespace Client { - public delegate void OnLobbyCreated(int id); + public delegate void LobbyCallback(int id); class Client : ObservableObject { private TcpClient tcpClient; @@ -23,7 +23,8 @@ namespace Client public Callback OnLobbiesListReceived; public Callback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; - public OnLobbyCreated OnLobbyCreated; + public LobbyCallback OnLobbyCreated; + public LobbyCallback OnLobbyLeave; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -118,6 +119,10 @@ namespace Client case LobbyIdentifier.JOIN_SUCCESS: OnLobbyJoinSuccess?.Invoke(); break; + case LobbyIdentifier.LEAVE: + int lobbyLeaveID = JSONConvert.GetLobbyID(payload); + OnLobbyLeave?.Invoke(lobbyLeaveID); + break; } //TODO fill lobby with the data received break; diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index bbb9843..37d4ed9 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -34,6 +34,7 @@ namespace Client _lobbies = new ObservableCollection(); client = ClientData.Instance.Client; client.OnLobbiesListReceived = updateLobbies; + client.OnLobbyLeave = leaveLobby; OnHostButtonClick = new RelayCommand(hostGame); @@ -41,6 +42,13 @@ namespace Client JoinSelectedLobby = new RelayCommand(joinLobby, true); } + private void leaveLobby(int id) + { + _model.CanStartGame = true; + ClientData.Instance.Lobby = null; + SelectedLobby = null; + } + private void hostGame() { Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username); @@ -96,19 +104,6 @@ namespace Client Lobby[] lobbiesArr = client.Lobbies; Application.Current.Dispatcher.Invoke(delegate { - - //for (int i = 0; i < lobbiesArr.Length; i++) - //{ - // Lobby lobby = lobbiesArr[i]; - // Debug.WriteLine(lobby.PlayersIn); - // if (i < _lobbies.Count && _lobbies[i].ID == lobby.ID) - // { - // _lobbies[i].Set(lobby); - // } else - // { - // _lobbies.Add(lobbiesArr[i]); - // } - //} _lobbies.Clear(); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index c6090da..843db9c 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -169,6 +169,7 @@ namespace Server.Models case LobbyIdentifier.LEAVE: id = JSONConvert.GetLobbyID(payload); ServerCommunication.INSTANCE.LeaveLobby(User, id); + sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id)); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; } -- 2.47.2 From 9e1ac07b80f39250fc7337d668a97e63436d32eb Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:03:20 +0200 Subject: [PATCH 4/6] [ADD] added equals operators --- SharedClientServer/User.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/SharedClientServer/User.cs b/SharedClientServer/User.cs index e504418..4a1a36c 100644 --- a/SharedClientServer/User.cs +++ b/SharedClientServer/User.cs @@ -27,6 +27,30 @@ namespace SharedClientServer _host = false; } + public static bool operator ==(User u1, User u2) + { + return u1.Username == u2.Username; + } + + public static bool operator !=(User u1, User u2) + { + return u1.Username != u2.Username; + } + + public override bool Equals(object obj) + { + if ((obj == null) || !this.GetType().Equals(obj.GetType())) + { + return false; + } + else + { + User other = obj as User; + return other.Username == this.Username; + } + } + + public string Username { get { return _username; } -- 2.47.2 From be99c8d3f92e80527b3b230761c64d9dabf4aec2 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:18:37 +0200 Subject: [PATCH 5/6] [FIX] fixed equals operators --- SharedClientServer/User.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/SharedClientServer/User.cs b/SharedClientServer/User.cs index 4a1a36c..8d45dc2 100644 --- a/SharedClientServer/User.cs +++ b/SharedClientServer/User.cs @@ -1,11 +1,12 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace SharedClientServer { - class User + class User : IEquatable { private string _username; private int _score; @@ -29,12 +30,20 @@ namespace SharedClientServer public static bool operator ==(User u1, User u2) { - return u1.Username == u2.Username; + if (object.ReferenceEquals(u1, null)) + { + return object.ReferenceEquals(u2, null); + } + return u1.Equals(u2 as object); } public static bool operator !=(User u1, User u2) { - return u1.Username != u2.Username; + if (object.ReferenceEquals(u1, null)) + { + return object.ReferenceEquals(u2, null); + } + return u1.Equals(u2 as object); } public override bool Equals(object obj) @@ -45,11 +54,15 @@ namespace SharedClientServer } else { - User other = obj as User; - return other.Username == this.Username; + return this.Equals(obj as User); + } } - + + public bool Equals([AllowNull] User other) + { + return other.Username == this.Username; + } public string Username { -- 2.47.2 From 8a90e74e74f603739266f914ba528fd9484b7f28 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:32:56 +0200 Subject: [PATCH 6/6] [FIX] fixed leaving lobby --- Server/Models/ServerCommunication.cs | 50 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 511f190..c461aa1 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -161,30 +161,6 @@ namespace Server.Models } } - public void RemoveFromLobby(Lobby lobby, User user) - { - foreach (Lobby l in lobbies) - { - if (l == lobby) - { - if (lobby.Users.Contains(user)) - { - Debug.WriteLine("[SERVERCOMM] removed user from lobby!"); - lobby.Users.Remove(user); - foreach (ServerClient sc in serverClients) - { - if (sc.User.Username == user.Username) - { - serverClientsInlobbies[l].Remove(sc); - break; - } - } - } - - } - break; - } - } public int HostForLobby(User user) { @@ -211,11 +187,35 @@ namespace Server.Models public void LeaveLobby(User user, int id) { + Debug.WriteLine("[SERVERCOMM] removing user from lobby"); foreach (Lobby l in lobbies) { if (l.ID == id) { - RemoveFromLobby(l, user); + Debug.WriteLine($"[SERVERCOMM] checking for lobby with id {l.ID}"); + + foreach (User u in l.Users) + { + Debug.WriteLine($"[SERVERCOMM] checking if {u.Username} is {user.Username} "); + // contains doesn't work, so we'll do it like this... + if (u.Username == user.Username) + { + Debug.WriteLine("[SERVERCOMM] removed user from lobby!"); + l.Users.Remove(user); + foreach (ServerClient sc in serverClients) + { + if (sc.User.Username == user.Username) + { + serverClientsInlobbies[l].Remove(sc); + break; + } + } + break; + } + } + + + } } } -- 2.47.2