6 Commits

Author SHA1 Message Date
Sem van der Hoeven
7ce04447d0 [FIX] fixed canvas data being sent to person drawing 2020-10-23 17:44:39 +02:00
Sem van der Hoeven
2d64accf09 [EDIT] made loginviewmodel kinda mvvm 2020-10-23 17:35:33 +02:00
Sem van der Hoeven
1d8963ec60 [REMOVE] removed the checks for who is host 2020-10-23 17:27:43 +02:00
Lars
13cf0e44ee [ADDED] a begin for the turn base 2020-10-23 16:46:29 +02:00
Lars
eee4f0347f Merge branch 'master' into setupBranch 2020-10-23 15:54:21 +02:00
Lars
fd59368c03 Merge branch 'master' into setupBranch 2020-10-23 14:59:41 +02:00
21 changed files with 150 additions and 454 deletions

View File

@@ -221,6 +221,23 @@ namespace Client
int lobbyElapsedID = JSONConvert.GetLobbyID(payload);
//todo set next round
break;
case JSONConvert.GameCommand.INITIALIZE:
int lobbyID = JSONConvert.GetLobbyID(payload);
string userName = JSONConvert.GetUsernameLogin(payload);
if (lobbyID == clientData.Lobby.ID)
{
if (userName == clientData.User.Username)
{
clientData.User.TurnToDraw = true;
Debug.WriteLine("[CLIENT] Setting a player's turnToDraw to true");
}
clientData.Client.UpdateUserScores?.Invoke(clientData.Lobby);
}
break;
}

View File

@@ -1,37 +0,0 @@
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();
});
};
}
}
}

View File

@@ -121,13 +121,18 @@ namespace Client
_lobbies.Clear();
Lobby clientLobby = ClientData.Instance.Lobby;
foreach (Lobby l in lobbiesArr)
{
_lobbies.Add(l);
Lobby clientLobby = ClientData.Instance.Lobby;
if (l.ID == clientLobby?.ID)
{
clientLobby = l;
clientLobby.Users.Clear();
foreach (User user in l.Users)
{
clientLobby.Users.Add(user);
}
}
}

View File

@@ -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
@@ -27,14 +27,15 @@ namespace Client.ViewModels
public Queue<double[][]> linesQueue;
private Timer queueTimer;
private bool wordGuessed = false;
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
public ObservableCollection<string> Players { get; } = new ObservableCollection<string>();
private dynamic _payload;
public string _username;
public string _username;
public string _message;
public string Message
{
@@ -51,7 +52,23 @@ namespace Client.ViewModels
private string _randomWord;
public string RandomWord
{
get { return _randomWord; }
get {
if (data.User.TurnToDraw)
return _randomWord;
if (!wordGuessed)
{
string hiddenWord = "";
for (int i = 0; i < _randomWord.Length; i++)
{
hiddenWord += "_ ";
}
return hiddenWord;
}
else
return _randomWord;
}
set { _randomWord = value; }
}
@@ -60,10 +77,15 @@ namespace Client.ViewModels
get { return data.User.Host; }
}
public bool UserTurnToDraw
{
get { return data.User.TurnToDraw; }
}
public ViewModelGame(GameWindow window)
{
this.window = window;
_randomWord = "";
buffer = new double[maxLines][];
linesQueue = new Queue<double[][]>();
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
@@ -74,17 +96,7 @@ namespace Client.ViewModels
data.Client.RandomWord = HandleRandomWord;
data.Client.IncomingMsg = HandleIncomingMsg;
data.Client.IncomingPlayer = HandleIncomingPlayer;
data.Client.UpdateUserScores = UpdateUserScores;
Application.Current.Dispatcher.Invoke(delegate
{
Messages.Clear();
});
queueTimer = new Timer(50);
queueTimer.Start();
queueTimer.Elapsed += sendArrayFromQueue;
data.Client.UpdateUserScores = UpdateUserScores;
}
public ICommand OnKeyDown { get; set; }
@@ -93,7 +105,10 @@ namespace Client.ViewModels
public void BeginGame()
{
queueTimer = new Timer(50);
queueTimer.Start();
queueTimer.Elapsed += sendArrayFromQueue;
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
}
@@ -101,7 +116,7 @@ namespace Client.ViewModels
private void CanvasResetLocal()
{
this.window.CanvasForPaint.Children.Clear();
data.Client.SendMessage(JSONConvert.ConstructCanvasReset());
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CANVAS_RESET));
}
@@ -152,7 +167,9 @@ namespace Client.ViewModels
public void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
sendArrayFromQueue(sender, null);
sendArrayFromQueue(sender, null);
}
private void sendArrayFromQueue(object sender, ElapsedEventArgs e)
@@ -194,10 +211,7 @@ namespace Client.ViewModels
private void CanvasResetData()
{
Application.Current.Dispatcher.Invoke(delegate
{
this.window.CanvasForPaint.Children.Clear();
});
this.window.CanvasForPaint.Children.Clear();
}
private void ChatBox_KeyDown()
@@ -249,7 +263,7 @@ namespace Client.ViewModels
Players.Clear();
foreach (var item in lobby.Users)
{
Players.Add(item.Username + "\n" + item.Score);
Players.Add(item.Username + "\n" + item.Score + "\n" + item.TurnToDraw);
}
});
}

