[ADDITION] Tried to send the random word to a lobby

This commit is contained in:
Dogukan
2020-10-22 13:55:19 +02:00
parent ef255e4828
commit 5b5d66c41b
7 changed files with 66 additions and 24 deletions

View File

@@ -138,6 +138,10 @@ namespace Client
// canvas data // canvas data
break; break;
case JSONConvert.RANDOMWORD:
//Flag byte for receiving the random word.
ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload));
break;
default: default:
Debug.WriteLine("[CLIENT] Received weird identifier: " + id); Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
break; break;

View File

@@ -1,11 +1,12 @@
using Client.Views; using Client.Views;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using SharedClientServer; using SharedClientServer;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
@@ -25,6 +26,12 @@ namespace Client.ViewModels
private dynamic _payload; private dynamic _payload;
public static string Word
{
get;
set;
}
public string _username; public string _username;
public string _message; public string _message;
@@ -82,21 +89,10 @@ namespace Client.ViewModels
colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B; colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B;
color = colorSelected; color = colorSelected;
} }
public ViewModelGame() 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); OnKeyDown = new RelayCommand(ChatBox_KeyDown);
} }
@@ -121,6 +117,10 @@ namespace Client.ViewModels
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload)); 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) public static void HandleIncomingMsg(string username, string message)
{ {
Application.Current.Dispatcher.Invoke(delegate Application.Current.Dispatcher.Invoke(delegate
@@ -128,13 +128,21 @@ namespace Client.ViewModels
Messages.Add($"{username}: {message}"); Messages.Add($"{username}: {message}");
}); });
} }
public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e) public void LeaveGame(object sender, CancelEventArgs e)
{ {
Debug.WriteLine("Leaving..."); Debug.WriteLine("Leaving...");
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID)); 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";
}
} }
} }

View File

@@ -36,6 +36,8 @@
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="2" Margin="84,10,10,10" Content="RESET"/> <Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="2" Margin="84,10,10,10" Content="RESET"/>
<Label Name="GuessWord" Grid.Row="0" Grid.Column="1" Content="{Binding Path=Word, UpdateSourceTrigger=PropertyChanged}" Margin="140,0,109,0"/>
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/> <xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5"> <Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">

View File

@@ -152,6 +152,10 @@ namespace Server.Models
// canvas data // canvas data
// todo send canvas data to all other serverclients in lobby // todo send canvas data to all other serverclients in lobby
break; break;
case JSONConvert.RANDOMWORD:
//Flag byte for receiving the random word.
break;
default: default:
Debug.WriteLine("[SERVER] Received weird identifier: " + id); Debug.WriteLine("[SERVER] Received weird identifier: " + id);
break; break;
@@ -177,8 +181,13 @@ namespace Server.Models
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());
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(RANDOMWORD, new
{
word = JSONConvert.SendRandomWord("WordsForGame.json")
}));
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
Debug.WriteLine("Random chosen word: {0}", JSONConvert.GetRandomWord(@"..\resources\WordsForGame.json"));
break; break;
case LobbyIdentifier.LEAVE: case LobbyIdentifier.LEAVE:
id = JSONConvert.GetLobbyID(payload); id = JSONConvert.GetLobbyID(payload);

View File

@@ -142,6 +142,7 @@ namespace Server.Models
{ {
foreach (ServerClient sc in serverClientsInlobbies[l]) foreach (ServerClient sc in serverClientsInlobbies[l])
{ {
Debug.WriteLine("[SERVERCLIENT] Sending message");
sc.sendMessage(message); sc.sendMessage(message);
} }
break; break;

View File

@@ -12,7 +12,7 @@
<ItemGroup> <ItemGroup>
<Content Include="resources\WordsForGame.json"> <Content Include="resources\WordsForGame.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

@@ -1,4 +1,5 @@
using Client; using Client;
using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
@@ -16,6 +17,7 @@ namespace SharedClientServer
public const byte MESSAGE = 0x02; public const byte MESSAGE = 0x02;
public const byte LOBBY = 0x03; public const byte LOBBY = 0x03;
public const byte CANVAS = 0x04; public const byte CANVAS = 0x04;
public const byte RANDOMWORD = 0x05;
public enum LobbyIdentifier public enum LobbyIdentifier
{ {
@@ -161,21 +163,37 @@ namespace SharedClientServer
return res; return res;
} }
public static string GetRandomWord(string filename) /*
* This method sends a random word from the json file, this happens when the client joins a lobby.
*/
public static string SendRandomWord(string filename)
{ {
string[] words = new string[25]; dynamic words;
using(StreamReader reader = new StreamReader(@"../../json1.jsonWordsForGame.json")) 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(); string json = reader.ReadToEnd();
words = JsonConvert.DeserializeObject<List<string>>(json).ToArray(); words = JsonConvert.DeserializeObject(json);
} }
Random random = new Random();
int index = random.Next(0, 24); int index = random.Next(0, 24);
return words[index]; 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;
}
} }
} }