[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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public delegate void OnLobbyCreated(int id);
|
||||
|
||||
public delegate void CanvasDataReceived(double[] coordinates);
|
||||
class Client : ObservableObject
|
||||
public delegate void CanvasDataReceived(double[] coordinates);
|
||||
class Client : ObservableObject
|
||||
{
|
||||
|
||||
private ClientData clientData = ClientData.Instance;
|
||||
|
||||
private TcpClient tcpClient;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[2048];
|
||||
private byte[] totalBuffer = new byte[2048];
|
||||
private int totalBufferReceived = 0;
|
||||
public int Port = 5555;
|
||||
public bool Connected = false;
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public Callback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public OnLobbyCreated OnLobbyCreated;
|
||||
|
||||
public CanvasDataReceived CanvasDataReceived;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
public Client(string username)
|
||||
{
|
||||
this.username = username;
|
||||
this.tcpClient = new TcpClient();
|
||||
Debug.WriteLine("Starting connect to server");
|
||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
private void OnConnect(IAsyncResult ar)
|
||||
{
|
||||
Debug.Write("finished connecting to server");
|
||||
this.tcpClient.EndConnect(ar);
|
||||
this.stream = tcpClient.GetStream();
|
||||
OnSuccessfullConnect?.Invoke();
|
||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||
}
|
||||
|
||||
private void OnReadComplete(IAsyncResult ar)
|
||||
{
|
||||
int amountReceived = stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived > 2048)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
||||
totalBufferReceived += amountReceived;
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the complete packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// put the message received into the message array
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
|
||||
handleData(message);
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
}
|
||||
|
||||
private void handleData(byte[] message)
|
||||
{
|
||||
byte id = message[4];
|
||||
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
|
||||
switch (id)
|
||||
{
|
||||
case JSONConvert.LOGIN:
|
||||
// json log in username data
|
||||
break;
|
||||
case JSONConvert.MESSAGE:
|
||||
// json message data
|
||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
//TODO display username and message in chat window
|
||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
break;
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
// 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;
|
||||
}
|
||||
//TODO fill lobby with the data received
|
||||
break;
|
||||
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
|
||||
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SendMessage(byte[] message)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
||||
}
|
||||
|
||||
private void OnWriteComplete(IAsyncResult ar)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] finished writing");
|
||||
stream.EndWrite(ar);
|
||||
}
|
||||
}
|
||||
}
|
||||
private ClientData clientData = ClientData.Instance;
|
||||
|
||||
private TcpClient tcpClient;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[2048];
|
||||
private byte[] totalBuffer = new byte[2048];
|
||||
private int totalBufferReceived = 0;
|
||||
public int Port = 5555;
|
||||
public bool Connected = false;
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public Callback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public OnLobbyCreated OnLobbyCreated;
|
||||
|
||||
public CanvasDataReceived CanvasDataReceived;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
public Client(string username)
|
||||
{
|
||||
this.username = username;
|
||||
this.tcpClient = new TcpClient();
|
||||
Debug.WriteLine("Starting connect to server");
|
||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
private void OnConnect(IAsyncResult ar)
|
||||
{
|
||||
Debug.Write("finished connecting to server");
|
||||
this.tcpClient.EndConnect(ar);
|
||||
this.stream = tcpClient.GetStream();
|
||||
OnSuccessfullConnect?.Invoke();
|
||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||
}
|
||||
|
||||
private void OnReadComplete(IAsyncResult ar)
|
||||
{
|
||||
int amountReceived = stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived > 2048)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
||||
totalBufferReceived += amountReceived;
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the complete packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// put the message received into the message array
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
|
||||
handleData(message);
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
}
|
||||
|
||||
private void handleData(byte[] message)
|
||||
{
|
||||
byte id = message[4];
|
||||
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
|
||||
switch (id)
|
||||
{
|
||||
case JSONConvert.LOGIN:
|
||||
// json log in username data
|
||||
break;
|
||||
case JSONConvert.MESSAGE:
|
||||
// json message data
|
||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
//TODO display username and message in chat window
|
||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
break;
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
// 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;
|
||||
}
|
||||
//TODO fill lobby with the data received
|
||||
break;
|
||||
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
|
||||
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SendMessage(byte[] message)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
||||
}
|
||||
|
||||
private void OnWriteComplete(IAsyncResult ar)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] finished writing");
|
||||
stream.EndWrite(ar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,10 @@ using System.Collections.ObjectModel;
|
||||
using Client.Views;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
using System.Data;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
class ViewModel : INotifyPropertyChanged
|
||||
@@ -79,8 +82,16 @@ namespace Client
|
||||
{
|
||||
// lobby die je wilt joinen verwijderen
|
||||
// nieuwe binnengekregen lobby toevoegen
|
||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||
if (SelectedLobby != null)
|
||||
{
|
||||
if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoineble)
|
||||
{
|
||||
return;
|
||||
}
|
||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnLobbyJoinSuccess()
|
||||
@@ -168,5 +179,7 @@ namespace Client
|
||||
get { return _lobbies; }
|
||||
set { _lobbies = value; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,15 @@ namespace Client.ViewModels
|
||||
{
|
||||
class ViewModelGame : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
private ClientData data = ClientData.Instance;
|
||||
private GameWindow window;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private Point currentPoint = new Point();
|
||||
private Color color;
|
||||
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
private string _username;
|
||||
|
||||
private string _message;
|
||||
|
||||
public string Message
|
||||
{
|
||||
get
|
||||
@@ -39,7 +33,43 @@ namespace Client.ViewModels
|
||||
_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 ButtonStartGame { get; set; }
|
||||
|
||||
public void BeginGame()
|
||||
{
|
||||
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
|
||||
}
|
||||
|
||||
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
|
||||
{
|
||||
@@ -82,26 +112,6 @@ namespace Client.ViewModels
|
||||
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -35,9 +35,23 @@
|
||||
|
||||
</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">
|
||||
<Canvas Name="CanvasForPaint" MouseDown="CanvasForPaint_MouseDown" MouseMove="CanvasForPaint_MouseMove">
|
||||
|
||||
@@ -57,7 +57,8 @@
|
||||
<GridView x:Name="grdList">
|
||||
<GridViewColumn Header="Lobby ID" DisplayMemberBinding="{Binding ID}" 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>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
|
||||
Reference in New Issue
Block a user