View File

@@ -35,15 +35,15 @@
<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"/>
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100" />
<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" Command="{Binding ButtonResetCanvas}" Grid.Row="0" Grid.Column="3" Content="RESET"/>
<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 IsHost}"/>
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">

View File

@@ -25,5 +25,7 @@
<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>

View File

@@ -20,8 +20,10 @@ namespace Client.Views
public partial class LoginScreen : Window
{
ClientData data = ClientData.Instance;
private LoginViewModel loginViewModel;
public LoginScreen()
{
loginViewModel = new LoginViewModel(this);
InitializeComponent();
}
@@ -29,22 +31,8 @@ namespace Client.Views
{
string name = usernameTextbox.Text;
if (name == string.Empty) return;
User user = new User(name);
Client client = new Client(user.Username);
LoginButton.IsEnabled = false;
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();
this.Close();
});
};
loginViewModel.UsernameEntered(name);
}

View File

@@ -1,12 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,14 +0,0 @@
using System;
namespace Client2
{
class Program
{
static void Main(string[] args)
{
Client.App app = new Client.App();
app.Run();
}
}
}

View File

@@ -9,8 +9,6 @@ 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
@@ -30,10 +28,6 @@ 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

View File

@@ -1,10 +0,0 @@
using SharedClientServer;
using System;
namespace TestHelper
{
public class Helper
{
public static JSONConvert JSONConvert;
}
}

View File

@@ -1,14 +0,0 @@
<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>

View File

@@ -1,158 +0,0 @@
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!");
}
}
}

View File

@@ -1,77 +0,0 @@
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);
}
}
}

View File

@@ -1,25 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<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>

View File

@@ -176,14 +176,13 @@ namespace Server.Models
coords = JSONConvert.getCoordinates(payload),
color = JSONConvert.getCanvasDrawingColor(payload)
};
//serverCom.SendToLobby(serverCom.GetLobbyForUser(User),JSONConvert.GetMessageToSend(JSONConvert.CANVAS,canvasData));
serverCom.SendCanvasDataToLobby(serverCom.GetLobbyForUser(User), User.Username, 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
{
canvasType = JSONConvert.GetCanvasMessageType(payload)
type = JSONConvert.GetCanvasMessageType(payload)
};
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasDataForReset));
break;
@@ -209,12 +208,14 @@ namespace Server.Models
lobbyTimer.Elapsed += LobbyTimer_Elapsed;
lobbyTimer.Start();
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
serverCom.SendToLobby(lobbyID, JSONConvert.ConstructGameInitializeData( serverCom.FindUserNameInLobby(lobbyID),lobbyID));
break;
case GameCommand.TIMER_ELAPSED:
break;
case GameCommand.NEXT_ROUND:
// The next round has been started, so we can start the timer again
lobbyID = JSONConvert.GetLobbyID(payload);
foreach (System.Timers.Timer timer in lobbyTimers.Keys)
{

View File

@@ -299,8 +299,24 @@ namespace Server.Models
if (lobby.ID == lobbyID)
{
lobby.LobbyJoinable = false;
break;
}
}
}
public string FindUserNameInLobby(int lobbyID)
{
Lobby lobbyFound = null;
foreach (Lobby lobby in lobbies)
{
if (lobby.ID == lobbyID)
{
lobbyFound = lobby;
break;
}
}
return lobbyFound?.Users[lobbyFound.UserDrawing]?.Username;
}
}
}

