diff --git a/Client/Client.cs b/Client/Client.cs
index a86ba3e..4799d2e 100644
--- a/Client/Client.cs
+++ b/Client/Client.cs
@@ -49,6 +49,9 @@ namespace Client
private void OnReadComplete(IAsyncResult ar)
{
+ if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
+ return;
+
int amountReceived = stream.EndRead(ar);
if (totalBufferReceived + amountReceived > 1024)
@@ -74,6 +77,7 @@ namespace Client
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
}
+ ar.AsyncWaitHandle.WaitOne();
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
}
@@ -140,6 +144,13 @@ namespace Client
// canvas data
break;
+ case JSONConvert.RANDOMWORD:
+ //Flag byte for receiving the random word.
+ int lobbyId = JSONConvert.GetLobbyID(payload);
+
+ if(data.Lobby?.ID == lobbyId)
+ ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload));
+ break;
default:
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
break;
diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs
index 661e83f..cbc6a49 100644
--- a/Client/ViewModels/ViewModelGame.cs
+++ b/Client/ViewModels/ViewModelGame.cs
@@ -1,11 +1,12 @@
-using Client.Views;
+using Client.Views;
using GalaSoft.MvvmLight.Command;
using SharedClientServer;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
+using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
@@ -25,6 +26,12 @@ namespace Client.ViewModels
private dynamic _payload;
+ public static string Word
+ {
+ get;
+ set;
+ }
+
public string _username;
public string _message;
@@ -82,21 +89,10 @@ namespace Client.ViewModels
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
color = colorSelected;
}
-
+
public ViewModelGame()
{
- if (_payload == null)
- {
- _message = "";
-
- }
- else
- {
- //_message = data.Message;
- //_username = data.User.Username;
- //Messages.Add($"{data.User.Username}: {Message}");
- }
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
}
@@ -121,6 +117,10 @@ namespace Client.ViewModels
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
}
+ /*
+ * MISC make this a callback
+ * Handles the incoming chat message from another client.
+ */
public static void HandleIncomingMsg(string username, string message)
{
Application.Current.Dispatcher.Invoke(delegate
@@ -128,13 +128,21 @@ namespace Client.ViewModels
Messages.Add($"{username}: {message}");
});
}
- public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e)
+ public void LeaveGame(object sender, CancelEventArgs e)
{
Debug.WriteLine("Leaving...");
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID));
}
-
+ /*
+ * MISC make this a callback
+ * Handles the random word that has been received from the server.
+ */
+ public static void HandleRandomWord(string randomWord)
+ {
+ Debug.WriteLine("[CLIENT] Reached the handle random word method!");
+ Word = "NegerPik";
+ }
}
}
-
+
diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml
index 1599e1f..dff9a66 100644
--- a/Client/Views/GameWindow.xaml
+++ b/Client/Views/GameWindow.xaml
@@ -36,6 +36,8 @@
+
+
diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs
index 05eca4f..755f28a 100644
--- a/Server/Models/ServerClient.cs
+++ b/Server/Models/ServerClient.cs
@@ -9,6 +9,8 @@ using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
using static SharedClientServer.JSONConvert;
namespace Server.Models
@@ -22,7 +24,7 @@ namespace Server.Models
private int totalBufferReceived = 0;
public User User { get; set; }
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
-
+
///
/// Constructor that creates a new serverclient object with the given tcp client.
@@ -86,17 +88,17 @@ namespace Server.Models
}
+ ar.AsyncWaitHandle.WaitOne();
// start reading for a new message
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
-
}
catch (IOException e)
{
tcpClient.Close();
ServerCommunication.INSTANCE.ServerClientDisconnect(this);
}
-
-
+
+
}
///
@@ -108,21 +110,21 @@ namespace Server.Models
Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}");
byte id = message[4];
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));
- switch(id)
+ switch (id)
{
-
+
case JSONConvert.LOGIN:
// json log in username data
string uName = JSONConvert.GetUsernameLogin(payload);
-
+
if (uName != null)
{
User = new User(uName);
User.Username = uName;
Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
-
+
}
break;
case JSONConvert.MESSAGE:
@@ -145,13 +147,17 @@ namespace Server.Models
case JSONConvert.LOBBY:
// lobby data
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
- handleLobbyMessage(payload,l);
+ handleLobbyMessage(payload, l);
break;
case JSONConvert.CANVAS:
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
// canvas data
// todo send canvas data to all other serverclients in lobby
break;
+
+ case JSONConvert.RANDOMWORD:
+ //Flag byte for receiving the random word.
+ break;
default:
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
break;
@@ -175,10 +181,21 @@ namespace Server.Models
break;
case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload);
+ ServerCommunication.INSTANCE.JoinLobby(this.User, id);
+ sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
+
bool isHost;
ServerCommunication.INSTANCE.JoinLobby(this.User,id, out isHost);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage(isHost));
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
+
+ //Task.Run(SendLobbyData);
+
+ serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
+ {
+ id = serverCom.GetLobbyForUser(User).ID,
+ word = JSONConvert.SendRandomWord("WordsForGame.json")
+ }));
break;
case LobbyIdentifier.LEAVE:
id = JSONConvert.GetLobbyID(payload);
@@ -189,6 +206,21 @@ namespace Server.Models
}
}
+ private async void SendLobbyData()
+ {
+ string result = await WaitForData();
+ if(result == "bruh momento")
+ {
+
+ }
+ }
+
+ private async Task WaitForData()
+ {
+ await Task.Delay(1000);
+ return "bruh momento";
+ }
+
///
/// sends a message to the tcp client
///
diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs
index dffe07f..2f35e42 100644
--- a/Server/Models/ServerCommunication.cs
+++ b/Server/Models/ServerCommunication.cs
@@ -90,7 +90,7 @@ namespace Server.Models
/// send a message to all tcp clients in the list
///
/// the message to send
- public void sendToAll(byte[] message)
+ public async void sendToAll(byte[] message)
{
foreach (ServerClient sc in serverClients)
{
@@ -142,6 +142,7 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
+ Debug.WriteLine("[SERVERCLIENT] Sending message");
sc.sendMessage(message);
}
break;
diff --git a/Server/Server.csproj b/Server/Server.csproj
index 65a6e4e..0e46462 100644
--- a/Server/Server.csproj
+++ b/Server/Server.csproj
@@ -6,6 +6,16 @@
true
+
+
+
+
+
+
+ Always
+
+
+
diff --git a/Server/resources/WordsForGame.json b/Server/resources/WordsForGame.json
new file mode 100644
index 0000000..71676ba
--- /dev/null
+++ b/Server/resources/WordsForGame.json
@@ -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"
+ ]
+}
\ No newline at end of file
diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs
index 9ab0900..f9af510 100644
--- a/SharedClientServer/JSONConvert.cs
+++ b/SharedClientServer/JSONConvert.cs
@@ -1,9 +1,11 @@
using Client;
+using Microsoft.VisualBasic.CompilerServices;
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;
@@ -15,6 +17,7 @@ namespace SharedClientServer
public const byte MESSAGE = 0x02;
public const byte LOBBY = 0x03;
public const byte CANVAS = 0x04;
+ public const byte RANDOMWORD = 0x05;
public enum LobbyIdentifier
{
@@ -166,7 +169,39 @@ namespace SharedClientServer
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;
+ Random random = new Random();
+ string workingDir = Path.GetFullPath(@"..\Server");
+ string projDir = Directory.GetParent(workingDir).Parent.Parent.FullName;
+ string filePath = projDir += $@"\resources\{filename}";
+ 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;
+ }
}
}