Merge branch 'feature/jsonForWords' of https://github.com/SemvdH/Csharp-eindproject into feature/jsonForWords

This commit is contained in:
Sem van der Hoeven
2020-10-22 16:16:08 +02:00
4 changed files with 39 additions and 14 deletions

View File

@@ -48,6 +48,9 @@ namespace Client
private void OnReadComplete(IAsyncResult ar) private void OnReadComplete(IAsyncResult ar)
{ {
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
return;
int amountReceived = stream.EndRead(ar); int amountReceived = stream.EndRead(ar);
if (totalBufferReceived + amountReceived > 1024) if (totalBufferReceived + amountReceived > 1024)
@@ -140,6 +143,9 @@ namespace Client
case JSONConvert.RANDOMWORD: case JSONConvert.RANDOMWORD:
//Flag byte for receiving the random word. //Flag byte for receiving the random word.
int lobbyId = JSONConvert.GetLobbyID(payload);
if(data.Lobby?.ID == lobbyId)
ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload)); ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload));
break; break;
default: default:

View File

@@ -9,6 +9,8 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static SharedClientServer.JSONConvert; using static SharedClientServer.JSONConvert;
namespace Server.Models namespace Server.Models
@@ -22,7 +24,7 @@ namespace Server.Models
private int totalBufferReceived = 0; private int totalBufferReceived = 0;
public User User { get; set; } public User User { get; set; }
private ServerCommunication serverCom = ServerCommunication.INSTANCE; private ServerCommunication serverCom = ServerCommunication.INSTANCE;
/// <summary> /// <summary>
/// Constructor that creates a new serverclient object with the given tcp client. /// Constructor that creates a new serverclient object with the given tcp client.
@@ -96,8 +98,8 @@ namespace Server.Models
tcpClient.Close(); tcpClient.Close();
ServerCommunication.INSTANCE.ServerClientDisconnect(this); ServerCommunication.INSTANCE.ServerClientDisconnect(this);
} }
} }
/// <summary> /// <summary>
@@ -109,21 +111,21 @@ namespace Server.Models
Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}"); Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}");
byte id = message[4]; byte id = message[4];
byte[] payload = new byte[message.Length - 5]; byte[] payload = new byte[message.Length - 5];
Array.Copy(message,5,payload,0,message.Length-5); Array.Copy(message, 5, payload, 0, message.Length - 5);
Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload)); Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
switch(id) switch (id)
{ {
case JSONConvert.LOGIN: case JSONConvert.LOGIN:
// json log in username data // json log in username data
string uName = JSONConvert.GetUsernameLogin(payload); string uName = JSONConvert.GetUsernameLogin(payload);
if (uName != null) if (uName != null)
{ {
User = new User(uName); User = new User(uName);
User.Username = uName; User.Username = uName;
Debug.WriteLine("[SERVERCLIENT] set username to " + uName); Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
} }
break; break;
case JSONConvert.MESSAGE: case JSONConvert.MESSAGE:
@@ -146,7 +148,7 @@ namespace Server.Models
case JSONConvert.LOBBY: case JSONConvert.LOBBY:
// lobby data // lobby data
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
handleLobbyMessage(payload,l); handleLobbyMessage(payload, l);
break; break;
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
@@ -180,15 +182,17 @@ namespace Server.Models
break; break;
case LobbyIdentifier.JOIN: case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload); int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User,id); ServerCommunication.INSTANCE.JoinLobby(this.User, id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(RANDOMWORD, new Task.Run(SendLobbyData);
serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
{ {
id = serverCom.GetLobbyForUser(User).ID,
word = JSONConvert.SendRandomWord("WordsForGame.json") word = JSONConvert.SendRandomWord("WordsForGame.json")
})); }));
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break; break;
case LobbyIdentifier.LEAVE: case LobbyIdentifier.LEAVE:
id = JSONConvert.GetLobbyID(payload); id = JSONConvert.GetLobbyID(payload);
@@ -199,6 +203,20 @@ namespace Server.Models
} }
} }
private async void SendLobbyData()
{
string result = await WaitForData();
if(result == "bruh momento")
{
}
}
private async Task<string> WaitForData()
{
await Task.Delay(1000);
return "bruh momento";
}
/// <summary> /// <summary>
/// sends a message to the tcp client /// sends a message to the tcp client
/// </summary> /// </summary>

View File

@@ -90,7 +90,7 @@ namespace Server.Models
/// send a message to all tcp clients in the list /// send a message to all tcp clients in the list
/// </summary> /// </summary>
/// <param name="message">the message to send</param> /// <param name="message">the message to send</param>
public void sendToAll(byte[] message) public async void sendToAll(byte[] message)
{ {
foreach (ServerClient sc in serverClients) foreach (ServerClient sc in serverClients)
{ {

View File

@@ -184,6 +184,7 @@ namespace SharedClientServer
int index = random.Next(0, 24); int index = random.Next(0, 24);
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}"); Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
return words.words[index]; return words.words[index];
} }