Compare commits
13 Commits
feature/di
...
feature/js
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5075f285d1 | ||
|
|
8190d9b31b | ||
|
|
b8d0f206ba | ||
|
|
3b667b3f0c | ||
|
|
5b5d66c41b | ||
|
|
d4d0545f0b | ||
|
|
0b72c4dc9b | ||
|
|
471247827b | ||
|
|
ef255e4828 | ||
|
|
b08e6cc749 | ||
|
|
332fcf799c | ||
|
|
4d3dda023c | ||
|
|
d5d6d59690 |
@@ -9,6 +9,7 @@ using static SharedClientServer.JSONConvert;
|
||||
namespace Client
|
||||
{
|
||||
public delegate void LobbyCallback(int id);
|
||||
public delegate void LobbyJoinCallback(bool isHost);
|
||||
class Client : ObservableObject
|
||||
{
|
||||
private TcpClient tcpClient;
|
||||
@@ -21,10 +22,11 @@ namespace Client
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public Callback OnLobbyJoinSuccess;
|
||||
public LobbyJoinCallback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public LobbyCallback OnLobbyCreated;
|
||||
public LobbyCallback OnLobbyLeave;
|
||||
private ClientData data = ClientData.Instance;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
public Client(string username)
|
||||
@@ -47,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)
|
||||
@@ -72,6 +77,7 @@ namespace Client
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
ar.AsyncWaitHandle.WaitOne();
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
}
|
||||
|
||||
@@ -82,6 +88,7 @@ namespace Client
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
|
||||
Debug.WriteLine("[CLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
|
||||
switch (id)
|
||||
{
|
||||
case JSONConvert.LOGIN:
|
||||
@@ -93,6 +100,11 @@ namespace Client
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
if(textUsername != data.User.Username)
|
||||
{
|
||||
ViewModels.ViewModelGame.HandleIncomingMsg(textUsername, textMsg);
|
||||
}
|
||||
|
||||
//TODO display username and message in chat window
|
||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
@@ -117,7 +129,8 @@ namespace Client
|
||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||
break;
|
||||
case LobbyIdentifier.JOIN_SUCCESS:
|
||||
OnLobbyJoinSuccess?.Invoke();
|
||||
|
||||
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
|
||||
break;
|
||||
case LobbyIdentifier.LEAVE:
|
||||
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
|
||||
@@ -131,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;
|
||||
|
||||
@@ -85,14 +85,14 @@ namespace Client
|
||||
|
||||
private void joinLobby()
|
||||
{
|
||||
// lobby die je wilt joinen verwijderen
|
||||
// nieuwe binnengekregen lobby toevoegen
|
||||
|
||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||
}
|
||||
|
||||
private void OnLobbyJoinSuccess()
|
||||
private void OnLobbyJoinSuccess(bool isHost)
|
||||
{
|
||||
ClientData.Instance.User.Host = isHost;
|
||||
startGameInLobby();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -21,13 +22,19 @@ namespace Client.ViewModels
|
||||
private Point currentPoint = new Point();
|
||||
private Color color;
|
||||
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
private string _username;
|
||||
public static string Word
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private string _message;
|
||||
public string _username;
|
||||
|
||||
public string _message;
|
||||
public string Message
|
||||
{
|
||||
get
|
||||
@@ -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,13 +117,32 @@ namespace Client.ViewModels
|
||||
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
||||
}
|
||||
|
||||
public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
Messages.Add($"{username}: {message}");
|
||||
});
|
||||
}
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
|
||||
<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"/>
|
||||
|
||||
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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:
|
||||
@@ -131,23 +133,31 @@ namespace Server.Models
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
//Takes the data sent from the client, and then sets it in a data packet to be sent.
|
||||
dynamic packet = new
|
||||
{
|
||||
username = textUsername,
|
||||
message = textMsg
|
||||
};
|
||||
|
||||
// todo handle sending to all except this user the username and message to display in chat
|
||||
serverCom.SendToLobby(ServerCommunication.INSTANCE.GetLobbyForUser(User),payload);
|
||||
Debug.WriteLine("Payload has been sent!");
|
||||
//Sends the incomming message to be broadcast to all of the clients inside the current lobby.
|
||||
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet));
|
||||
break;
|
||||
|
||||
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;
|
||||
@@ -171,9 +181,21 @@ namespace Server.Models
|
||||
break;
|
||||
case LobbyIdentifier.JOIN:
|
||||
int id = JSONConvert.GetLobbyID(payload);
|
||||
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
|
||||
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);
|
||||
@@ -184,6 +206,21 @@ 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>
|
||||
/// sends a message to the tcp client
|
||||
/// </summary>
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace Server.Models
|
||||
/// send a message to all tcp clients in the list
|
||||
/// </summary>
|
||||
/// <param name="message">the message to send</param>
|
||||
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;
|
||||
@@ -201,12 +202,18 @@ namespace Server.Models
|
||||
return lobby.ID;
|
||||
}
|
||||
|
||||
public void JoinLobby(User user, int id)
|
||||
public void JoinLobby(User user, int id, out bool isHost)
|
||||
{
|
||||
isHost = false;
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l.ID == id)
|
||||
{
|
||||
if (l.Users.Count == 0)
|
||||
{
|
||||
user.Host = true;
|
||||
isHost = true;
|
||||
}
|
||||
AddToLobby(l, user);
|
||||
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
||||
break;
|
||||
|
||||
@@ -6,6 +6,16 @@
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="resources\WordsForGame.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="resources\WordsForGame.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
|
||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
||||
|
||||
31
Server/resources/WordsForGame.json
Normal file
31
Server/resources/WordsForGame.json
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
@@ -133,9 +136,16 @@ namespace SharedClientServer
|
||||
return dynamicAsObject.ToObject<Lobby>();
|
||||
}
|
||||
|
||||
public static byte[] ConstructLobbyJoinSuccessMessage()
|
||||
public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost)
|
||||
{
|
||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS,
|
||||
host = isHost});
|
||||
}
|
||||
|
||||
public static bool GetLobbyJoinIsHost(byte[] json)
|
||||
{
|
||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||
return payload.host;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -159,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user