20 Commits

Author SHA1 Message Date
Dogukan
7f8dfc0f9c [ADDITION] Added username tests, as well as random word tests. 2020-10-23 23:27:37 +02:00
Sem van der Hoeven
5ac4b59e38 test 2020-10-23 22:25:50 +02:00
Sem van der Hoeven
3c1f1a0028 rename 2020-10-23 22:25:13 +02:00
Sem van der Hoeven
e7b40d7e64 [ADD] test 2020-10-23 22:23:08 +02:00
Sem van der Hoeven
27e70d1ec0 [FIX] tests?? 2020-10-23 22:04:36 +02:00
Sem van der Hoeven
0e6a2bab59 Merge branch 'master' of https://github.com/SemvdH/Csharp-eindproject 2020-10-23 21:14:26 +02:00
Sem van der Hoeven
125f4c0410 [ADD] added 2 test methods 2020-10-23 21:14:16 +02:00
Lars
b9b217622a Merge remote-tracking branch 'origin/master' 2020-10-23 21:07:27 +02:00
Lars
2d4540e28e [DELETED] something 2020-10-23 21:07:12 +02:00
Sem van der Hoeven
052f07db93 [ADD] added test project for real 2020-10-23 21:05:05 +02:00
Sem van der Hoeven
898b579ca9 Merge branch 'master' of https://github.com/SemvdH/Csharp-eindproject 2020-10-23 21:04:29 +02:00
Sem van der Hoeven
39ded8524b [ADD] added test project 2020-10-23 21:04:22 +02:00
Lars
398ba1d5bd [ADDED] other canvas can be reset via an other canvas 2020-10-23 21:04:14 +02:00
Sem van der Hoeven
8ca4efc296 stuff 2020-10-23 20:45:25 +02:00
Sem van der Hoeven
3015c0312f [FIX] fix double drawing 2020-10-23 20:33:47 +02:00
Lars
3c0f5018db [TRYING] 2020-10-23 20:26:05 +02:00
SemvdH
9558c75eab Merge pull request #14 from SemvdH/feature/guesedWord
[ADDITION] added a new property to the user (may be removed later on)…
2020-10-23 15:51:30 +02:00
SemvdH
87ebdeef58 Merge pull request #13 from SemvdH/feature/timer
Feature/timer
2020-10-23 15:45:12 +02:00
Sem van der Hoeven
bf09dba850 [ADD] added timer start on next round message receive 2020-10-23 15:44:18 +02:00
Sem van der Hoeven
6512518fb7 [FIX] made first user in lobby a host if the host leaves 2020-10-23 14:30:16 +02:00
21 changed files with 553 additions and 62 deletions

View File

@@ -214,6 +214,17 @@ namespace Client
default:
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
break;
case JSONConvert.GAME:
switch (JSONConvert.GetGameCommand(payload))
{
case JSONConvert.GameCommand.TIMER_ELAPSED:
int lobbyElapsedID = JSONConvert.GetLobbyID(payload);
//todo set next round
break;
}
break;
}
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED, null));

View 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();
});
};
}
}
}

View File

@@ -34,6 +34,7 @@ namespace Client.ViewModels
public string _username;
public string _message;
public string Message
{
@@ -74,6 +75,16 @@ namespace Client.ViewModels
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;
}
public ICommand OnKeyDown { get; set; }
@@ -83,9 +94,6 @@ 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());
}
@@ -185,8 +193,11 @@ namespace Client.ViewModels
}
private void CanvasResetData()
{
Application.Current.Dispatcher.Invoke(delegate
{
this.window.CanvasForPaint.Children.Clear();
});
}
private void ChatBox_KeyDown()

View File

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

View File

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

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

View File

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

View File

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

View File

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

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

View 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);
}
}
}

View 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);
}
}
}

View 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);
}
}
}

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

View 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"
]
}

View File

