[ADDED] Color can now we transfered, still need to get fixed with the buffer -_-

This commit is contained in:
Lars
2020-10-22 17:30:40 +02:00
parent 1fdba622cc
commit eae05d17df
9 changed files with 120 additions and 39 deletions

View File

@@ -4,12 +4,14 @@ 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 System.Windows.Media;
using static SharedClientServer.JSONConvert; using static SharedClientServer.JSONConvert;
namespace Client namespace Client
{ {
public delegate void LobbyJoinCallback(bool isHost); public delegate void LobbyJoinCallback(bool isHost);
public delegate void CanvasDataReceived(double[] coordinates); public delegate void CanvasDataReceived(double[] coordinates, Color color);
public delegate void CanvasReset();
public delegate void LobbyCallback(int id); public delegate void LobbyCallback(int id);
class Client : ObservableObject class Client : ObservableObject
{ {
@@ -32,6 +34,7 @@ namespace Client
public LobbyCallback OnLobbyLeave; public LobbyCallback OnLobbyLeave;
private ClientData data = ClientData.Instance; private ClientData data = ClientData.Instance;
public CanvasDataReceived CanvasDataReceived; public CanvasDataReceived CanvasDataReceived;
public CanvasReset CReset;
public Lobby[] Lobbies { get; set; } public Lobby[] Lobbies { get; set; }
public Client(string username) public Client(string username)
@@ -55,13 +58,14 @@ namespace Client
private void OnReadComplete(IAsyncResult ar) private void OnReadComplete(IAsyncResult ar)
{ {
int amountReceived = stream.EndRead(ar); int amountReceived = stream.EndRead(ar);
if (totalBufferReceived + amountReceived > 1024)
if (totalBufferReceived > 2048)
{ {
throw new OutOfMemoryException("buffer too small"); throw new OutOfMemoryException("buffer too small");
} }
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); // copy the received bytes into the buffer
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
// add the bytes we received to the total amount
totalBufferReceived += amountReceived; totalBufferReceived += amountReceived;
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
@@ -144,8 +148,18 @@ namespace Client
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)); CanvasInfo type = JSONConvert.GetCanvasMessageType(payload);
switch (type)
{
case CanvasInfo.RESET:
CReset?.Invoke();
break;
case CanvasInfo.DRAWING:
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
break;
}
break; break;
default: default:

View File

@@ -92,7 +92,7 @@ namespace Client
// nieuwe binnengekregen lobby toevoegen // nieuwe binnengekregen lobby toevoegen
if (SelectedLobby != null) if (SelectedLobby != null)
{ {
if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoineble) if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoinable)
{ {
return; return;
} }

View File

@@ -2,6 +2,7 @@
using Client.Views; using Client.Views;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using SharedClientServer; using SharedClientServer;
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
@@ -39,13 +40,9 @@ namespace Client.ViewModels
} }
} }
public User User public bool IsHost
{ {
get { return data.User; } get { return data.User.Host; }
set
{
data.User = value;
}
} }
public ViewModelGame(GameWindow window) public ViewModelGame(GameWindow window)
@@ -64,18 +61,28 @@ namespace Client.ViewModels
} }
OnKeyDown = new RelayCommand(ChatBox_KeyDown); OnKeyDown = new RelayCommand(ChatBox_KeyDown);
ButtonStartGame = new RelayCommand(BeginGame); ButtonStartGame = new RelayCommand(BeginGame);
ButtonResetCanvas = new RelayCommand(CanvasResetLocal);
data.Client.CanvasDataReceived = UpdateCanvasWithNewData; data.Client.CanvasDataReceived = UpdateCanvasWithNewData;
data.Client.CReset = CanvasResetData;
} }
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
public ICommand OnKeyDown { get; set; } public ICommand OnKeyDown { get; set; }
public ICommand ButtonStartGame { get; set; } public ICommand ButtonStartGame { get; set; }
public ICommand ButtonResetCanvas { get; set; }
public void BeginGame() public void BeginGame()
{ {
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID)); data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
} }
private void CanvasResetLocal()
{
this.window.CanvasForPaint.Children.Clear();
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CanvasInfo.RESET));
}
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window) public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
{ {
if (e.ButtonState == MouseButtonState.Pressed) if (e.ButtonState == MouseButtonState.Pressed)
@@ -104,7 +111,7 @@ namespace Client.ViewModels
currentPoint = e.GetPosition(window.CanvasForPaint); currentPoint = e.GetPosition(window.CanvasForPaint);
window.CanvasForPaint.Children.Add(line); window.CanvasForPaint.Children.Add(line);
data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(coordinates)); data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(JSONConvert.CanvasInfo.DRAWING,coordinates, color));
} }
} }
@@ -118,20 +125,23 @@ namespace Client.ViewModels
color = colorSelected; color = colorSelected;
} }
private void UpdateCanvasWithNewData(double[] coordinates) private void UpdateCanvasWithNewData(double[] coordinates, Color color)
{ {
Application.Current.Dispatcher.Invoke(delegate Application.Current.Dispatcher.Invoke(delegate
{ {
Line line = new Line(); Line line = new Line();
line.Stroke = new SolidColorBrush(Colors.Black); line.Stroke = new SolidColorBrush(color);
line.X1 = coordinates[0]; line.X1 = coordinates[0];
line.Y1 = coordinates[1]; line.Y1 = coordinates[1];
line.X2 = coordinates[2]; line.X2 = coordinates[2];
line.Y2 = coordinates[3]; line.Y2 = coordinates[3];
this.window.CanvasForPaint.Children.Add(line); this.window.CanvasForPaint.Children.Add(line);
}); });
}
private void CanvasResetData()
{
this.window.CanvasForPaint.Children.Clear();
} }
private void ChatBox_KeyDown() private void ChatBox_KeyDown()

