From ef255e4828386b25008d442af21c2720d5d0f312 Mon Sep 17 00:00:00 2001 From: Dogukan Date: Thu, 22 Oct 2020 01:37:59 +0200 Subject: [PATCH 01/10] [ADDED] a start for reading the random word from a file --- Server/Models/ServerClient.cs | 1 + Server/Server.csproj | 10 ++++++++++ Server/resources/WordsForGame.json | 31 ++++++++++++++++++++++++++++++ SharedClientServer/JSONConvert.cs | 16 +++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 Server/resources/WordsForGame.json diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index e264363..9bb0b93 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -178,6 +178,7 @@ namespace Server.Models ServerCommunication.INSTANCE.JoinLobby(this.User,id); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); + Debug.WriteLine("Random chosen word: {0}", JSONConvert.GetRandomWord(@"..\resources\WordsForGame.json")); break; case LobbyIdentifier.LEAVE: id = JSONConvert.GetLobbyID(payload); diff --git a/Server/Server.csproj b/Server/Server.csproj index 65a6e4e..d65a717 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -6,6 +6,16 @@ true + + + + + + + PreserveNewest + + + 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 9c96f2d..9f13def 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; @@ -159,7 +160,22 @@ namespace SharedClientServer Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); return res; } + + public static string GetRandomWord(string filename) + { + string[] words = new string[25]; + using(StreamReader reader = new StreamReader(@"../../json1.jsonWordsForGame.json")) + { + string json = reader.ReadToEnd(); + words = JsonConvert.DeserializeObject>(json).ToArray(); + } + Random random = new Random(); + + int index = random.Next(0, 24); + + return words[index]; + } } } From 5b5d66c41b09d377059ce8f760a578e0fb6b6f57 Mon Sep 17 00:00:00 2001 From: Dogukan Date: Thu, 22 Oct 2020 13:55:19 +0200 Subject: [PATCH 02/10] [ADDITION] Tried to send the random word to a lobby --- Client/Client.cs | 4 +++ Client/ViewModels/ViewModelGame.cs | 40 +++++++++++++++++----------- Client/Views/GameWindow.xaml | 2 ++ Server/Models/ServerClient.cs | 11 +++++++- Server/Models/ServerCommunication.cs | 1 + Server/Server.csproj | 2 +- SharedClientServer/JSONConvert.cs | 30 ++++++++++++++++----- 7 files changed, 66 insertions(+), 24 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index bc7b2b5..25f07c1 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -138,6 +138,10 @@ namespace Client // canvas data break; + case JSONConvert.RANDOMWORD: + //Flag byte for receiving the random word. + 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 @@