View File

@@ -1,15 +1,17 @@
using Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Media;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace SharedClientServer
{
public class JSONConvert
class JSONConvert
{
public const byte LOGIN = 0x01;
public const byte MESSAGE = 0x02;
@@ -21,6 +23,7 @@ namespace SharedClientServer
public const int CANVAS_WRITING = 0;
public const int CANVAS_RESET = 1;
public enum LobbyIdentifier
{
@@ -35,26 +38,19 @@ namespace SharedClientServer
public enum GameCommand
{
START_GAME,
INITIALIZE,
TIMER_ELAPSED,
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));
@@ -85,7 +81,7 @@ namespace SharedClientServer
{
identifier = LobbyIdentifier.HOST,
id = lobbyID
});
}) ;
}
public static byte[] ConstructLobbyRequestMessage()
@@ -99,12 +95,13 @@ 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
@@ -121,8 +118,7 @@ namespace SharedClientServer
identifier = LobbyIdentifier.LEAVE,
id = lobbyID
});
}
}
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
{
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
@@ -158,11 +154,8 @@ 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)
@@ -171,10 +164,11 @@ namespace SharedClientServer
return payload.host;
}
#endregion lobby messages
#endregion
public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend)
{
return GetMessageToSend(CANVAS, new
{
canvasType = typeToSend,
@@ -191,15 +185,6 @@ 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)
@@ -228,6 +213,7 @@ namespace SharedClientServer
public static byte[] ConstructGameStartData(int lobbyID)
{
return GetMessageToSend(GAME, new
{
command = GameCommand.START_GAME,
@@ -235,6 +221,17 @@ namespace SharedClientServer
}); ;
}
public static byte[] ConstructGameInitializeData(string userName, int lobbyID)
{
dynamic payload = new
{
id = lobbyID,
command = GameCommand.INITIALIZE,
username = userName
};
return GetMessageToSend(GAME, payload);
}
public static byte[] ConstructGameTimerElapsedMessage(int lobbyID)
{
return GetMessageToSend(GAME, new
@@ -274,14 +271,13 @@ 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;
@@ -290,27 +286,29 @@ 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];
}
}
/*
* 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;
}
}
}
}

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace Client
{
public class Lobby : INotifyPropertyChanged
internal class Lobby : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
@@ -17,6 +17,7 @@ namespace Client
private bool _lobbyJoinable;
//private List<string> _usernames;
private List<User> _users;
private int _userDrawing;
//public void AddUsername(string username, out bool success)
//{
@@ -36,6 +37,7 @@ namespace Client
//_usernames = new List<string>();
_users = new List<User>();
_lobbyJoinable = true;
_userDrawing = 0;
}
public void AddUser(string username, out bool succes)
@@ -95,5 +97,11 @@ namespace Client
set { _lobbyJoinable = value; }
}
public int UserDrawing
{
get { return _userDrawing; }
set { _userDrawing = value; }
}
}
}

View File

@@ -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>

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace SharedClientServer
{
public class User : IEquatable<User>
class User : IEquatable<User>
{
private string _username;
private int _score;