diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index b10639b..d5846cf 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Text; +using static SharedClientServer.JSONConvert; namespace Server.Models { @@ -121,6 +122,7 @@ namespace Server.Models case JSONConvert.LOBBY: // lobby data + LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); break; case JSONConvert.CANVAS: // canvas data diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index b0d83cb..6620e06 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -14,11 +14,12 @@ namespace SharedClientServer public const byte LOBBY = 0x03; public const byte CANVAS = 0x04; - enum LobbyIdentifier + public enum LobbyIdentifier { HOST, - ADD, + JOIN, LEAVE, + LIST, REQUEST } public static (string,string) GetUsernameAndMessage(byte[] json) @@ -43,12 +44,46 @@ namespace SharedClientServer }); } - public static byte[] ConstructLobbyDataMessage(Lobby lobby) + public static byte[] ConstructLobbyRequestMessage() { - return null; + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.REQUEST + }); } + 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 + { + identifier = LobbyIdentifier.JOIN, + id = lobbyID + }); + } + + public static byte[] ConstructLobbyLeaveMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.LEAVE, + id = lobbyID + }); + } + + public static int GetLobbyID(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.id; + } /// /// constructs a message that can be sent to the clients or server @@ -70,5 +105,11 @@ namespace SharedClientServer Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); return res; } + + public static LobbyIdentifier GetLobbyIdentifier(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.identifier; + } } }