View File

@@ -49,7 +49,7 @@
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/> <Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/>
</Grid> </Grid>
<Button Name="StartGame" Grid.Row="0" Grid.Column="2" Content="Start Game" FontSize="20" Command="{Binding ButtonStartGame}" IsEnabled="{Binding User.Host}"/> <Button Name="StartGame" Grid.Row="0" Grid.Column="2" Content="Start Game" FontSize="20" Command="{Binding ButtonStartGame}" IsEnabled="{Binding IsHost}"/>
<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">

View File

@@ -58,7 +58,7 @@
<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}" Width="150"/> <GridViewColumn Header="max players available" DisplayMemberBinding="{Binding MaxPlayers}" Width="150"/>
<GridViewColumn Header="joineble" DisplayMemberBinding="{Binding LobbyJoineble}"/> <GridViewColumn Header="joinable" DisplayMemberBinding="{Binding LobbyJoinable}"/>
</GridView> </GridView>
</ListView.View> </ListView.View>
</ListView> </ListView>

View File

@@ -150,10 +150,28 @@ namespace Server.Models
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
dynamic canvasData = new { CanvasInfo typeToCheck = JSONConvert.GetCanvasMessageType(payload);
coordinatesLine = JSONConvert.getCoordinates(payload) switch (typeToCheck)
}; {
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData)); case CanvasInfo.DRAWING:
dynamic canvasData = new
{
type = JSONConvert.GetCanvasMessageType(payload),
coordinatesLine = JSONConvert.getCoordinates(payload),
color = JSONConvert.getCanvasDrawingColor(payload)
};
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData));
break;
case CanvasInfo.RESET:
dynamic canvasDataForReset = new
{
type = JSONConvert.GetCanvasMessageType(payload)
};
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasDataForReset));
break;
}
// canvas data // canvas data
// todo send canvas data to all other serverclients in lobby // todo send canvas data to all other serverclients in lobby

View File

@@ -149,6 +149,21 @@ namespace Server.Models
} }
} }
public void SendCanvasDataToLobby(Lobby lobby, string username, byte[] message)
{
foreach (Lobby l in lobbies)
{
if (l == lobby)
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
sc.sendMessage(message);
}
break;
}
}
}
public Lobby GetLobbyForUser(User user) public Lobby GetLobbyForUser(User user)
{ {
foreach (Lobby l in lobbies) foreach (Lobby l in lobbies)
@@ -261,7 +276,7 @@ namespace Server.Models
{ {
if (lobby.ID == lobbyID) if (lobby.ID == lobbyID)
{ {
lobby.LobbyJoineble = false; lobby.LobbyJoinable = false;
} }
} }
} }

View File

@@ -6,7 +6,8 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Media;
namespace SharedClientServer namespace SharedClientServer
{ {
class JSONConvert class JSONConvert
@@ -26,6 +27,13 @@ namespace SharedClientServer
LIST, LIST,
REQUEST REQUEST
} }
public enum CanvasInfo
{
DRAWING,
RESET
}
public static (string,string) GetUsernameAndMessage(byte[] json) public static (string,string) GetUsernameAndMessage(byte[] json)
{ {
string msg = Encoding.ASCII.GetString(json); string msg = Encoding.ASCII.GetString(json);
@@ -149,24 +157,40 @@ namespace SharedClientServer
#endregion #endregion
public static byte[] ConstructCanvasDataSend(double[] coordinates) public static byte[] ConstructCanvasDataSend(CanvasInfo typeToSend, double[] coordinates, Color colorToSend)
{ {
return GetMessageToSend(CANVAS, new return GetMessageToSend(CANVAS, new
{ {
coordinatesLine = coordinates type = typeToSend,
}); coordinatesLine = coordinates,
color = colorToSend
}); ;
}
public static CanvasInfo GetCanvasMessageType(byte[] payload)
{
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
CanvasInfo type = json.type;
return type;
} }
public static double[] getCoordinates(byte[] payload) public static double[] getCoordinates(byte[] payload)
{ {
dynamic payloadD = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
JArray coordinatesArray = payloadD.coordinatesLine; JArray coordinatesArray = json.coordinatesLine;
double[] coordinates = coordinatesArray.ToObject<double[]>(); double[] coordinates = coordinatesArray.ToObject<double[]>();
return coordinates; return coordinates;
} }
public static Color getCanvasDrawingColor(byte[] payload)
{
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
Color color = json.color;
return color;
}
public static byte[] ConstructGameStartData(int lobbyID) public static byte[] ConstructGameStartData(int lobbyID)
{ {
string startGame = "startGame"; string startGame = "startGame";

View File

@@ -14,7 +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 bool _lobbyJoinable;
//private List<string> _usernames; //private List<string> _usernames;
private List<User> _users; private List<User> _users;
@@ -35,7 +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; _lobbyJoinable = true;
} }
public void AddUser(string username, out bool succes) public void AddUser(string username, out bool succes)
@@ -89,10 +89,10 @@ namespace Client
set { _users = value; } set { _users = value; }
} }
public bool LobbyJoineble public bool LobbyJoinable
{ {
get { return _lobbyJoineble; } get { return _lobbyJoinable; }
set { _lobbyJoineble = value; } set { _lobbyJoinable = value; }
} }
} }