Compare commits
22 Commits
feature/se
...
feature/js
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5075f285d1 | ||
|
|
8190d9b31b | ||
|
|
b8d0f206ba | ||
|
|
3b667b3f0c | ||
|
|
5b5d66c41b | ||
|
|
d4d0545f0b | ||
|
|
0b72c4dc9b | ||
|
|
471247827b | ||
|
|
ef255e4828 | ||
|
|
b08e6cc749 | ||
|
|
c150bf3611 | ||
|
|
332fcf799c | ||
|
|
4d3dda023c | ||
|
|
e8a72e164f | ||
|
|
d5d6d59690 | ||
|
|
754c1c5db9 | ||
|
|
8a90e74e74 | ||
|
|
be99c8d3f9 | ||
|
|
9e1ac07b80 | ||
|
|
cc5c71a30f | ||
|
|
442cdccc49 | ||
|
|
c339746a82 |
@@ -8,7 +8,8 @@ using static SharedClientServer.JSONConvert;
|
|||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
public delegate void OnLobbyCreated(int id);
|
public delegate void LobbyCallback(int id);
|
||||||
|
public delegate void LobbyJoinCallback(bool isHost);
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
@@ -21,9 +22,11 @@ namespace Client
|
|||||||
private string username;
|
private string username;
|
||||||
public Callback OnSuccessfullConnect;
|
public Callback OnSuccessfullConnect;
|
||||||
public Callback OnLobbiesListReceived;
|
public Callback OnLobbiesListReceived;
|
||||||
public Callback OnLobbyJoinSuccess;
|
public LobbyJoinCallback OnLobbyJoinSuccess;
|
||||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||||
public OnLobbyCreated OnLobbyCreated;
|
public LobbyCallback OnLobbyCreated;
|
||||||
|
public LobbyCallback OnLobbyLeave;
|
||||||
|
private ClientData data = ClientData.Instance;
|
||||||
public Lobby[] Lobbies { get; set; }
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
@@ -46,6 +49,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)
|
||||||
@@ -71,6 +77,7 @@ namespace Client
|
|||||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ar.AsyncWaitHandle.WaitOne();
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +88,7 @@ namespace Client
|
|||||||
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("[CLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
{
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
@@ -92,6 +100,11 @@ namespace Client
|
|||||||
string textUsername = combo.Item1;
|
string textUsername = combo.Item1;
|
||||||
string textMsg = combo.Item2;
|
string textMsg = combo.Item2;
|
||||||
|
|
||||||
|
if(textUsername != data.User.Username)
|
||||||
|
{
|
||||||
|
ViewModels.ViewModelGame.HandleIncomingMsg(textUsername, textMsg);
|
||||||
|
}
|
||||||
|
|
||||||
//TODO display username and message in chat window
|
//TODO display username and message in chat window
|
||||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||||
@@ -116,7 +129,12 @@ namespace Client
|
|||||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||||
break;
|
break;
|
||||||
case LobbyIdentifier.JOIN_SUCCESS:
|
case LobbyIdentifier.JOIN_SUCCESS:
|
||||||
OnLobbyJoinSuccess?.Invoke();
|
|
||||||
|
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
|
||||||
|
break;
|
||||||
|
case LobbyIdentifier.LEAVE:
|
||||||
|
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
|
||||||
|
OnLobbyLeave?.Invoke(lobbyLeaveID);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//TODO fill lobby with the data received
|
//TODO fill lobby with the data received
|
||||||
@@ -126,6 +144,13 @@ namespace Client
|
|||||||
// canvas data
|
// canvas data
|
||||||
break;
|
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:
|
default:
|
||||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ namespace Client
|
|||||||
_lobbies = new ObservableCollection<Lobby>();
|
_lobbies = new ObservableCollection<Lobby>();
|
||||||
client = ClientData.Instance.Client;
|
client = ClientData.Instance.Client;
|
||||||
client.OnLobbiesListReceived = updateLobbies;
|
client.OnLobbiesListReceived = updateLobbies;
|
||||||
|
client.OnLobbyLeave = leaveLobby;
|
||||||
|
|
||||||
|
|
||||||
OnHostButtonClick = new RelayCommand(hostGame);
|
OnHostButtonClick = new RelayCommand(hostGame);
|
||||||
@@ -41,6 +42,13 @@ namespace Client
|
|||||||
JoinSelectedLobby = new RelayCommand(joinLobby, true);
|
JoinSelectedLobby = new RelayCommand(joinLobby, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void leaveLobby(int id)
|
||||||
|
{
|
||||||
|
_model.CanStartGame = true;
|
||||||
|
ClientData.Instance.Lobby = null;
|
||||||
|
SelectedLobby = null;
|
||||||
|
}
|
||||||
|
|
||||||
private void hostGame()
|
private void hostGame()
|
||||||
{
|
{
|
||||||
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
|
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
|
||||||
@@ -77,14 +85,14 @@ namespace Client
|
|||||||
|
|
||||||
private void joinLobby()
|
private void joinLobby()
|
||||||
{
|
{
|
||||||
// lobby die je wilt joinen verwijderen
|
|
||||||
// nieuwe binnengekregen lobby toevoegen
|
|
||||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnLobbyJoinSuccess()
|
private void OnLobbyJoinSuccess(bool isHost)
|
||||||
{
|
{
|
||||||
|
ClientData.Instance.User.Host = isHost;
|
||||||
startGameInLobby();
|
startGameInLobby();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,19 +105,6 @@ namespace Client
|
|||||||
Application.Current.Dispatcher.Invoke(delegate
|
Application.Current.Dispatcher.Invoke(delegate
|
||||||
{
|
{
|
||||||
|
|
||||||
//for (int i = 0; i < lobbiesArr.Length; i++)
|
|
||||||
//{
|
|
||||||
// Lobby lobby = lobbiesArr[i];
|
|
||||||
// Debug.WriteLine(lobby.PlayersIn);
|
|
||||||
// if (i < _lobbies.Count && _lobbies[i].ID == lobby.ID)
|
|
||||||
// {
|
|
||||||
// _lobbies[i].Set(lobby);
|
|
||||||
// } else
|
|
||||||
// {
|
|
||||||
// _lobbies.Add(lobbiesArr[i]);
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
_lobbies.Clear();
|
_lobbies.Clear();
|
||||||
|
|
||||||
foreach (Lobby l in lobbiesArr)
|
foreach (Lobby l in lobbiesArr)
|
||||||
|
|||||||
@@ -1,10 +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.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;
|
||||||
@@ -20,13 +22,19 @@ namespace Client.ViewModels
|
|||||||
private Point currentPoint = new Point();
|
private Point currentPoint = new Point();
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||||
|
|
||||||
private dynamic _payload;
|
private dynamic _payload;
|
||||||
|
|
||||||
private string _username;
|
public static string Word
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
private string _message;
|
public string _username;
|
||||||
|
|
||||||
|
public string _message;
|
||||||
public string Message
|
public string Message
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -85,17 +93,6 @@ namespace Client.ViewModels
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +117,32 @@ 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)
|
||||||
|
{
|
||||||
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:local="clr-namespace:Client.Views"
|
|
||||||
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
|
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Title="Scrubl.io" Height="600" Width="1200">
|
Title="Scrubl.io" Height="600" Width="1200">
|
||||||
@@ -37,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">
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
using Client.ViewModels;
|
using Client.ViewModels;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
|
||||||
using System.Windows.Data;
|
|
||||||
using System.Windows.Documents;
|
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
|
||||||
using System.Windows.Shapes;
|
|
||||||
|
|
||||||
namespace Client.Views
|
namespace Client.Views
|
||||||
{
|
{
|
||||||
@@ -24,6 +17,7 @@ namespace Client.Views
|
|||||||
{
|
{
|
||||||
this.viewModel = new ViewModelGame();
|
this.viewModel = new ViewModelGame();
|
||||||
DataContext = this.viewModel;
|
DataContext = this.viewModel;
|
||||||
|
Closing += this.viewModel.LeaveGame;
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -58,5 +52,6 @@ namespace Client.Views
|
|||||||
{
|
{
|
||||||
viewModel.Color_Picker(e, this);
|
viewModel.Color_Picker(e, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ using SharedClientServer;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
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
|
||||||
@@ -45,7 +48,8 @@ namespace Server.Models
|
|||||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
int bytesReceived = this.stream.EndRead(ar);
|
int bytesReceived = this.stream.EndRead(ar);
|
||||||
|
|
||||||
if (totalBufferReceived + bytesReceived > 1024)
|
if (totalBufferReceived + bytesReceived > 1024)
|
||||||
@@ -84,8 +88,15 @@ namespace Server.Models
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
ar.AsyncWaitHandle.WaitOne();
|
||||||
// start reading for a new message
|
// start reading for a new message
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
tcpClient.Close();
|
||||||
|
ServerCommunication.INSTANCE.ServerClientDisconnect(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -99,9 +110,9 @@ 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:
|
||||||
@@ -122,23 +133,31 @@ namespace Server.Models
|
|||||||
string textUsername = combo.Item1;
|
string textUsername = combo.Item1;
|
||||||
string textMsg = combo.Item2;
|
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
|
//Sends the incomming message to be broadcast to all of the clients inside the current lobby.
|
||||||
serverCom.SendToLobby(ServerCommunication.INSTANCE.GetLobbyForUser(User),payload);
|
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet));
|
||||||
Debug.WriteLine("Payload has been sent!");
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
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!!!");
|
||||||
// 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;
|
||||||
@@ -162,13 +181,46 @@ 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());
|
||||||
|
|
||||||
|
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);
|
||||||
|
ServerCommunication.INSTANCE.LeaveLobby(User, id);
|
||||||
|
sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace Server.Models
|
|||||||
public bool Started = false;
|
public bool Started = false;
|
||||||
public List<Lobby> lobbies;
|
public List<Lobby> lobbies;
|
||||||
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
||||||
|
internal Action DisconnectClientAction;
|
||||||
public Action newClientAction;
|
public Action newClientAction;
|
||||||
|
|
||||||
|
|
||||||
@@ -89,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)
|
||||||
{
|
{
|
||||||
@@ -97,6 +98,26 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ServerClientDisconnect(ServerClient serverClient)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("[SERVERCOMM] handling disconnect");
|
||||||
|
DisconnectClientAction?.Invoke();
|
||||||
|
int id = -1;
|
||||||
|
foreach (Lobby l in serverClientsInlobbies.Keys)
|
||||||
|
{
|
||||||
|
if (serverClientsInlobbies[l].Contains(serverClient))
|
||||||
|
{
|
||||||
|
id = l.ID;
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id != -1)
|
||||||
|
{
|
||||||
|
LeaveLobby(serverClient.User, id);
|
||||||
|
SendToAllExcept(serverClient, JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendToAllExcept(string username, byte[] message)
|
public void SendToAllExcept(string username, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (ServerClient sc in serverClients)
|
foreach (ServerClient sc in serverClients)
|
||||||
@@ -105,6 +126,14 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendToAllExcept(ServerClient sc, byte[] message)
|
||||||
|
{
|
||||||
|
foreach (ServerClient s in serverClients)
|
||||||
|
{
|
||||||
|
if (s != sc) s.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendToLobby(Lobby lobby, byte[] message)
|
public void SendToLobby(Lobby lobby, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -113,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;
|
||||||
@@ -161,6 +191,7 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int HostForLobby(User user)
|
public int HostForLobby(User user)
|
||||||
{
|
{
|
||||||
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
||||||
@@ -171,17 +202,58 @@ namespace Server.Models
|
|||||||
return lobby.ID;
|
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)
|
foreach (Lobby l in lobbies)
|
||||||
{
|
{
|
||||||
if (l.ID == id)
|
if (l.ID == id)
|
||||||
{
|
{
|
||||||
|
if (l.Users.Count == 0)
|
||||||
|
{
|
||||||
|
user.Host = true;
|
||||||
|
isHost = true;
|
||||||
|
}
|
||||||
AddToLobby(l, user);
|
AddToLobby(l, user);
|
||||||
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void LeaveLobby(User user, int id)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("[SERVERCOMM] removing user from lobby");
|
||||||
|
foreach (Lobby l in lobbies)
|
||||||
|
{
|
||||||
|
if (l.ID == id)
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"[SERVERCOMM] checking for lobby with id {l.ID}");
|
||||||
|
|
||||||
|
foreach (User u in l.Users)
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"[SERVERCOMM] checking if {u.Username} is {user.Username} ");
|
||||||
|
// contains doesn't work, so we'll do it like this...
|
||||||
|
if (u.Username == user.Username)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("[SERVERCOMM] removed user from lobby!");
|
||||||
|
l.Users.Remove(user);
|
||||||
|
foreach (ServerClient sc in serverClients)
|
||||||
|
{
|
||||||
|
if (sc.User.Username == user.Username)
|
||||||
|
{
|
||||||
|
serverClientsInlobbies[l].Remove(sc);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,16 @@
|
|||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="resources\WordsForGame.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="resources\WordsForGame.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
|
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
|
||||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ namespace Server.ViewModels
|
|||||||
{
|
{
|
||||||
InformationModel.ClientsConnected++;
|
InformationModel.ClientsConnected++;
|
||||||
};
|
};
|
||||||
|
serverCommunication.DisconnectClientAction = () =>
|
||||||
|
{
|
||||||
|
InformationModel.ClientsConnected--;
|
||||||
|
};
|
||||||
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
|
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
|
||||||
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
|
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
|
||||||
|
|
||||||
|
|||||||
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 Client;
|
||||||
|
using Microsoft.VisualBasic.CompilerServices;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -15,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
|
||||||
{
|
{
|
||||||
@@ -133,13 +136,19 @@ namespace SharedClientServer
|
|||||||
return dynamicAsObject.ToObject<Lobby>();
|
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
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// constructs a message that can be sent to the clients or server
|
/// constructs a message that can be sent to the clients or server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -161,6 +170,38 @@ namespace SharedClientServer
|
|||||||
return res;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
{
|
{
|
||||||
class User
|
class User : IEquatable<User>
|
||||||
{
|
{
|
||||||
private string _username;
|
private string _username;
|
||||||
private int _score;
|
private int _score;
|
||||||
@@ -27,6 +28,42 @@ namespace SharedClientServer
|
|||||||
_host = false;
|
_host = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(User u1, User u2)
|
||||||
|
{
|
||||||
|
if (object.ReferenceEquals(u1, null))
|
||||||
|
{
|
||||||
|
return object.ReferenceEquals(u2, null);
|
||||||
|
}
|
||||||
|
return u1.Equals(u2 as object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(User u1, User u2)
|
||||||
|
{
|
||||||
|
if (object.ReferenceEquals(u1, null))
|
||||||
|
{
|
||||||
|
return object.ReferenceEquals(u2, null);
|
||||||
|
}
|
||||||
|
return u1.Equals(u2 as object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
if ((obj == null) || !this.GetType().Equals(obj.GetType()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return this.Equals(obj as User);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals([AllowNull] User other)
|
||||||
|
{
|
||||||
|
return other.Username == this.Username;
|
||||||
|
}
|
||||||
|
|
||||||
public string Username
|
public string Username
|
||||||
{
|
{
|
||||||
get { return _username; }
|
get { return _username; }
|
||||||
|
|||||||
Reference in New Issue
Block a user