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 ///