6 Commits

Author SHA1 Message Date
Sem van der Hoeven
8a90e74e74 [FIX] fixed leaving lobby 2020-10-21 22:32:56 +02:00
Sem van der Hoeven
be99c8d3f9 [FIX] fixed equals operators 2020-10-21 22:18:37 +02:00
Sem van der Hoeven
9e1ac07b80 [ADD] added equals operators 2020-10-21 22:03:20 +02:00
Sem van der Hoeven
cc5c71a30f [ADD] added buttons reenabled when leaving 2020-10-21 21:55:28 +02:00
Sem van der Hoeven
442cdccc49 [ADD] add updating lobbies on exiting window 2020-10-21 21:40:35 +02:00
Sem van der Hoeven
c339746a82 [ADD] add closing method 2020-10-21 21:05:04 +02:00
9 changed files with 105 additions and 26 deletions

View File

@@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert;
namespace Client namespace Client
{ {
public delegate void OnLobbyCreated(int id); public delegate void LobbyCallback(int id);
class Client : ObservableObject class Client : ObservableObject
{ {
private TcpClient tcpClient; private TcpClient tcpClient;
@@ -23,7 +23,8 @@ namespace Client
public Callback OnLobbiesListReceived; public Callback OnLobbiesListReceived;
public Callback OnLobbyJoinSuccess; public Callback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost; public Callback OnLobbiesReceivedAndWaitingForHost;
public OnLobbyCreated OnLobbyCreated; public LobbyCallback OnLobbyCreated;
public LobbyCallback OnLobbyLeave;
public Lobby[] Lobbies { get; set; } public Lobby[] Lobbies { get; set; }
public Client(string username) public Client(string username)
@@ -118,6 +119,10 @@ namespace Client
case LobbyIdentifier.JOIN_SUCCESS: case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke(); OnLobbyJoinSuccess?.Invoke();
break; break;
case LobbyIdentifier.LEAVE:
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
OnLobbyLeave?.Invoke(lobbyLeaveID);
break;
} }
//TODO fill lobby with the data received //TODO fill lobby with the data received
break; break;

View File

@@ -34,6 +34,7 @@ namespace Client
_lobbies = new ObservableCollection<Lobby>(); _lobbies = new ObservableCollection<Lobby>();
client = ClientData.Instance.Client; client = ClientData.Instance.Client;
client.OnLobbiesListReceived = updateLobbies; client.OnLobbiesListReceived = updateLobbies;
client.OnLobbyLeave = leaveLobby;
OnHostButtonClick = new RelayCommand(hostGame); OnHostButtonClick = new RelayCommand(hostGame);
@@ -41,6 +42,13 @@ namespace Client
JoinSelectedLobby = new RelayCommand(joinLobby, true); JoinSelectedLobby = new RelayCommand(joinLobby, true);
} }
private void leaveLobby(int id)
{
_model.CanStartGame = true;
ClientData.Instance.Lobby = null;
SelectedLobby = null;
}
private void hostGame() private void hostGame()
{ {
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username); Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
@@ -97,19 +105,6 @@ namespace Client
Application.Current.Dispatcher.Invoke(delegate 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(); _lobbies.Clear();
foreach (Lobby l in lobbiesArr) foreach (Lobby l in lobbiesArr)

View File

@@ -4,6 +4,7 @@ using GalaSoft.MvvmLight.Command;
using SharedClientServer; using SharedClientServer;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
@@ -120,6 +121,12 @@ namespace Client.ViewModels
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); 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));
}
} }
} }

View File

@@ -3,7 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client.Views"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d" mc:Ignorable="d"
Title="Scrubl.io" Height="600" Width="1200"> Title="Scrubl.io" Height="600" Width="1200">

View File

@@ -1,15 +1,8 @@
using Client.ViewModels; using Client.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Client.Views namespace Client.Views
{ {
@@ -24,6 +17,7 @@ namespace Client.Views
{ {
this.viewModel = new ViewModelGame(); this.viewModel = new ViewModelGame();
DataContext = this.viewModel; DataContext = this.viewModel;
Closing += this.viewModel.LeaveGame;
InitializeComponent(); InitializeComponent();
} }
@@ -58,5 +52,6 @@ namespace Client.Views
{ {
viewModel.Color_Picker(e, this); viewModel.Color_Picker(e, this);
} }
} }
} }

View File

@@ -166,6 +166,12 @@ namespace Server.Models
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break; break;
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;
} }
} }

View File

@@ -161,6 +161,7 @@ namespace Server.Models
} }
} }
public int HostForLobby(User user) public int HostForLobby(User user)
{ {
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8); Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
@@ -183,5 +184,40 @@ 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)
{
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;
}
}
}
}
}
} }
} }

View File

@@ -139,7 +139,6 @@ namespace SharedClientServer
} }
#endregion #endregion
/// <summary> /// <summary>
/// constructs a message that can be sent to the clients or server /// constructs a message that can be sent to the clients or server
/// </summary> /// </summary>

View File

@@ -1,11 +1,12 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text; using System.Text;
namespace SharedClientServer namespace SharedClientServer
{ {
class User class User : IEquatable<User>
{ {
private string _username; private string _username;
private int _score; private int _score;
@@ -27,6 +28,42 @@ namespace SharedClientServer
_host = false; _host = false;
} }
public static bool operator ==(User u1, User u2)
{
if (object.ReferenceEquals(u1, null))
{
return object.ReferenceEquals(u2, null);
}
return u1.Equals(u2 as object);
}
public static bool operator !=(User u1, User u2)
{
if (object.ReferenceEquals(u1, null))
{
return object.ReferenceEquals(u2, null);
}
return u1.Equals(u2 as object);
}
public override bool Equals(object obj)
{
if ((obj == null) || !this.GetType().Equals(obj.GetType()))
{
return false;
}
else
{
return this.Equals(obj as User);
}
}
public bool Equals([AllowNull] User other)
{
return other.Username == this.Username;
}
public string Username public string Username
{ {
get { return _username; } get { return _username; }