Compare commits
26 Commits
setupBranc
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
302536575c | ||
|
|
53c0cd515d | ||
|
|
3963e07568 | ||
|
|
b13493537e | ||
|
|
9199265eb6 | ||
|
|
9292919f26 | ||
|
|
8018c9c6af | ||
|
|
880d4b4424 | ||
|
|
7601db667c | ||
|
|
7f8dfc0f9c | ||
|
|
b1b03e136d | ||
|
|
5ac4b59e38 | ||
|
|
3c1f1a0028 | ||
|
|
e7b40d7e64 | ||
|
|
27e70d1ec0 | ||
|
|
0e6a2bab59 | ||
|
|
125f4c0410 | ||
|
|
b9b217622a | ||
|
|
2d4540e28e | ||
|
|
052f07db93 | ||
|
|
898b579ca9 | ||
|
|
39ded8524b | ||
|
|
398ba1d5bd | ||
|
|
8ca4efc296 | ||
|
|
3015c0312f | ||
|
|
3c0f5018db |
37
Client/ViewModels/LoginViewModel.cs
Normal file
37
Client/ViewModels/LoginViewModel.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
namespace Client.ViewModels
|
||||
{
|
||||
|
||||
class LoginViewModel
|
||||
{
|
||||
ClientData data = ClientData.Instance;
|
||||
private Window window;
|
||||
|
||||
public LoginViewModel(Window window)
|
||||
{
|
||||
this.window = window;
|
||||
}
|
||||
public void UsernameEntered(string name) {
|
||||
User user = new User(name);
|
||||
|
||||
Client client = new Client(user.Username);
|
||||
client.OnSuccessfullConnect = () =>
|
||||
{
|
||||
// because we need to start the main window on a UI thread, we need to let the dispatcher handle it, which will execute the code on the ui thread
|
||||
Application.Current.Dispatcher.Invoke(delegate {
|
||||
data.User = user;
|
||||
data.Client = client;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
|
||||
MainWindow startWindow = new MainWindow();
|
||||
startWindow.Show();
|
||||
window.Close();
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Timers;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Client.ViewModels
|
||||
@@ -32,8 +32,9 @@ namespace Client.ViewModels
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
public string _username;
|
||||
|
||||
public string _username;
|
||||
|
||||
|
||||
public string _message;
|
||||
public string Message
|
||||
{
|
||||
@@ -73,7 +74,17 @@ namespace Client.ViewModels
|
||||
data.Client.RandomWord = HandleRandomWord;
|
||||
data.Client.IncomingMsg = HandleIncomingMsg;
|
||||
data.Client.IncomingPlayer = HandleIncomingPlayer;
|
||||
data.Client.UpdateUserScores = UpdateUserScores;
|
||||
data.Client.UpdateUserScores = UpdateUserScores;
|
||||
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
{
|
||||
Messages.Clear();
|
||||
});
|
||||
|
||||
|
||||
queueTimer = new Timer(50);
|
||||
queueTimer.Start();
|
||||
queueTimer.Elapsed += sendArrayFromQueue;
|
||||
}
|
||||
|
||||
public ICommand OnKeyDown { get; set; }
|
||||
@@ -82,10 +93,7 @@ namespace Client.ViewModels
|
||||
|
||||
public void BeginGame()
|
||||
{
|
||||
|
||||
queueTimer = new Timer(50);
|
||||
queueTimer.Start();
|
||||
queueTimer.Elapsed += sendArrayFromQueue;
|
||||
|
||||
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
|
||||
}
|
||||
|
||||
@@ -93,7 +101,7 @@ namespace Client.ViewModels
|
||||
private void CanvasResetLocal()
|
||||
{
|
||||
this.window.CanvasForPaint.Children.Clear();
|
||||
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CANVAS_RESET));
|
||||
data.Client.SendMessage(JSONConvert.ConstructCanvasReset());
|
||||
}
|
||||
|
||||
|
||||
@@ -186,7 +194,10 @@ namespace Client.ViewModels
|
||||
|
||||
private void CanvasResetData()
|
||||
{
|
||||
this.window.CanvasForPaint.Children.Clear();
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
{
|
||||
this.window.CanvasForPaint.Children.Clear();
|
||||
});
|
||||
}
|
||||
|
||||
private void ChatBox_KeyDown()
|
||||
|
||||
@@ -40,10 +40,10 @@
|
||||
|
||||
<Label Name="GuessWord" Grid.Row="0" Grid.Column="2" Content="{Binding Path=RandomWord, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"/>
|
||||
|
||||
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/>
|
||||
<Button Name="CanvasReset" Command="{Binding ButtonResetCanvas}" 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 IsHost}"/>
|
||||
|
||||
|
||||
|
||||
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">
|
||||
|
||||
@@ -25,7 +25,5 @@
|
||||
|
||||
<TextBox Name="usernameTextbox" Grid.Row="1" Grid.Column="1" MaxLength="69" FontSize="30" VerticalAlignment="Center" HorizontalAlignment="Left" Width="250"/>
|
||||
<Button Name="LoginButton" Content="ENTER" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Width="100" Height="40" Click="Button_EnterUsername"/>
|
||||
|
||||
<Label Grid.Row="3" Grid.Column="0" Content="Tip of the century: base64 != UTF8!" FontSize="20" FontWeight="Bold"/>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
12
Eindproject/Client2/Client2.csproj
Normal file
12
Eindproject/Client2/Client2.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Client\Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
14
Eindproject/Client2/Program.cs
Normal file
14
Eindproject/Client2/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Client2
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Client.App app = new Client.App();
|
||||
app.Run();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "..\Client\Client.
|
||||
EndProject
|
||||
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SharedClientServer", "..\SharedClientServer\SharedClientServer.shproj", "{6D26F969-9CB1-414F-AC3E-7253D449AC5A}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{4CB2CD76-07D9-44D0-A559-A7A301621D30}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SharedMSBuildProjectFiles) = preSolution
|
||||
..\SharedClientServer\SharedClientServer.projitems*{67a9bf1a-d317-47ca-9f07-c3480d1360ff}*SharedItemsImports = 5
|
||||
@@ -28,6 +30,10 @@ Global
|
||||
{83768EDB-097E-4089-A5DE-208CB252D1A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{83768EDB-097E-4089-A5DE-208CB252D1A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{83768EDB-097E-4089-A5DE-208CB252D1A0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4CB2CD76-07D9-44D0-A559-A7A301621D30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4CB2CD76-07D9-44D0-A559-A7A301621D30}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4CB2CD76-07D9-44D0-A559-A7A301621D30}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4CB2CD76-07D9-44D0-A559-A7A301621D30}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
10
Eindproject/TestHelper/Helper.cs
Normal file
10
Eindproject/TestHelper/Helper.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
|
||||
namespace TestHelper
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
public static JSONConvert JSONConvert;
|
||||
}
|
||||
}
|
||||
14
Eindproject/TestHelper/TestHelper.csproj
Normal file
14
Eindproject/TestHelper/TestHelper.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="..\..\SharedClientServer\SharedClientServer.projitems" Label="Shared" />
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
161
Eindproject/Tests/JSONConvertCanvasMessages.cs
Normal file
161
Eindproject/Tests/JSONConvertCanvasMessages.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertCanvasMessages
|
||||
{
|
||||
//Helper method for the tests
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
|
||||
return json;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructCanvasDataSend()
|
||||
{
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = { 10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
Color color = Color.FromRgb(0, 0, 0);
|
||||
|
||||
byte[] message = JSONConvert.ConstructCanvasDataSend(type, coordinateInfo, color);
|
||||
byte[] payload = GetPayload(message);
|
||||
dynamic json = GetDynamic(payload);
|
||||
int ID = json.canvasType;
|
||||
JArray coorArray = json.coords;
|
||||
double[][] coordinates = coorArray.ToObject<double[][]>();
|
||||
Color colorResult = json.color;
|
||||
|
||||
Assert.AreEqual(0x04, message[4]);
|
||||
Assert.AreEqual(type, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData");
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructDrawingCanvasData()
|
||||
{
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = {10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
Color color = Color.FromRgb(0, 0, 0);
|
||||
|
||||
byte[] message = JSONConvert.ConstructDrawingCanvasData(coordinateInfo, color);
|
||||
byte[] payload = GetPayload(message);
|
||||
dynamic json = GetDynamic(payload);
|
||||
int ID = json.canvasType;
|
||||
JArray coorArray = json.coords;
|
||||
double[][] coordinates = coorArray.ToObject<double[][]>();
|
||||
Color colorResult = json.color;
|
||||
|
||||
Assert.AreEqual(0x04, message[4]);
|
||||
Assert.AreEqual(JSONConvert.CANVAS_WRITING, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData");
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructCanvasReset()
|
||||
{
|
||||
byte[] canvasReset = JSONConvert.ConstructCanvasReset();
|
||||
dynamic json = GetDynamic(GetPayload(canvasReset));
|
||||
int ID = json.canvasType;
|
||||
|
||||
Assert.AreEqual(JSONConvert.CANVAS_RESET, ID, $"Canvas type should be reset(1)! Not, {ID}");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetCanvasMessageType()
|
||||
{
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
int resultID = JSONConvert.GetCanvasMessageType(payload);
|
||||
|
||||
Assert.AreEqual(type, resultID, $"Canvas type should be {IDsend}! Not, {resultID}");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestgetCoordinates()
|
||||
{
|
||||
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = { 10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type,
|
||||
coords = coordinateInfo
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
|
||||
double[][] coordinates = JSONConvert.getCoordinates(payload);
|
||||
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetCanvasDrawingColor()
|
||||
{
|
||||
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
Color colorSend = Color.FromRgb(0, 0, 0);
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type,
|
||||
color = colorSend
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
|
||||
Color colorResult = JSONConvert.getCanvasDrawingColor(payload);
|
||||
|
||||
Assert.AreEqual(colorSend, colorResult, "Colors are not equal!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
100
Eindproject/Tests/JSONConvertGameCommand.cs
Normal file
100
Eindproject/Tests/JSONConvertGameCommand.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertGameCommand
|
||||
{
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||
}
|
||||
|
||||
public byte[] GameCommandMessage()
|
||||
{
|
||||
byte identifier = 0x05;
|
||||
dynamic payload = new
|
||||
{
|
||||
command = JSONConvert.GameCommand.NEXT_ROUND
|
||||
};
|
||||
|
||||
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public byte[] GameLobbyID()
|
||||
{
|
||||
byte identifier = 0x05;
|
||||
dynamic payload = new
|
||||
{
|
||||
lobbyToStart = 1
|
||||
};
|
||||
|
||||
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructGameStartData()
|
||||
{
|
||||
byte[] lobbyData = JSONConvert.ConstructGameStartData(1);
|
||||
dynamic payload = GetDynamic(lobbyData);
|
||||
|
||||
int lobbyid = 1;
|
||||
JSONConvert.GameCommand gameCommand = payload.command;
|
||||
|
||||
Assert.AreEqual(0x05, lobbyData[4]);
|
||||
Assert.AreEqual(JSONConvert.GameCommand.START_GAME, gameCommand);
|
||||
Assert.AreEqual(lobbyid, (int)payload.lobbyToStart);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructGameTimerElapsedMessage()
|
||||
{
|
||||
byte[] lobbyData = JSONConvert.ConstructGameTimerElapsedMessage(1);
|
||||
dynamic payload = GetDynamic(lobbyData);
|
||||
|
||||
int lobbyid = 1;
|
||||
JSONConvert.GameCommand gameCommand = payload.command;
|
||||
|
||||
Assert.AreEqual(0x05, lobbyData[4]);
|
||||
Assert.AreEqual(JSONConvert.GameCommand.TIMER_ELAPSED, gameCommand);
|
||||
Assert.AreEqual(lobbyid, (int)payload.id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetGameCommand()
|
||||
{
|
||||
byte[] data = GetPayload(GameCommandMessage());
|
||||
JSONConvert.GameCommand gameCommand = JSONConvert.GetGameCommand(data);
|
||||
|
||||
Assert.AreEqual(JSONConvert.GameCommand.NEXT_ROUND, gameCommand);
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetStartGameLobbyID()
|
||||
{
|
||||
byte[] data = GetPayload(GameLobbyID());
|
||||
int lobbyID = JSONConvert.GetStartGameLobbyID(data);
|
||||
|
||||
int expected = 1;
|
||||
|
||||
Assert.AreEqual(expected, lobbyID);
|
||||
}
|
||||
}
|
||||
}
|
||||
77
Eindproject/Tests/JSONConvertGetMessageToSendTest.cs
Normal file
77
Eindproject/Tests/JSONConvertGetMessageToSendTest.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
|
||||
[TestClass]
|
||||
public class JSONConvertGetMessageToSendTest
|
||||
{
|
||||
|
||||
private byte[] testArray1()
|
||||
{
|
||||
byte identifier = 0x01;
|
||||
dynamic payload = new
|
||||
{
|
||||
value = "test"
|
||||
};
|
||||
|
||||
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
return result;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetMessageToSendLength()
|
||||
{
|
||||
|
||||
byte identifier = 0x01;
|
||||
dynamic payload = new
|
||||
{
|
||||
value = "test"
|
||||
};
|
||||
string payloadToJson = JsonConvert.SerializeObject(payload);
|
||||
byte[] payloadToBytes = Encoding.UTF8.GetBytes(payloadToJson);
|
||||
|
||||
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
Assert.AreEqual(payloadToBytes.Length + 5, result.Length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetMessageToSendIdentifier()
|
||||
{
|
||||
byte[] result = testArray1();
|
||||
Assert.AreEqual(0x01, result[4]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetMessageToSendMessageLength()
|
||||
{
|
||||
|
||||
byte identifier = 0x01;
|
||||
dynamic payload = new
|
||||
{
|
||||
value = "test"
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(payload);
|
||||
byte[] payloadBytes = Encoding.UTF8.GetBytes(json);
|
||||
byte[] res = new byte[payloadBytes.Length + 5];
|
||||
Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length);
|
||||
res[4] = identifier;
|
||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length + 5), 0, res, 0, 4);
|
||||
int lengthTest = BitConverter.ToInt32(res, 0);
|
||||
byte[] result = testArray1();
|
||||
int lengthResult = BitConverter.ToInt32(result, 0);
|
||||
|
||||
Assert.AreEqual(lengthTest, lengthResult);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
203
Eindproject/Tests/JSONConvertLobbyMessagesTests.cs
Normal file
203
Eindproject/Tests/JSONConvertLobbyMessagesTests.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
using Client;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertLobbyMessagesTests
|
||||
{
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] res)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(res)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyHostMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyHostMessage();
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyHostCreatedMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyHostCreatedMessage(3);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier);
|
||||
Assert.AreEqual(3, id);
|
||||
}
|
||||
[TestMethod]
|
||||
public void TestLobbyRequestMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyRequestMessage();
|
||||
dynamic payload = GetDynamic(res);
|
||||
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, identifier);
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyListMessage()
|
||||
{
|
||||
|
||||
Lobby[] lobbies = new Lobby[] { new Lobby(3, 0, 9), new Lobby(1, 0, 3), new Lobby(1, 0, 1) };
|
||||
byte[] res = JSONConvert.ConstructLobbyListMessage(lobbies);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
JArray lobbiesJArray = payload.lobbies;
|
||||
Lobby[] lobbiesFromDynamic = lobbiesJArray.ToObject<Lobby[]>();
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.LIST, identifier);
|
||||
for (int i = 0; i < lobbies.Length; i++)
|
||||
{
|
||||
Lobby l1 = lobbies[i];
|
||||
Lobby l2 = lobbiesFromDynamic[i];
|
||||
Assert.AreEqual(l1.ID, l2.ID);
|
||||
Assert.AreEqual(l1.PlayersIn, l2.PlayersIn);
|
||||
Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyJoinMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyJoinMessage(8);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN, identifier);
|
||||
Assert.AreEqual(8, id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyLeaveMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyLeaveMessage(4);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.LEAVE, identifier);
|
||||
Assert.AreEqual(4, id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbyIdentifier()
|
||||
{
|
||||
dynamic res = new
|
||||
{
|
||||
identifier = JSONConvert.LobbyIdentifier.REQUEST
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
|
||||
JSONConvert.LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(arr);
|
||||
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, lobbyIdentifier);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyJoinSuccessMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyJoinSuccessMessage(true);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
bool host = payload.host;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN_SUCCESS, identifier);
|
||||
Assert.IsTrue(host);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbiesFromMessage()
|
||||
{
|
||||
Lobby[] lobbiesArray = new Lobby[] { new Lobby(7, 0, 8), new Lobby(3, 0, 5), new Lobby(5, 0, 10) };
|
||||
dynamic res = new
|
||||
{
|
||||
lobbies = lobbiesArray
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
Lobby[] testLobbies = JSONConvert.GetLobbiesFromMessage(arr);
|
||||
|
||||
for (int i = 0; i < lobbiesArray.Length; i++)
|
||||
{
|
||||
Lobby l1 = lobbiesArray[i];
|
||||
Lobby l2 = testLobbies[i];
|
||||
Assert.AreEqual(l1.ID, l2.ID);
|
||||
Assert.AreEqual(l1.PlayersIn, l2.PlayersIn);
|
||||
Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbyID()
|
||||
{
|
||||
dynamic res = new
|
||||
{
|
||||
id = 5
|
||||
};
|
||||
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
int testID = JSONConvert.GetLobbyID(arr);
|
||||
|
||||
Assert.AreEqual(5, testID);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobby()
|
||||
{
|
||||
Lobby l = new Lobby(6, 0, 9);
|
||||
dynamic res = new
|
||||
{
|
||||
lobby = l
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
|
||||
Lobby testLobby = JSONConvert.GetLobby(arr);
|
||||
|
||||
Assert.AreEqual(l.ID, testLobby.ID);
|
||||
Assert.AreEqual(l.MaxPlayers, testLobby.MaxPlayers);
|
||||
Assert.AreEqual(l.PlayersIn, testLobby.PlayersIn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
59
Eindproject/Tests/JSONConvertRandomWord.cs
Normal file
59
Eindproject/Tests/JSONConvertRandomWord.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertRandomWord
|
||||
{
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public byte[] RandomWord()
|
||||
{
|
||||
byte identifier = 0x07;
|
||||
dynamic payload = new
|
||||
{
|
||||
word = "teacher"
|
||||
};
|
||||
|
||||
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSendRandomWord()
|
||||
{
|
||||
string randomWord = JSONConvert.SendRandomWord("WordsForGame.json");
|
||||
|
||||
string result = "teacher";
|
||||
|
||||
Assert.AreEqual(result, randomWord);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetRandomWord()
|
||||
{
|
||||
byte[] data = GetPayload(RandomWord());
|
||||
string result = JSONConvert.GetRandomWord(data);
|
||||
|
||||
string word = "teacher";
|
||||
|
||||
Assert.AreEqual(word, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
89
Eindproject/Tests/JSONConvertUserMessages.cs
Normal file
89
Eindproject/Tests/JSONConvertUserMessages.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertUserMessages
|
||||
{
|
||||
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||
}
|
||||
|
||||
private byte[] ComboArray()
|
||||
{
|
||||
byte identifier = 0x02;
|
||||
dynamic payload = new
|
||||
{
|
||||
username = "testName",
|
||||
message = "message"
|
||||
};
|
||||
|
||||
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] LoginArray()
|
||||
{
|
||||
byte identifier = 0x01;
|
||||
dynamic payload = new
|
||||
{
|
||||
username = "testname"
|
||||
};
|
||||
|
||||
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetUsernameAndMessage()
|
||||
{
|
||||
byte[] data = GetPayload(ComboArray());
|
||||
(string,string) result = JSONConvert.GetUsernameAndMessage(data);
|
||||
|
||||
(string, string) testCombo = ("testName", "message");
|
||||
|
||||
Assert.AreEqual(testCombo, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetUsernameLogin()
|
||||
{
|
||||
byte[] data = GetPayload(LoginArray());
|
||||
string result = JSONConvert.GetUsernameLogin(data);
|
||||
string username = "testname";
|
||||
|
||||
Assert.AreEqual(username, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructUsernameMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructUsernameMessage("testname");
|
||||
dynamic payload = GetDynamic(res);
|
||||
|
||||
string username = "testname";
|
||||
|
||||
Assert.AreEqual(0x01, res[4]);
|
||||
Assert.AreEqual(username, (string)payload.username);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
35
Eindproject/Tests/Tests.csproj
Normal file
35
Eindproject/Tests/Tests.csproj
Normal file
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="resources\WordsForGame.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="resources\WordsForGame.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Client\Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
31
Eindproject/Tests/resources/WordsForGame.json
Normal file
31
Eindproject/Tests/resources/WordsForGame.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"filename": "wordsForGame",
|
||||
"words": [
|
||||
"teacher",
|
||||
"love",
|
||||
"engineer",
|
||||
"supermarket",
|
||||
"disaster",
|
||||
"studio",
|
||||
"restaurant",
|
||||
"music",
|
||||
"chocolate",
|
||||
"dirt",
|
||||
"thought",
|
||||
"virus",
|
||||
"lieutenant",
|
||||
"painter",
|
||||
"kiwi",
|
||||
"power ranger",
|
||||
"computer",
|
||||
"people",
|
||||
"candidate",
|
||||
"security guard",
|
||||
"Canada",
|
||||
"teeth",
|
||||
"army",
|
||||
"airport",
|
||||
"president",
|
||||
"bedroom"
|
||||
]
|
||||
}
|
||||
@@ -176,13 +176,14 @@ namespace Server.Models
|
||||
coords = JSONConvert.getCoordinates(payload),
|
||||
color = JSONConvert.getCanvasDrawingColor(payload)
|
||||
};
|
||||
serverCom.SendToLobby(serverCom.GetLobbyForUser(User),JSONConvert.GetMessageToSend(JSONConvert.CANVAS,canvasData));
|
||||
//serverCom.SendToLobby(serverCom.GetLobbyForUser(User),JSONConvert.GetMessageToSend(JSONConvert.CANVAS,canvasData));
|
||||
serverCom.SendCanvasDataToLobby(serverCom.GetLobbyForUser(User), User.Username, JSONConvert.GetMessageToSend(JSONConvert.CANVAS, canvasData));
|
||||
break;
|
||||
|
||||
case JSONConvert.CANVAS_RESET:
|
||||
dynamic canvasDataForReset = new
|
||||
{
|
||||
type = JSONConvert.GetCanvasMessageType(payload)
|
||||
canvasType = JSONConvert.GetCanvasMessageType(payload)
|
||||
};
|
||||
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasDataForReset));
|
||||
break;
|
||||
|
||||
@@ -174,7 +174,8 @@ namespace Server.Models
|
||||
{
|
||||
foreach (ServerClient sc in serverClientsInlobbies[l])
|
||||
{
|
||||
sc.sendMessage(message);
|
||||
if (sc.User.Username != username)
|
||||
sc.sendMessage(message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
using Client;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace SharedClientServer
|
||||
{
|
||||
class JSONConvert
|
||||
public class JSONConvert
|
||||
{
|
||||
public const byte LOGIN = 0x01;
|
||||
public const byte MESSAGE = 0x02;
|
||||
@@ -23,7 +21,6 @@ namespace SharedClientServer
|
||||
|
||||
public const int CANVAS_WRITING = 0;
|
||||
public const int CANVAS_RESET = 1;
|
||||
|
||||
|
||||
public enum LobbyIdentifier
|
||||
{
|
||||
@@ -42,14 +39,22 @@ namespace SharedClientServer
|
||||
NEXT_ROUND
|
||||
}
|
||||
|
||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||
public static (string, string) GetUsernameAndMessage(byte[] json)
|
||||
{
|
||||
string msg = Encoding.UTF8.GetString(json);
|
||||
dynamic payload = JsonConvert.DeserializeObject(msg);
|
||||
|
||||
dynamic payload = JsonConvert.DeserializeObject(msg);
|
||||
|
||||
return (payload.username, payload.message);
|
||||
}
|
||||
|
||||
internal static byte[] ConstructCanvasResetMessage()
|
||||
{
|
||||
return GetMessageToSend(CANVAS, new
|
||||
{
|
||||
canvasType = CANVAS_RESET
|
||||
});
|
||||
}
|
||||
|
||||
public static string GetUsernameLogin(byte[] json)
|
||||
{
|
||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
|
||||
@@ -80,7 +85,7 @@ namespace SharedClientServer
|
||||
{
|
||||
identifier = LobbyIdentifier.HOST,
|
||||
id = lobbyID
|
||||
}) ;
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] ConstructLobbyRequestMessage()
|
||||
@@ -94,13 +99,12 @@ namespace SharedClientServer
|
||||
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
|
||||
{
|
||||
return GetMessageToSend(LOBBY, new
|
||||
{
|
||||
{
|
||||
identifier = LobbyIdentifier.LIST,
|
||||
lobbies = lobbiesList
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
||||
{
|
||||
return GetMessageToSend(LOBBY, new
|
||||
@@ -117,7 +121,8 @@ namespace SharedClientServer
|
||||
identifier = LobbyIdentifier.LEAVE,
|
||||
id = lobbyID
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
|
||||
{
|
||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
|
||||
@@ -153,8 +158,11 @@ namespace SharedClientServer
|
||||
|
||||
public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost)
|
||||
{
|
||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS,
|
||||
host = isHost});
|
||||
return GetMessageToSend(LOBBY, new
|
||||
{
|
||||
identifier = LobbyIdentifier.JOIN_SUCCESS,
|
||||
host = isHost
|
||||
});
|
||||
}
|
||||
|
||||
public static bool GetLobbyJoinIsHost(byte[] json)
|
||||
@@ -163,11 +171,10 @@ namespace SharedClientServer
|
||||
return payload.host;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion lobby messages
|
||||
|
||||
public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend)
|
||||
{
|
||||
|
||||
return GetMessageToSend(CANVAS, new
|
||||
{
|
||||
canvasType = typeToSend,
|
||||
@@ -184,6 +191,15 @@ namespace SharedClientServer
|
||||
coords = buffer,
|
||||
color = colorToSend
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] ConstructCanvasReset()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
canvasType = CANVAS_RESET
|
||||
};
|
||||
return GetMessageToSend(CANVAS, payload);
|
||||
}
|
||||
|
||||
public static int GetCanvasMessageType(byte[] json)
|
||||
@@ -212,7 +228,6 @@ namespace SharedClientServer
|
||||
|
||||
public static byte[] ConstructGameStartData(int lobbyID)
|
||||
{
|
||||
|
||||
return GetMessageToSend(GAME, new
|
||||
{
|
||||
command = GameCommand.START_GAME,
|
||||
@@ -259,13 +274,14 @@ namespace SharedClientServer
|
||||
// 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);
|
||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length + 5), 0, res, 0, 4);
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* This method sends a random word from the json file, this happens when the client joins a lobby.
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
* This method sends a random word from the json file, this happens when the client joins a lobby.
|
||||
*/
|
||||
|
||||
public static string SendRandomWord(string filename)
|
||||
{
|
||||
dynamic words;
|
||||
@@ -274,29 +290,27 @@ namespace SharedClientServer
|
||||
string projDir = Directory.GetParent(workingDir).Parent.Parent.FullName;
|
||||
string filePath = projDir += $@"\resources\{filename}";
|
||||
|
||||
using(StreamReader reader = new StreamReader(filePath))
|
||||
using (StreamReader reader = new StreamReader(filePath))
|
||||
{
|
||||
string json = reader.ReadToEnd();
|
||||
words = JsonConvert.DeserializeObject(json);
|
||||
}
|
||||
|
||||
|
||||
int index = random.Next(0, 24);
|
||||
|
||||
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
|
||||
|
||||
return words.words[index];
|
||||
}
|
||||
|
||||
return words.words[0];
|
||||
}
|
||||
|
||||
/*
|
||||
* Client gets the payload and retrieves the word from the payload
|
||||
*/
|
||||
*/
|
||||
|
||||
public static string GetRandomWord(byte[] json)
|
||||
{
|
||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||
return payload.word;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using System.Text;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
internal class Lobby : INotifyPropertyChanged
|
||||
public class Lobby : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
<PropertyGroup />
|
||||
<Import Project="SharedClientServer.projitems" Label="Shared" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -6,7 +6,7 @@ using System.Text;
|
||||
|
||||
namespace SharedClientServer
|
||||
{
|
||||
class User : IEquatable<User>
|
||||
public class User : IEquatable<User>
|
||||
{
|
||||
private string _username;
|
||||
private int _score;
|
||||
|
||||
Reference in New Issue
Block a user