[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.Net.Sockets;
using System.Text;
using System.Windows.Media;
using static SharedClientServer.JSONConvert;
namespace Client
{
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);
class Client : ObservableObject
{
@@ -32,6 +34,7 @@ namespace Client
public LobbyCallback OnLobbyLeave;
private ClientData data = ClientData.Instance;
public CanvasDataReceived CanvasDataReceived;
public CanvasReset CReset;
public Lobby[] Lobbies { get; set; }
public Client(string username)
@@ -55,13 +58,14 @@ namespace Client
private void OnReadComplete(IAsyncResult ar)
{
int amountReceived = stream.EndRead(ar);
if (totalBufferReceived > 2048)
if (totalBufferReceived + amountReceived > 1024)
{
throw new OutOfMemoryException("buffer too small");
}
// 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;
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
@@ -145,7 +149,17 @@ namespace Client
case JSONConvert.CANVAS:
// canvas data
//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;
default:

View File

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

View File

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

View File

@@ -49,7 +49,7 @@
<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}"/>
<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">

View File

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

View File

@@ -150,10 +150,28 @@ namespace Server.Models
case JSONConvert.CANVAS:
Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
dynamic canvasData = new {
coordinatesLine = JSONConvert.getCoordinates(payload)
};
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData));
CanvasInfo typeToCheck = JSONConvert.GetCanvasMessageType(payload);
switch (typeToCheck)
{
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
// 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)
{
foreach (Lobby l in lobbies)
@@ -261,7 +276,7 @@ namespace Server.Models
{
if (lobby.ID == lobbyID)
{
lobby.LobbyJoineble = false;
lobby.LobbyJoinable = false;
}
}
}

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace SharedClientServer
{
@@ -26,6 +27,13 @@ namespace SharedClientServer
LIST,
REQUEST
}
public enum CanvasInfo
{
DRAWING,
RESET
}
public static (string,string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.ASCII.GetString(json);
@@ -149,24 +157,40 @@ namespace SharedClientServer
#endregion
public static byte[] ConstructCanvasDataSend(double[] coordinates)
public static byte[] ConstructCanvasDataSend(CanvasInfo typeToSend, double[] coordinates, Color colorToSend)
{
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)
{
dynamic payloadD = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
JArray coordinatesArray = payloadD.coordinatesLine;
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
JArray coordinatesArray = json.coordinatesLine;
double[] coordinates = coordinatesArray.ToObject<double[]>();
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)
{
string startGame = "startGame";

View File

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