Compare commits
23 Commits
feature/br
...
feature/di
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c150bf3611 | ||
|
|
e8a72e164f | ||
|
|
754c1c5db9 | ||
|
|
8a90e74e74 | ||
|
|
be99c8d3f9 | ||
|
|
9e1ac07b80 | ||
|
|
cc5c71a30f | ||
|
|
442cdccc49 | ||
|
|
c339746a82 | ||
|
|
0f01354b72 | ||
|
|
61577e0df7 | ||
|
|
298bd760e5 | ||
|
|
c4c97a3fbe | ||
|
|
95448832c3 | ||
|
|
3a13314519 | ||
|
|
4f07eeb95a | ||
|
|
fc5d51a876 | ||
|
|
24701f8bb3 | ||
|
|
2aeca40aa4 | ||
|
|
76cd392525 | ||
|
|
7b30911cd7 | ||
|
|
671951c35b | ||
|
|
ba1b71d870 |
@@ -4,9 +4,11 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using static SharedClientServer.JSONConvert;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
|
public delegate void LobbyCallback(int id);
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
@@ -18,6 +20,12 @@ namespace Client
|
|||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
private string username;
|
private string username;
|
||||||
public Callback OnSuccessfullConnect;
|
public Callback OnSuccessfullConnect;
|
||||||
|
public Callback OnLobbiesListReceived;
|
||||||
|
public Callback OnLobbyJoinSuccess;
|
||||||
|
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||||
|
public LobbyCallback OnLobbyCreated;
|
||||||
|
public LobbyCallback OnLobbyLeave;
|
||||||
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
{
|
{
|
||||||
@@ -70,8 +78,10 @@ namespace Client
|
|||||||
private void handleData(byte[] message)
|
private void handleData(byte[] message)
|
||||||
{
|
{
|
||||||
byte id = message[4];
|
byte id = message[4];
|
||||||
byte[] payload = new byte[message.Length - 1];
|
|
||||||
Array.Copy(message, 1, payload, 0, message.Length - 1);
|
byte[] payload = new byte[message.Length - 5];
|
||||||
|
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||||
|
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
{
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
@@ -90,6 +100,30 @@ namespace Client
|
|||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
|
LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload);
|
||||||
|
switch (lobbyIdentifier)
|
||||||
|
{
|
||||||
|
case LobbyIdentifier.LIST:
|
||||||
|
Debug.WriteLine("got lobbies list");
|
||||||
|
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
|
||||||
|
OnLobbiesListReceived?.Invoke();
|
||||||
|
OnLobbiesReceivedAndWaitingForHost?.Invoke();
|
||||||
|
break;
|
||||||
|
case LobbyIdentifier.HOST:
|
||||||
|
// we receive this when the server has made us a host of a new lobby
|
||||||
|
// TODO get lobby id
|
||||||
|
Debug.WriteLine("[CLIENT] got lobby object");
|
||||||
|
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
|
||||||
|
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||||
|
break;
|
||||||
|
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
|
//TODO fill lobby with the data received
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ using System.Diagnostics;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using Client.Views;
|
using Client.Views;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Windows.Data;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
@@ -21,21 +23,96 @@ namespace Client
|
|||||||
|
|
||||||
public Lobby SelectedLobby { get; set; }
|
public Lobby SelectedLobby { get; set; }
|
||||||
|
|
||||||
|
private Client client;
|
||||||
|
|
||||||
|
private bool wantToBeHost = false;
|
||||||
|
private int wantToBeHostId = 0;
|
||||||
|
|
||||||
public ViewModel()
|
public ViewModel()
|
||||||
{
|
{
|
||||||
_model = new Model();
|
_model = new Model();
|
||||||
_lobbies = new ObservableCollection<Lobby>();
|
_lobbies = new ObservableCollection<Lobby>();
|
||||||
|
client = ClientData.Instance.Client;
|
||||||
|
client.OnLobbiesListReceived = updateLobbies;
|
||||||
|
client.OnLobbyLeave = leaveLobby;
|
||||||
|
|
||||||
_lobbies.Add(new Lobby(50, 3, 8));
|
|
||||||
_lobbies.Add(new Lobby(69, 1, 9));
|
|
||||||
_lobbies.Add(new Lobby(420, 7, 7));
|
|
||||||
|
|
||||||
OnHostButtonClick = new RelayCommand(() =>
|
OnHostButtonClick = new RelayCommand(hostGame);
|
||||||
|
|
||||||
|
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);
|
||||||
|
client.SendMessage(JSONConvert.ConstructLobbyHostMessage());
|
||||||
|
client.OnLobbyCreated = becomeHostForLobby;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void becomeHostForLobby(int id)
|
||||||
|
{
|
||||||
|
|
||||||
|
Debug.WriteLine($"got host succes with data {id} ");
|
||||||
|
wantToBeHost = true;
|
||||||
|
wantToBeHostId = id;
|
||||||
|
client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hostLobbiesReceived()
|
||||||
|
{
|
||||||
|
if (wantToBeHost)
|
||||||
|
foreach (Lobby l in Lobbies)
|
||||||
|
{
|
||||||
|
if (l.ID == wantToBeHostId)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("found lobby that we want to be host of: " + l.ID + ", joining..");
|
||||||
|
SelectedLobby = l;
|
||||||
|
startGameInLobby();
|
||||||
|
wantToBeHost = false;
|
||||||
|
client.OnLobbiesReceivedAndWaitingForHost = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void joinLobby()
|
||||||
|
{
|
||||||
|
// lobby die je wilt joinen verwijderen
|
||||||
|
// nieuwe binnengekregen lobby toevoegen
|
||||||
|
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||||
|
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnLobbyJoinSuccess()
|
||||||
|
{
|
||||||
|
startGameInLobby();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void updateLobbies()
|
||||||
|
{
|
||||||
|
Debug.WriteLine("updating lobbies...");
|
||||||
|
Lobby[] lobbiesArr = client.Lobbies;
|
||||||
|
Application.Current.Dispatcher.Invoke(delegate
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Host button clicked");
|
|
||||||
});
|
|
||||||
|
|
||||||
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
|
_lobbies.Clear();
|
||||||
|
|
||||||
|
foreach (Lobby l in lobbiesArr)
|
||||||
|
{
|
||||||
|
_lobbies.Add(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startGameInLobby()
|
private void startGameInLobby()
|
||||||
@@ -43,11 +120,18 @@ namespace Client
|
|||||||
if (SelectedLobby != null)
|
if (SelectedLobby != null)
|
||||||
{
|
{
|
||||||
ClientData.Instance.Lobby = SelectedLobby;
|
ClientData.Instance.Lobby = SelectedLobby;
|
||||||
|
startGameWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_model.CanStartGame = false;
|
private void startGameWindow()
|
||||||
|
{
|
||||||
|
_model.CanStartGame = false;
|
||||||
|
Application.Current.Dispatcher.Invoke(delegate
|
||||||
|
{
|
||||||
GameWindow window = new GameWindow();
|
GameWindow window = new GameWindow();
|
||||||
window.Show();
|
window.Show();
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClickCheck()
|
private void ClickCheck()
|
||||||
|
|||||||
@@ -1,16 +1,26 @@
|
|||||||
using GalaSoft.MvvmLight.Command;
|
|
||||||
|
using Client.Views;
|
||||||
|
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.Diagnostics;
|
||||||
|
using System.Windows;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
|
||||||
namespace Client.ViewModels
|
namespace Client.ViewModels
|
||||||
{
|
{
|
||||||
class ViewModelGame : INotifyPropertyChanged
|
class ViewModelGame : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
ClientData data = ClientData.Instance;
|
private ClientData data = ClientData.Instance;
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
|
private Point currentPoint = new Point();
|
||||||
|
private Color color;
|
||||||
|
|
||||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||||
|
|
||||||
private dynamic _payload;
|
private dynamic _payload;
|
||||||
@@ -31,6 +41,49 @@ namespace Client.ViewModels
|
|||||||
}
|
}
|
||||||
public ICommand OnKeyDown { get; set; }
|
public ICommand OnKeyDown { get; set; }
|
||||||
|
|
||||||
|
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
|
||||||
|
{
|
||||||
|
if (e.ButtonState == MouseButtonState.Pressed)
|
||||||
|
{
|
||||||
|
currentPoint = e.GetPosition(window.CanvasForPaint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Canvas_MouseMove(MouseEventArgs e, GameWindow window)
|
||||||
|
{
|
||||||
|
if (e.LeftButton == MouseButtonState.Pressed)
|
||||||
|
{
|
||||||
|
double[] coordinates = new double[4];
|
||||||
|
Line line = new Line();
|
||||||
|
|
||||||
|
line.Stroke = new SolidColorBrush(color);
|
||||||
|
//line.Stroke = SystemColors.WindowFrameBrush;
|
||||||
|
line.X1 = currentPoint.X;
|
||||||
|
line.Y1 = currentPoint.Y;
|
||||||
|
line.X2 = e.GetPosition(window.CanvasForPaint).X;
|
||||||
|
line.Y2 = e.GetPosition(window.CanvasForPaint).Y;
|
||||||
|
coordinates[0] = line.X1;
|
||||||
|
coordinates[1] = line.Y1;
|
||||||
|
coordinates[2] = line.X2;
|
||||||
|
coordinates[3] = line.Y2;
|
||||||
|
currentPoint = e.GetPosition(window.CanvasForPaint);
|
||||||
|
|
||||||
|
window.CanvasForPaint.Children.Add(line);
|
||||||
|
data.Client.SendMessage(JSONConvert.GetMessageToSend(0x04, coordinates));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Color_Picker(RoutedPropertyChangedEventArgs<Color?> e, GameWindow window)
|
||||||
|
{
|
||||||
|
Color colorSelected = new Color();
|
||||||
|
colorSelected.A = 255;
|
||||||
|
colorSelected.R = window.ClrPcker_Background.SelectedColor.Value.R;
|
||||||
|
colorSelected.G = window.ClrPcker_Background.SelectedColor.Value.G;
|
||||||
|
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
|
||||||
|
color = colorSelected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public ViewModelGame()
|
public ViewModelGame()
|
||||||
{
|
{
|
||||||
if (_payload == null)
|
if (_payload == null)
|
||||||
@@ -67,5 +120,14 @@ namespace Client.ViewModels
|
|||||||
//Broadcast the message after adding it to the list!
|
//Broadcast the message after adding it to the list!
|
||||||
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
|
||||||
{
|
{
|
||||||
@@ -18,55 +11,26 @@ namespace Client.Views
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class GameWindow : Window
|
public partial class GameWindow : Window
|
||||||
{
|
{
|
||||||
|
ClientData data = ClientData.Instance;
|
||||||
|
private ViewModelGame viewModel;
|
||||||
public GameWindow()
|
public GameWindow()
|
||||||
{
|
{
|
||||||
DataContext = new ViewModelGame();
|
this.viewModel = new ViewModelGame();
|
||||||
|
DataContext = this.viewModel;
|
||||||
|
Closing += this.viewModel.LeaveGame;
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
}
|
}
|
||||||
Point currentPoint = new Point();
|
|
||||||
|
|
||||||
private void CanvasForPaint_MouseDown(object sender, MouseButtonEventArgs e)
|
private void CanvasForPaint_MouseDown(object sender, MouseButtonEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.ButtonState == MouseButtonState.Pressed)
|
this.viewModel.Canvas_MouseDown(e, this);
|
||||||
{
|
|
||||||
currentPoint = e.GetPosition(CanvasForPaint);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CanvasForPaint_MouseMove(object sender, MouseEventArgs e)
|
private void CanvasForPaint_MouseMove(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.LeftButton == MouseButtonState.Pressed)
|
viewModel.Canvas_MouseMove(e, this);
|
||||||
{
|
|
||||||
Line line = new Line();
|
|
||||||
|
|
||||||
|
|
||||||
line.Stroke = new SolidColorBrush(color);
|
|
||||||
//line.Stroke = SystemColors.WindowFrameBrush;
|
|
||||||
line.X1 = currentPoint.X;
|
|
||||||
line.Y1 = currentPoint.Y;
|
|
||||||
line.X2 = e.GetPosition(CanvasForPaint).X;
|
|
||||||
line.Y2 = e.GetPosition(CanvasForPaint).Y;
|
|
||||||
|
|
||||||
currentPoint = e.GetPosition(CanvasForPaint);
|
|
||||||
|
|
||||||
CanvasForPaint.Children.Add(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color color;
|
|
||||||
|
|
||||||
private void ClrPcker_Background_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
|
|
||||||
{
|
|
||||||
Color colorSelected = new Color();
|
|
||||||
colorSelected.A = 255;
|
|
||||||
colorSelected.R = ClrPcker_Background.SelectedColor.Value.R;
|
|
||||||
colorSelected.G = ClrPcker_Background.SelectedColor.Value.G;
|
|
||||||
colorSelected.B = ClrPcker_Background.SelectedColor.Value.B;
|
|
||||||
color = colorSelected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CanvasReset_Click(object sender, RoutedEventArgs e)
|
private void CanvasReset_Click(object sender, RoutedEventArgs e)
|
||||||
@@ -86,22 +50,8 @@ namespace Client.Views
|
|||||||
|
|
||||||
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
|
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
|
||||||
{
|
{
|
||||||
Color colorSelected = new Color();
|
viewModel.Color_Picker(e, this);
|
||||||
colorSelected.A = 255;
|
|
||||||
colorSelected.R = ClrPcker_Background.SelectedColor.Value.R;
|
|
||||||
colorSelected.G = ClrPcker_Background.SelectedColor.Value.G;
|
|
||||||
colorSelected.B = ClrPcker_Background.SelectedColor.Value.B;
|
|
||||||
color = colorSelected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///*
|
|
||||||
// * Writes the current client's message to the chatbox.
|
|
||||||
// */
|
|
||||||
//private void WriteToChat(string message)
|
|
||||||
//{
|
|
||||||
// string user = data.User.Username;
|
|
||||||
// SentMessage.AppendText($"{user}: {message}\n");
|
|
||||||
// data.User.Message = message;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ namespace Client.Views
|
|||||||
Application.Current.Dispatcher.Invoke(delegate {
|
Application.Current.Dispatcher.Invoke(delegate {
|
||||||
data.User = user;
|
data.User = user;
|
||||||
data.Client = client;
|
data.Client = client;
|
||||||
|
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
|
||||||
MainWindow startWindow = new MainWindow();
|
MainWindow startWindow = new MainWindow();
|
||||||
startWindow.Show();
|
startWindow.Show();
|
||||||
this.Close();
|
this.Close();
|
||||||
|
|||||||
@@ -32,18 +32,6 @@ namespace Client
|
|||||||
|
|
||||||
private void Button_Click(object sender, RoutedEventArgs e)
|
private void Button_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
Lobby lobbySelected = LobbyList.SelectedItem as Lobby;
|
|
||||||
if(lobbySelected != null)
|
|
||||||
{
|
|
||||||
testLabel.Content = lobbySelected.ID;
|
|
||||||
colorSelection.IsEnabled = false;
|
|
||||||
joinButton.IsEnabled = false;
|
|
||||||
hostButton.IsEnabled = false;
|
|
||||||
|
|
||||||
GameWindow window = new GameWindow();
|
|
||||||
window.Show();
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Client;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using SharedClientServer;
|
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;
|
||||||
|
|
||||||
namespace Server.Models
|
namespace Server.Models
|
||||||
{
|
{
|
||||||
@@ -42,47 +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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,14 +134,17 @@ namespace Server.Models
|
|||||||
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||||
|
|
||||||
// todo handle sending to all except this user the username and message to display in chat
|
// todo handle sending to all except this user the username and message to display in chat
|
||||||
serverCom.SendToLobby(User.Lobby,payload);
|
serverCom.SendToLobby(ServerCommunication.INSTANCE.GetLobbyForUser(User),payload);
|
||||||
Debug.WriteLine("Payload has been sent!");
|
Debug.WriteLine("Payload has been sent!");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
|
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
||||||
|
handleLobbyMessage(payload,l);
|
||||||
break;
|
break;
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
|
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
|
||||||
// canvas data
|
// canvas data
|
||||||
// todo send canvas data to all other serverclients in lobby
|
// todo send canvas data to all other serverclients in lobby
|
||||||
break;
|
break;
|
||||||
@@ -136,7 +152,36 @@ namespace Server.Models
|
|||||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//TODO implement ways to handle the message
|
}
|
||||||
|
|
||||||
|
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
|
||||||
|
{
|
||||||
|
switch (l)
|
||||||
|
{
|
||||||
|
case LobbyIdentifier.REQUEST:
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies...");
|
||||||
|
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
|
break;
|
||||||
|
case LobbyIdentifier.HOST:
|
||||||
|
// add new lobby and add this serverclient to it
|
||||||
|
int createdLobbyID = ServerCommunication.INSTANCE.HostForLobby(this.User);
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] created lobby");
|
||||||
|
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(createdLobbyID));
|
||||||
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
|
break;
|
||||||
|
case LobbyIdentifier.JOIN:
|
||||||
|
int id = JSONConvert.GetLobbyID(payload);
|
||||||
|
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
|
||||||
|
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);
|
||||||
|
sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||||
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|
||||||
@@ -32,7 +33,10 @@ namespace Server.Models
|
|||||||
listener = new TcpListener(IPAddress.Any, port);
|
listener = new TcpListener(IPAddress.Any, port);
|
||||||
serverClients = new List<ServerClient>();
|
serverClients = new List<ServerClient>();
|
||||||
lobbies = new List<Lobby>();
|
lobbies = new List<Lobby>();
|
||||||
|
Lobby temp = new Lobby(1, 7, 8);
|
||||||
|
lobbies.Add(temp);
|
||||||
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
||||||
|
serverClientsInlobbies.Add(temp, new List<ServerClient>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -94,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)
|
||||||
@@ -102,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)
|
||||||
@@ -117,6 +149,18 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Lobby GetLobbyForUser(User user)
|
||||||
|
{
|
||||||
|
foreach (Lobby l in lobbies)
|
||||||
|
{
|
||||||
|
if (l.Users.Contains(user))
|
||||||
|
{
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void AddToLobby(Lobby lobby, User user)
|
public void AddToLobby(Lobby lobby, User user)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -125,6 +169,7 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
bool succ;
|
bool succ;
|
||||||
l.AddUser(user, out succ);
|
l.AddUser(user, out succ);
|
||||||
|
Debug.WriteLine("[SERVERCOMM] added user to lobby, now contains " + l.PlayersIn);
|
||||||
if (!succ)
|
if (!succ)
|
||||||
{
|
{
|
||||||
// TODO send lobby full message
|
// TODO send lobby full message
|
||||||
@@ -144,5 +189,64 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int HostForLobby(User user)
|
||||||
|
{
|
||||||
|
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
||||||
|
lobbies.Add(lobby);
|
||||||
|
serverClientsInlobbies.Add(lobby, new List<ServerClient>());
|
||||||
|
user.Host = true;
|
||||||
|
AddToLobby(lobby, user);
|
||||||
|
return lobby.ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void JoinLobby(User user, int id)
|
||||||
|
{
|
||||||
|
foreach (Lobby l in lobbies)
|
||||||
|
{
|
||||||
|
if (l.ID == id)
|
||||||
|
{
|
||||||
|
AddToLobby(l, user);
|
||||||
|
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using Client;
|
using Client;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
@@ -14,11 +16,13 @@ namespace SharedClientServer
|
|||||||
public const byte LOBBY = 0x03;
|
public const byte LOBBY = 0x03;
|
||||||
public const byte CANVAS = 0x04;
|
public const byte CANVAS = 0x04;
|
||||||
|
|
||||||
enum LobbyIdentifier
|
public enum LobbyIdentifier
|
||||||
{
|
{
|
||||||
HOST,
|
HOST,
|
||||||
ADD,
|
JOIN,
|
||||||
|
JOIN_SUCCESS,
|
||||||
LEAVE,
|
LEAVE,
|
||||||
|
LIST,
|
||||||
REQUEST
|
REQUEST
|
||||||
}
|
}
|
||||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||||
@@ -43,12 +47,98 @@ namespace SharedClientServer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] ConstructLobbyDataMessage(Lobby lobby)
|
#region lobby messages
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyHostMessage()
|
||||||
{
|
{
|
||||||
return null;
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.HOST
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.HOST,
|
||||||
|
id = lobbyID
|
||||||
|
}) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyRequestMessage()
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.REQUEST
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.LIST,
|
||||||
|
lobbies = lobbiesList
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.JOIN,
|
||||||
|
id = lobbyID
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyLeaveMessage(int lobbyID)
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new
|
||||||
|
{
|
||||||
|
identifier = LobbyIdentifier.LEAVE,
|
||||||
|
id = lobbyID
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
|
||||||
|
{
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
return payload.identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Lobby[] GetLobbiesFromMessage(byte[] json)
|
||||||
|
{
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
JArray lobbiesArray = payload.lobbies;
|
||||||
|
Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString());
|
||||||
|
Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>();
|
||||||
|
Debug.WriteLine("lobbies in array: ");
|
||||||
|
foreach (Lobby l in lobbiesTemp)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("players: " + l.PlayersIn);
|
||||||
|
}
|
||||||
|
return lobbiesTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetLobbyID(byte[] json)
|
||||||
|
{
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
return payload.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Lobby GetLobby(byte[] json)
|
||||||
|
{
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
JObject dynamicAsObject = payload.lobby;
|
||||||
|
return dynamicAsObject.ToObject<Lobby>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyJoinSuccessMessage()
|
||||||
|
{
|
||||||
|
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
||||||
|
}
|
||||||
|
|
||||||
|
#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>
|
||||||
@@ -69,5 +159,7 @@ namespace SharedClientServer
|
|||||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,16 +64,29 @@ namespace Client
|
|||||||
|
|
||||||
public int PlayersIn
|
public int PlayersIn
|
||||||
{
|
{
|
||||||
get { return _playersIn; }
|
get { return _users.Count; }
|
||||||
set { _playersIn = value; }
|
set { _playersIn = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Set(Lobby lobby)
|
||||||
|
{
|
||||||
|
this._id = lobby._id;
|
||||||
|
this._users = lobby._users;
|
||||||
|
this._maxPlayers = lobby._maxPlayers;
|
||||||
|
}
|
||||||
|
|
||||||
public int MaxPlayers
|
public int MaxPlayers
|
||||||
{
|
{
|
||||||
get { return _maxPlayers; }
|
get { return _maxPlayers; }
|
||||||
set { _maxPlayers = value; }
|
set { _maxPlayers = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<User> Users
|
||||||
|
{
|
||||||
|
get { return _users; }
|
||||||
|
set { _users = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
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;
|
||||||
private bool _host;
|
private bool _host;
|
||||||
private string _message;
|
private string _message;
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
public User(string username, int score, bool host)
|
public User(string username, int score, bool host)
|
||||||
{
|
{
|
||||||
_username = username;
|
_username = username;
|
||||||
@@ -25,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