Compare commits
9 Commits
feature/se
...
feature/di
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c150bf3611 | ||
|
|
e8a72e164f | ||
|
|
754c1c5db9 | ||
|
|
8a90e74e74 | ||
|
|
be99c8d3f9 | ||
|
|
9e1ac07b80 | ||
|
|
cc5c71a30f | ||
|
|
442cdccc49 | ||
|
|
c339746a82 |
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
||||||
@@ -96,19 +104,6 @@ namespace Client
|
|||||||
Lobby[] lobbiesArr = client.Lobbies;
|
Lobby[] lobbiesArr = client.Lobbies;
|
||||||
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();
|
||||||
|
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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">
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using SharedClientServer;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using static SharedClientServer.JSONConvert;
|
using static SharedClientServer.JSONConvert;
|
||||||
@@ -45,48 +46,56 @@ namespace Server.Models
|
|||||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
int bytesReceived = this.stream.EndRead(ar);
|
|
||||||
|
|
||||||
if (totalBufferReceived + bytesReceived > 1024)
|
|
||||||
{
|
{
|
||||||
throw new OutOfMemoryException("buffer is too small!");
|
int bytesReceived = this.stream.EndRead(ar);
|
||||||
}
|
|
||||||
|
|
||||||
// copy the received bytes into the buffer
|
if (totalBufferReceived + bytesReceived > 1024)
|
||||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
|
||||||
// add the bytes we received to the total amount
|
|
||||||
totalBufferReceived += bytesReceived;
|
|
||||||
|
|
||||||
// calculate the expected length of the message
|
|
||||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
|
||||||
|
|
||||||
while (totalBufferReceived >= expectedMessageLength)
|
|
||||||
{
|
|
||||||
// we have received the full packet
|
|
||||||
byte[] message = new byte[expectedMessageLength];
|
|
||||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
|
||||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
|
||||||
HandleIncomingMessage(message);
|
|
||||||
|
|
||||||
// move the contents of the totalbuffer to the start of the array
|
|
||||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
|
||||||
|
|
||||||
// remove the length of the expected message from the total buffer
|
|
||||||
totalBufferReceived -= expectedMessageLength;
|
|
||||||
// and set the new expected length to the rest that is still in the buffer
|
|
||||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
|
||||||
|
|
||||||
if (expectedMessageLength == 0)
|
|
||||||
{
|
{
|
||||||
break;
|
throw new OutOfMemoryException("buffer is too small!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy the received bytes into the buffer
|
||||||
|
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||||
|
// add the bytes we received to the total amount
|
||||||
|
totalBufferReceived += bytesReceived;
|
||||||
|
|
||||||
|
// calculate the expected length of the message
|
||||||
|
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
|
|
||||||
|
while (totalBufferReceived >= expectedMessageLength)
|
||||||
|
{
|
||||||
|
// we have received the full packet
|
||||||
|
byte[] message = new byte[expectedMessageLength];
|
||||||
|
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||||
|
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||||
|
HandleIncomingMessage(message);
|
||||||
|
|
||||||
|
// move the contents of the totalbuffer to the start of the array
|
||||||
|
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||||
|
|
||||||
|
// remove the length of the expected message from the total buffer
|
||||||
|
totalBufferReceived -= expectedMessageLength;
|
||||||
|
// and set the new expected length to the rest that is still in the buffer
|
||||||
|
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
|
|
||||||
|
if (expectedMessageLength == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
// start reading for a new message
|
||||||
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||||
|
|
||||||
}
|
}
|
||||||
// start reading for a new message
|
catch (IOException e)
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
{
|
||||||
|
tcpClient.Close();
|
||||||
|
ServerCommunication.INSTANCE.ServerClientDisconnect(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +175,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace Server.Models
|
|||||||
public bool Started = false;
|
public bool Started = false;
|
||||||
public List<Lobby> lobbies;
|
public List<Lobby> lobbies;
|
||||||
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
||||||
|
internal Action DisconnectClientAction;
|
||||||
public Action newClientAction;
|
public Action newClientAction;
|
||||||
|
|
||||||
|
|
||||||
@@ -97,6 +98,26 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ServerClientDisconnect(ServerClient serverClient)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("[SERVERCOMM] handling disconnect");
|
||||||
|
DisconnectClientAction?.Invoke();
|
||||||
|
int id = -1;
|
||||||
|
foreach (Lobby l in serverClientsInlobbies.Keys)
|
||||||
|
{
|
||||||
|
if (serverClientsInlobbies[l].Contains(serverClient))
|
||||||
|
{
|
||||||
|
id = l.ID;
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id != -1)
|
||||||
|
{
|
||||||
|
LeaveLobby(serverClient.User, id);
|
||||||
|
SendToAllExcept(serverClient, JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendToAllExcept(string username, byte[] message)
|
public void SendToAllExcept(string username, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (ServerClient sc in serverClients)
|
foreach (ServerClient sc in serverClients)
|
||||||
@@ -105,6 +126,14 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendToAllExcept(ServerClient sc, byte[] message)
|
||||||
|
{
|
||||||
|
foreach (ServerClient s in serverClients)
|
||||||
|
{
|
||||||
|
if (s != sc) s.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendToLobby(Lobby lobby, byte[] message)
|
public void SendToLobby(Lobby lobby, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -161,6 +190,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 +213,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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ namespace Server.ViewModels
|
|||||||
{
|
{
|
||||||
InformationModel.ClientsConnected++;
|
InformationModel.ClientsConnected++;
|
||||||
};
|
};
|
||||||
|
serverCommunication.DisconnectClientAction = () =>
|
||||||
|
{
|
||||||
|
InformationModel.ClientsConnected--;
|
||||||
|
};
|
||||||
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
|
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
|
||||||
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
|
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
|
||||||
|
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
|||||||
Reference in New Issue
Block a user