@@ -11,6 +11,7 @@ using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using static SharedClientServer.JSONConvert;
namespace Server.Models
@@ -24,6 +25,7 @@ namespace Server.Models
private byte[] totalBuffer = new byte[2048];
private string _randomWord = "";
private int totalBufferReceived = 0;
private Dictionary<System.Timers.Timer, int> lobbyTimers;
public User User { get; set; }
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
private Callback OnMessageReceivedOk;
@@ -35,6 +37,7 @@ namespace Server.Models
/// <param name="client">the TcpClient object to use</param>
public ServerClient(TcpClient client)
{
lobbyTimers = new Dictionary<System.Timers.Timer, int>();
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
tcpClient = client;
stream = tcpClient.GetStream();
@@ -173,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;
@@ -192,14 +196,35 @@ namespace Server.Models
case JSONConvert.GAME:
Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic");
string command = JSONConvert.GetGameCommand(payload);
GameCommand command = JSONConvert.GetGameCommand(payload);
switch (command)
{
case "startGame":
case GameCommand.START_GAME:
int lobbyID = JSONConvert.GetStartGameLobbyID(payload);
serverCom.CloseALobby(lobbyID);
//todo start a timer for this lobby
Debug.WriteLine("[SERVERCLIENT] making timer for lobby " + lobbyID);
System.Timers.Timer lobbyTimer = new System.Timers.Timer(60 * 1000);
this.lobbyTimers.Add(lobbyTimer, lobbyID);
lobbyTimer.Elapsed += LobbyTimer_Elapsed;
lobbyTimer.Start();
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
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)
{
if (lobbyTimers[timer] == lobbyID)
{
timer.Start();
break;
}
}
break;
}
break;
@@ -217,6 +242,17 @@ namespace Server.Models
}
}
private void LobbyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
System.Timers.Timer timer = sender as System.Timers.Timer;
int lobbyID = lobbyTimers[timer];
Debug.WriteLine("[SERVERCLIENT] timer elapsed for lobby " + lobbyID);
serverCom.SendToLobby(lobbyID, JSONConvert.ConstructGameTimerElapsedMessage(lobbyID));
timer.Stop();
}
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
{
switch (l)

View File

@@ -142,7 +142,23 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
Debug.WriteLine("[SERVERCLIENT] Sending message");
Debug.WriteLine("[SERVERCLIENT] Sending message to lobby");
sc.sendMessage(message);
}
break;
}
}
}
public void SendToLobby(int lobbyID, byte[] message)
{
foreach (Lobby l in lobbies)
{
if (l.ID == lobbyID)
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
Debug.WriteLine("[SERVERCLIENT] Sending message to lobby");
sc.sendMessage(message);
}
break;
@@ -158,6 +174,7 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
if (sc.User.Username != username)
sc.sendMessage(message);
}
break;
@@ -261,6 +278,10 @@ namespace Server.Models
break;
}
}
if (l.Users.Count != 0)
{
l.Users[0].Host = true;
}
break;
}
}

View File

@@ -2,16 +2,14 @@ using Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace SharedClientServer
{
class JSONConvert
public class JSONConvert
{
public const byte LOGIN = 0x01;
public const byte MESSAGE = 0x02;
@@ -24,7 +22,6 @@ namespace SharedClientServer
public const int CANVAS_WRITING = 0;
public const int CANVAS_RESET = 1;
public enum LobbyIdentifier
{
HOST,
@@ -35,7 +32,14 @@ namespace SharedClientServer
REQUEST
}
public static (string,string) GetUsernameAndMessage(byte[] json)
public enum GameCommand
{
START_GAME,
TIMER_ELAPSED,
NEXT_ROUND
}
public static (string, string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.UTF8.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
@@ -43,6 +47,14 @@ namespace SharedClientServer
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));
@@ -73,7 +85,7 @@ namespace SharedClientServer
{
identifier = LobbyIdentifier.HOST,
id = lobbyID
}) ;
});
}
public static byte[] ConstructLobbyRequestMessage()
@@ -93,7 +105,6 @@ namespace SharedClientServer
});
}
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
{
return GetMessageToSend(LOBBY, new
@@ -111,6 +122,7 @@ namespace SharedClientServer
id = lobbyID
});
}
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
{
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
@@ -146,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)
@@ -156,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,
@@ -179,6 +193,15 @@ namespace SharedClientServer
});
}
public static byte[] ConstructCanvasReset()
{
dynamic payload = new
{
canvasType = CANVAS_RESET
};
return GetMessageToSend(CANVAS, payload);
}
public static int GetCanvasMessageType(byte[] json)
{
dynamic d = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
@@ -205,15 +228,23 @@ namespace SharedClientServer
public static byte[] ConstructGameStartData(int lobbyID)
{
string startGame = "startGame";
return GetMessageToSend(GAME, new
{
command = startGame,
command = GameCommand.START_GAME,
lobbyToStart = lobbyID
}); ;
}
public static string GetGameCommand(byte[] payload)
public static byte[] ConstructGameTimerElapsedMessage(int lobbyID)
{
return GetMessageToSend(GAME, new
{
command = GameCommand.TIMER_ELAPSED,
id = lobbyID
});
}
public static GameCommand GetGameCommand(byte[] payload)
{
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
return json.command;
@@ -243,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.
*/
public static string SendRandomWord(string filename)
{
dynamic words;
@@ -258,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;
}
}
}

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace Client
{
internal class Lobby : INotifyPropertyChanged
public class Lobby : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

View File

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