[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");
}
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;
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
@@ -144,8 +148,18 @@ namespace Client
case JSONConvert.CANVAS:
// canvas data
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
//clientData.CanvasData = 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>