[ADDED] lobby now secured when the game starts. start button is still for everyone available, not working yet
This commit is contained in:
308
Client/Client.cs
308
Client/Client.cs
@@ -1,158 +1,158 @@
|
|||||||
using SharedClientServer;
|
using SharedClientServer;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
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;
|
using static SharedClientServer.JSONConvert;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
public delegate void OnLobbyCreated(int id);
|
public delegate void OnLobbyCreated(int id);
|
||||||
|
|
||||||
public delegate void CanvasDataReceived(double[] coordinates);
|
public delegate void CanvasDataReceived(double[] coordinates);
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
|
|
||||||
private ClientData clientData = ClientData.Instance;
|
private ClientData clientData = ClientData.Instance;
|
||||||
|
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
private byte[] buffer = new byte[2048];
|
private byte[] buffer = new byte[2048];
|
||||||
private byte[] totalBuffer = new byte[2048];
|
private byte[] totalBuffer = new byte[2048];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public int Port = 5555;
|
public int Port = 5555;
|
||||||
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 OnLobbiesListReceived;
|
||||||
public Callback OnLobbyJoinSuccess;
|
public Callback OnLobbyJoinSuccess;
|
||||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||||
public OnLobbyCreated OnLobbyCreated;
|
public OnLobbyCreated OnLobbyCreated;
|
||||||
|
|
||||||
public CanvasDataReceived CanvasDataReceived;
|
public CanvasDataReceived CanvasDataReceived;
|
||||||
public Lobby[] Lobbies { get; set; }
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
{
|
{
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.tcpClient = new TcpClient();
|
this.tcpClient = new TcpClient();
|
||||||
Debug.WriteLine("Starting connect to server");
|
Debug.WriteLine("Starting connect to server");
|
||||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnConnect(IAsyncResult ar)
|
private void OnConnect(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
Debug.Write("finished connecting to server");
|
Debug.Write("finished connecting to server");
|
||||||
this.tcpClient.EndConnect(ar);
|
this.tcpClient.EndConnect(ar);
|
||||||
this.stream = tcpClient.GetStream();
|
this.stream = tcpClient.GetStream();
|
||||||
OnSuccessfullConnect?.Invoke();
|
OnSuccessfullConnect?.Invoke();
|
||||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnReadComplete(IAsyncResult ar)
|
private void OnReadComplete(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
int amountReceived = stream.EndRead(ar);
|
int amountReceived = stream.EndRead(ar);
|
||||||
|
|
||||||
if (totalBufferReceived > 2048)
|
if (totalBufferReceived > 2048)
|
||||||
{
|
{
|
||||||
throw new OutOfMemoryException("buffer too small");
|
throw new OutOfMemoryException("buffer too small");
|
||||||
}
|
}
|
||||||
|
|
||||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
||||||
totalBufferReceived += amountReceived;
|
totalBufferReceived += amountReceived;
|
||||||
|
|
||||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
|
|
||||||
while (totalBufferReceived >= expectedMessageLength)
|
while (totalBufferReceived >= expectedMessageLength)
|
||||||
{
|
{
|
||||||
// we have received the complete packet
|
// we have received the complete packet
|
||||||
byte[] message = new byte[expectedMessageLength];
|
byte[] message = new byte[expectedMessageLength];
|
||||||
// put the message received into the message array
|
// put the message received into the message array
|
||||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||||
|
|
||||||
handleData(message);
|
handleData(message);
|
||||||
|
|
||||||
totalBufferReceived -= expectedMessageLength;
|
totalBufferReceived -= expectedMessageLength;
|
||||||
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
||||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleData(byte[] message)
|
private void handleData(byte[] message)
|
||||||
{
|
{
|
||||||
byte id = message[4];
|
byte id = message[4];
|
||||||
|
|
||||||
byte[] payload = new byte[message.Length - 5];
|
byte[] payload = new byte[message.Length - 5];
|
||||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||||
|
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
{
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
// json log in username data
|
// json log in username data
|
||||||
break;
|
break;
|
||||||
case JSONConvert.MESSAGE:
|
case JSONConvert.MESSAGE:
|
||||||
// json message data
|
// json message data
|
||||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||||
string textUsername = combo.Item1;
|
string textUsername = combo.Item1;
|
||||||
string textMsg = combo.Item2;
|
string textMsg = combo.Item2;
|
||||||
|
|
||||||
//TODO display username and message in chat window
|
//TODO display username and message in chat window
|
||||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload);
|
LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload);
|
||||||
switch (lobbyIdentifier)
|
switch (lobbyIdentifier)
|
||||||
{
|
{
|
||||||
case LobbyIdentifier.LIST:
|
case LobbyIdentifier.LIST:
|
||||||
Debug.WriteLine("got lobbies list");
|
Debug.WriteLine("got lobbies list");
|
||||||
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
|
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
|
||||||
OnLobbiesListReceived?.Invoke();
|
OnLobbiesListReceived?.Invoke();
|
||||||
OnLobbiesReceivedAndWaitingForHost?.Invoke();
|
OnLobbiesReceivedAndWaitingForHost?.Invoke();
|
||||||
break;
|
break;
|
||||||
case LobbyIdentifier.HOST:
|
case LobbyIdentifier.HOST:
|
||||||
// we receive this when the server has made us a host of a new lobby
|
// we receive this when the server has made us a host of a new lobby
|
||||||
// TODO get lobby id
|
// TODO get lobby id
|
||||||
Debug.WriteLine("[CLIENT] got lobby object");
|
Debug.WriteLine("[CLIENT] got lobby object");
|
||||||
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
|
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
|
||||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||||
break;
|
break;
|
||||||
case LobbyIdentifier.JOIN_SUCCESS:
|
case LobbyIdentifier.JOIN_SUCCESS:
|
||||||
OnLobbyJoinSuccess?.Invoke();
|
OnLobbyJoinSuccess?.Invoke();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//TODO fill lobby with the data received
|
//TODO fill lobby with the data received
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
// canvas data
|
// canvas data
|
||||||
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
|
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
|
||||||
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
|
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(byte[] message)
|
public void SendMessage(byte[] message)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnWriteComplete(IAsyncResult ar)
|
private void OnWriteComplete(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[CLIENT] finished writing");
|
Debug.WriteLine("[CLIENT] finished writing");
|
||||||
stream.EndWrite(ar);
|
stream.EndWrite(ar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ using System.Collections.ObjectModel;
|
|||||||
using Client.Views;
|
using Client.Views;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
|
using System.Data;
|
||||||
|
using System.Windows.Controls.Primitives;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
class ViewModel : INotifyPropertyChanged
|
class ViewModel : INotifyPropertyChanged
|
||||||
@@ -79,8 +82,16 @@ namespace Client
|
|||||||
{
|
{
|
||||||
// lobby die je wilt joinen verwijderen
|
// lobby die je wilt joinen verwijderen
|
||||||
// nieuwe binnengekregen lobby toevoegen
|
// nieuwe binnengekregen lobby toevoegen
|
||||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
if (SelectedLobby != null)
|
||||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
{
|
||||||
|
if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoineble)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||||
|
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnLobbyJoinSuccess()
|
private void OnLobbyJoinSuccess()
|
||||||
@@ -168,5 +179,7 @@ namespace Client
|
|||||||
get { return _lobbies; }
|
get { return _lobbies; }
|
||||||
set { _lobbies = value; }
|
set { _lobbies = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,21 +13,15 @@ namespace Client.ViewModels
|
|||||||
{
|
{
|
||||||
class ViewModelGame : INotifyPropertyChanged
|
class ViewModelGame : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
private ClientData data = ClientData.Instance;
|
private ClientData data = ClientData.Instance;
|
||||||
private GameWindow window;
|
private GameWindow window;
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
|
||||||
|
|
||||||
private Point currentPoint = new Point();
|
private Point currentPoint = new Point();
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
|
||||||
|
|
||||||
private dynamic _payload;
|
private dynamic _payload;
|
||||||
|
|
||||||
private string _username;
|
private string _username;
|
||||||
|
|
||||||
private string _message;
|
private string _message;
|
||||||
|
|
||||||
public string Message
|
public string Message
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -39,7 +33,43 @@ namespace Client.ViewModels
|
|||||||
_message = value;
|
_message = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User User
|
||||||
|
{
|
||||||
|
get { return data.User; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
data.User = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ViewModelGame(GameWindow window)
|
||||||
|
{
|
||||||
|
this.window = window;
|
||||||
|
if (_payload == null)
|
||||||
|
{
|
||||||
|
_message = "";
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//_message = data.Message;
|
||||||
|
//_username = data.User.Username;
|
||||||
|
//Messages.Add($"{data.User.Username}: {Message}");
|
||||||
|
}
|
||||||
|
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
||||||
|
ButtonStartGame = new RelayCommand(BeginGame);
|
||||||
|
data.Client.CanvasDataReceived = UpdateCanvasWithNewData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||||
public ICommand OnKeyDown { get; set; }
|
public ICommand OnKeyDown { get; set; }
|
||||||
|
public ICommand ButtonStartGame { get; set; }
|
||||||
|
|
||||||
|
public void BeginGame()
|
||||||
|
{
|
||||||
|
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
|
||||||
|
}
|
||||||
|
|
||||||
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
|
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
|
||||||
{
|
{
|
||||||
@@ -82,26 +112,6 @@ namespace Client.ViewModels
|
|||||||
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
|
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
|
||||||
color = colorSelected;
|
color = colorSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ViewModelGame(GameWindow window)
|
|
||||||
{
|
|
||||||
this.window = window;
|
|
||||||
if (_payload == null)
|
|
||||||
{
|
|
||||||
_message = "";
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//_message = data.Message;
|
|
||||||
//_username = data.User.Username;
|
|
||||||
//Messages.Add($"{data.User.Username}: {Message}");
|
|
||||||
}
|
|
||||||
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
|
||||||
data.Client.CanvasDataReceived = UpdateCanvasWithNewData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void UpdateCanvasWithNewData(double[] coordinates)
|
private void UpdateCanvasWithNewData(double[] coordinates)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,9 +35,23 @@
|
|||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="2" Margin="84,10,10,10" Content="RESET"/>
|
<Grid Grid.Row="0" Grid.Column="1">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="160"/>
|
||||||
|
<ColumnDefinition Width="160"/>
|
||||||
|
<ColumnDefinition Width="*"/>
|
||||||
|
<ColumnDefinition Width="100"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" FontSize="20" Content="Pick a color -->"/>
|
||||||
|
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
|
||||||
|
|
||||||
|
<Label Grid.Row="0" Grid.Column="2" FontSize="20" Content="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
|
||||||
|
|
||||||
|
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Button Name="StartGame" Grid.Row="0" Grid.Column="2" Content="Start Game" FontSize="20" Command="{Binding ButtonStartGame}" IsEnabled="{Binding User.Host}"/>
|
||||||
|
|
||||||
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
|
|
||||||
|
|
||||||
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">
|
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">
|
||||||
<Canvas Name="CanvasForPaint" MouseDown="CanvasForPaint_MouseDown" MouseMove="CanvasForPaint_MouseMove">
|
<Canvas Name="CanvasForPaint" MouseDown="CanvasForPaint_MouseDown" MouseMove="CanvasForPaint_MouseMove">
|
||||||
|
|||||||
@@ -57,7 +57,8 @@
|
|||||||
<GridView x:Name="grdList">
|
<GridView x:Name="grdList">
|
||||||
<GridViewColumn Header="Lobby ID" DisplayMemberBinding="{Binding ID}" Width="70"/>
|
<GridViewColumn Header="Lobby ID" DisplayMemberBinding="{Binding ID}" Width="70"/>
|
||||||
<GridViewColumn Header="Players in" DisplayMemberBinding="{Binding PlayersIn}" Width="70"/>
|
<GridViewColumn Header="Players in" DisplayMemberBinding="{Binding PlayersIn}" Width="70"/>
|
||||||
<GridViewColumn Header="max players available" DisplayMemberBinding="{Binding MaxPlayers}"/>
|
<GridViewColumn Header="max players available" DisplayMemberBinding="{Binding MaxPlayers}" Width="150"/>
|
||||||
|
<GridViewColumn Header="joineble" DisplayMemberBinding="{Binding LobbyJoineble}"/>
|
||||||
</GridView>
|
</GridView>
|
||||||
</ListView.View>
|
</ListView.View>
|
||||||
</ListView>
|
</ListView>
|
||||||
|
|||||||
@@ -134,8 +134,9 @@ namespace Server.Models
|
|||||||
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
||||||
handleLobbyMessage(payload,l);
|
handleLobbyMessage(payload,l);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
|
Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
|
||||||
dynamic canvasData = new {
|
dynamic canvasData = new {
|
||||||
coordinatesLine = JSONConvert.getCoordinates(payload)
|
coordinatesLine = JSONConvert.getCoordinates(payload)
|
||||||
};
|
};
|
||||||
@@ -143,7 +144,22 @@ namespace Server.Models
|
|||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
|
case JSONConvert.GAME:
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic");
|
||||||
|
string command = JSONConvert.GetGameCommand(payload);
|
||||||
|
switch (command)
|
||||||
|
{
|
||||||
|
case "startGame":
|
||||||
|
int lobbyID = JSONConvert.GetStartGameLobbyID(payload);
|
||||||
|
serverCom.CloseALobby(lobbyID);
|
||||||
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -183,5 +183,16 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CloseALobby(int lobbyID)
|
||||||
|
{
|
||||||
|
foreach (Lobby lobby in lobbies)
|
||||||
|
{
|
||||||
|
if (lobby.ID == lobbyID)
|
||||||
|
{
|
||||||
|
lobby.LobbyJoineble = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,154 +1,155 @@
|
|||||||
using Client;
|
using Client;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
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.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
{
|
{
|
||||||
class JSONConvert
|
class JSONConvert
|
||||||
{
|
{
|
||||||
public const byte LOGIN = 0x01;
|
public const byte LOGIN = 0x01;
|
||||||
public const byte MESSAGE = 0x02;
|
public const byte MESSAGE = 0x02;
|
||||||
public const byte LOBBY = 0x03;
|
public const byte LOBBY = 0x03;
|
||||||
public const byte CANVAS = 0x04;
|
public const byte CANVAS = 0x04;
|
||||||
|
public const byte GAME = 0x05;
|
||||||
public enum LobbyIdentifier
|
|
||||||
{
|
public enum LobbyIdentifier
|
||||||
HOST,
|
{
|
||||||
JOIN,
|
HOST,
|
||||||
JOIN_SUCCESS,
|
JOIN,
|
||||||
LEAVE,
|
JOIN_SUCCESS,
|
||||||
LIST,
|
LEAVE,
|
||||||
REQUEST
|
LIST,
|
||||||
}
|
REQUEST
|
||||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
}
|
||||||
{
|
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||||
string msg = Encoding.ASCII.GetString(json);
|
{
|
||||||
dynamic payload = JsonConvert.DeserializeObject(msg);
|
string msg = Encoding.ASCII.GetString(json);
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(msg);
|
||||||
return (payload.username, payload.message);
|
|
||||||
}
|
return (payload.username, payload.message);
|
||||||
|
}
|
||||||
public static string GetUsernameLogin(byte[] json)
|
|
||||||
{
|
public static string GetUsernameLogin(byte[] json)
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
{
|
||||||
return payload.username;
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
}
|
return payload.username;
|
||||||
|
}
|
||||||
public static byte[] ConstructUsernameMessage(string uName)
|
|
||||||
{
|
public static byte[] ConstructUsernameMessage(string uName)
|
||||||
return GetMessageToSend(LOGIN, new
|
{
|
||||||
{
|
return GetMessageToSend(LOGIN, new
|
||||||
username = uName
|
{
|
||||||
});
|
username = uName
|
||||||
}
|
});
|
||||||
|
}
|
||||||
#region lobby messages
|
|
||||||
|
#region lobby messages
|
||||||
public static byte[] ConstructLobbyHostMessage()
|
|
||||||
{
|
public static byte[] ConstructLobbyHostMessage()
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.HOST
|
{
|
||||||
});
|
identifier = LobbyIdentifier.HOST
|
||||||
}
|
});
|
||||||
|
}
|
||||||
public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
|
|
||||||
{
|
public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.HOST,
|
{
|
||||||
id = lobbyID
|
identifier = LobbyIdentifier.HOST,
|
||||||
}) ;
|
id = lobbyID
|
||||||
}
|
}) ;
|
||||||
|
}
|
||||||
public static byte[] ConstructLobbyRequestMessage()
|
|
||||||
{
|
public static byte[] ConstructLobbyRequestMessage()
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.REQUEST
|
{
|
||||||
});
|
identifier = LobbyIdentifier.REQUEST
|
||||||
}
|
});
|
||||||
|
}
|
||||||
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
|
|
||||||
{
|
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.LIST,
|
{
|
||||||
lobbies = lobbiesList
|
identifier = LobbyIdentifier.LIST,
|
||||||
});
|
lobbies = lobbiesList
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
|
||||||
{
|
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.JOIN,
|
{
|
||||||
id = lobbyID
|
identifier = LobbyIdentifier.JOIN,
|
||||||
});
|
id = lobbyID
|
||||||
}
|
});
|
||||||
|
}
|
||||||
public static byte[] ConstructLobbyLeaveMessage(int lobbyID)
|
|
||||||
{
|
public static byte[] ConstructLobbyLeaveMessage(int lobbyID)
|
||||||
return GetMessageToSend(LOBBY, new
|
{
|
||||||
{
|
return GetMessageToSend(LOBBY, new
|
||||||
identifier = LobbyIdentifier.LEAVE,
|
{
|
||||||
id = lobbyID
|
identifier = LobbyIdentifier.LEAVE,
|
||||||
});
|
id = lobbyID
|
||||||
}
|
});
|
||||||
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
|
}
|
||||||
{
|
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
{
|
||||||
return payload.identifier;
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
}
|
return payload.identifier;
|
||||||
|
}
|
||||||
public static Lobby[] GetLobbiesFromMessage(byte[] json)
|
|
||||||
{
|
public static Lobby[] GetLobbiesFromMessage(byte[] json)
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
{
|
||||||
JArray lobbiesArray = payload.lobbies;
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString());
|
JArray lobbiesArray = payload.lobbies;
|
||||||
Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>();
|
Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString());
|
||||||
Debug.WriteLine("lobbies in array: ");
|
Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>();
|
||||||
foreach (Lobby l in lobbiesTemp)
|
Debug.WriteLine("lobbies in array: ");
|
||||||
{
|
foreach (Lobby l in lobbiesTemp)
|
||||||
Debug.WriteLine("players: " + l.PlayersIn);
|
{
|
||||||
}
|
Debug.WriteLine("players: " + l.PlayersIn);
|
||||||
return lobbiesTemp;
|
}
|
||||||
}
|
return lobbiesTemp;
|
||||||
|
}
|
||||||
public static int GetLobbyID(byte[] json)
|
|
||||||
{
|
public static int GetLobbyID(byte[] json)
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
{
|
||||||
return payload.id;
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
}
|
return payload.id;
|
||||||
|
}
|
||||||
public static Lobby GetLobby(byte[] json)
|
|
||||||
{
|
public static Lobby GetLobby(byte[] json)
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
{
|
||||||
JObject dynamicAsObject = payload.lobby;
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
return dynamicAsObject.ToObject<Lobby>();
|
JObject dynamicAsObject = payload.lobby;
|
||||||
}
|
return dynamicAsObject.ToObject<Lobby>();
|
||||||
|
}
|
||||||
public static byte[] ConstructLobbyJoinSuccessMessage()
|
|
||||||
{
|
public static byte[] ConstructLobbyJoinSuccessMessage()
|
||||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
{
|
||||||
}
|
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
||||||
|
}
|
||||||
#endregion
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
public static byte[] ConstructCanvasDataSend(double[] coordinates)
|
public static byte[] ConstructCanvasDataSend(double[] coordinates)
|
||||||
{
|
{
|
||||||
return GetMessageToSend(CANVAS, new
|
return GetMessageToSend(CANVAS, new
|
||||||
{
|
{
|
||||||
coordinatesLine = coordinates
|
coordinatesLine = coordinates
|
||||||
}); ;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] getCoordinates(byte[] payload)
|
public static double[] getCoordinates(byte[] payload)
|
||||||
{
|
{
|
||||||
dynamic payloadD = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
dynamic payloadD = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
||||||
@@ -157,29 +158,51 @@ namespace SharedClientServer
|
|||||||
double[] coordinates = coordinatesArray.ToObject<double[]>();
|
double[] coordinates = coordinatesArray.ToObject<double[]>();
|
||||||
|
|
||||||
return coordinates;
|
return coordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public static byte[] ConstructGameStartData(int lobbyID)
|
||||||
/// constructs a message that can be sent to the clients or server
|
{
|
||||||
/// </summary>
|
string startGame = "startGame";
|
||||||
/// <param name="identifier">the identifier for what kind of message it is</param>
|
return GetMessageToSend(GAME, new
|
||||||
/// <param name="payload">the json payload</param>
|
{
|
||||||
/// <returns>a byte array containing a message that can be sent to clients or server</returns>
|
command = startGame,
|
||||||
public static byte[] GetMessageToSend(byte identifier, dynamic payload)
|
lobbyToStart = lobbyID
|
||||||
{
|
}); ;
|
||||||
// convert the dynamic to bytes
|
}
|
||||||
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
|
|
||||||
// make the array that holds the message and copy the payload into it with the first spot containing the identifier
|
public static string GetGameCommand(byte[] payload)
|
||||||
byte[] res = new byte[payloadBytes.Length + 5];
|
{
|
||||||
// put the payload in the res array
|
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
||||||
Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length);
|
return json.command;
|
||||||
// put the identifier at the start of the payload part
|
}
|
||||||
res[4] = identifier;
|
|
||||||
// put the length of the payload at the start of the res array
|
public static int GetStartGameLobbyID(byte[] payload)
|
||||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
{
|
||||||
return res;
|
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
||||||
}
|
return json.lobbyToStart;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/// <summary>
|
||||||
}
|
/// constructs a message that can be sent to the clients or server
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifier">the identifier for what kind of message it is</param>
|
||||||
|
/// <param name="payload">the json payload</param>
|
||||||
|
/// <returns>a byte array containing a message that can be sent to clients or server</returns>
|
||||||
|
public static byte[] GetMessageToSend(byte identifier, dynamic payload)
|
||||||
|
{
|
||||||
|
// convert the dynamic to bytes
|
||||||
|
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
|
||||||
|
// make the array that holds the message and copy the payload into it with the first spot containing the identifier
|
||||||
|
byte[] res = new byte[payloadBytes.Length + 5];
|
||||||
|
// put the payload in the res array
|
||||||
|
Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length);
|
||||||
|
// put the identifier at the start of the payload part
|
||||||
|
res[4] = identifier;
|
||||||
|
// put the length of the payload at the start of the res array
|
||||||
|
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace Client
|
|||||||
private int _id;
|
private int _id;
|
||||||
private int _playersIn;
|
private int _playersIn;
|
||||||
private int _maxPlayers;
|
private int _maxPlayers;
|
||||||
|
private bool _lobbyJoineble;
|
||||||
//private List<string> _usernames;
|
//private List<string> _usernames;
|
||||||
private List<User> _users;
|
private List<User> _users;
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ namespace Client
|
|||||||
_maxPlayers = maxPlayers;
|
_maxPlayers = maxPlayers;
|
||||||
//_usernames = new List<string>();
|
//_usernames = new List<string>();
|
||||||
_users = new List<User>();
|
_users = new List<User>();
|
||||||
|
_lobbyJoineble = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddUser(string username, out bool succes)
|
public void AddUser(string username, out bool succes)
|
||||||
@@ -41,7 +43,7 @@ namespace Client
|
|||||||
succes = false;
|
succes = false;
|
||||||
if (_users.Count < _maxPlayers)
|
if (_users.Count < _maxPlayers)
|
||||||
{
|
{
|
||||||
_users.Add(new User(username, 0, false));
|
_users.Add(new User(username, 0, false, false));
|
||||||
succes = true;
|
succes = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,6 +89,11 @@ namespace Client
|
|||||||
set { _users = value; }
|
set { _users = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool LobbyJoineble
|
||||||
|
{
|
||||||
|
get { return _lobbyJoineble; }
|
||||||
|
set { _lobbyJoineble = value; }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,14 +10,16 @@ namespace SharedClientServer
|
|||||||
private string _username;
|
private string _username;
|
||||||
private int _score;
|
private int _score;
|
||||||
private bool _host;
|
private bool _host;
|
||||||
|
private bool _turnToDraw;
|
||||||
private string _message;
|
private string _message;
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public User(string username, int score, bool host)
|
public User(string username, int score, bool host, bool turnToDraw)
|
||||||
{
|
{
|
||||||
_username = username;
|
_username = username;
|
||||||
_score = score;
|
_score = score;
|
||||||
_host = host;
|
_host = host;
|
||||||
|
_turnToDraw = turnToDraw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(string username)
|
public User(string username)
|
||||||
@@ -25,6 +27,7 @@ namespace SharedClientServer
|
|||||||
_username = username;
|
_username = username;
|
||||||
_score = 0;
|
_score = 0;
|
||||||
_host = false;
|
_host = false;
|
||||||
|
_turnToDraw = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Username
|
public string Username
|
||||||
@@ -44,5 +47,11 @@ namespace SharedClientServer
|
|||||||
get { return _host; }
|
get { return _host; }
|
||||||
set { _host = value; }
|
set { _host = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool TurnToDraw
|
||||||
|
{
|
||||||
|
get { return _turnToDraw; }
|
||||||
|
set { _turnToDraw = value; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user