Compare commits
1 Commits
feature/js
...
feature/se
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35ded2b5ea |
@@ -8,8 +8,7 @@ using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public delegate void LobbyCallback(int id);
|
||||
public delegate void LobbyJoinCallback(bool isHost);
|
||||
public delegate void OnLobbyCreated(int id);
|
||||
class Client : ObservableObject
|
||||
{
|
||||
private TcpClient tcpClient;
|
||||
@@ -22,11 +21,9 @@ namespace Client
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public LobbyJoinCallback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public LobbyCallback OnLobbyCreated;
|
||||
public LobbyCallback OnLobbyLeave;
|
||||
private ClientData data = ClientData.Instance;
|
||||
public OnLobbyCreated OnLobbyCreated;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
public Client(string username)
|
||||
@@ -49,9 +46,6 @@ 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)
|
||||
@@ -77,7 +71,6 @@ namespace Client
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
ar.AsyncWaitHandle.WaitOne();
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
}
|
||||
|
||||
@@ -88,7 +81,6 @@ 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:
|
||||
@@ -100,11 +92,6 @@ 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);
|
||||
@@ -129,12 +116,7 @@ namespace Client
|
||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||
break;
|
||||
case LobbyIdentifier.JOIN_SUCCESS:
|
||||
|
||||
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
|
||||
break;
|
||||
case LobbyIdentifier.LEAVE:
|
||||
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
|
||||
OnLobbyLeave?.Invoke(lobbyLeaveID);
|
||||
OnLobbyJoinSuccess?.Invoke();
|
||||
break;
|
||||
}
|
||||
//TODO fill lobby with the data received
|
||||
@@ -144,13 +126,6 @@ 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;
|
||||
|
||||
@@ -34,7 +34,6 @@ namespace Client
|
||||
_lobbies = new ObservableCollection<Lobby>();
|
||||
client = ClientData.Instance.Client;
|
||||
client.OnLobbiesListReceived = updateLobbies;
|
||||
client.OnLobbyLeave = leaveLobby;
|
||||
|
||||
|
||||
OnHostButtonClick = new RelayCommand(hostGame);
|
||||
@@ -42,13 +41,6 @@ namespace Client
|
||||
JoinSelectedLobby = new RelayCommand(joinLobby, true);
|
||||
}
|
||||
|
||||
private void leaveLobby(int id)
|
||||
{
|
||||
_model.CanStartGame = true;
|
||||
ClientData.Instance.Lobby = null;
|
||||
SelectedLobby = null;
|
||||
}
|
||||
|
||||
private void hostGame()
|
||||
{
|
||||
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
|
||||
@@ -85,14 +77,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(bool isHost)
|
||||
private void OnLobbyJoinSuccess()
|
||||
{
|
||||
ClientData.Instance.User.Host = isHost;
|
||||
startGameInLobby();
|
||||
}
|
||||
|
||||
@@ -104,6 +96,19 @@ namespace Client
|
||||
Lobby[] lobbiesArr = client.Lobbies;
|
||||
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();
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
|
||||
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;
|
||||
@@ -22,19 +20,13 @@ namespace Client.ViewModels
|
||||
private Point currentPoint = new Point();
|
||||
private Color color;
|
||||
|
||||
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
public static string Word
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
private string _username;
|
||||
|
||||
public string _username;
|
||||
|
||||
public string _message;
|
||||
private string _message;
|
||||
public string Message
|
||||
{
|
||||
get
|
||||
@@ -89,10 +81,21 @@ 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);
|
||||
}
|
||||
|
||||
@@ -117,32 +120,7 @@ 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
|
||||
{
|
||||
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,6 +3,7 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Client.Views"
|
||||
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
|
||||
mc:Ignorable="d"
|
||||
Title="Scrubl.io" Height="600" Width="1200">
|
||||
@@ -36,8 +37,6 @@
|
||||
|
||||
<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">
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
using Client.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Client.Views
|
||||
{
|
||||
@@ -17,7 +24,6 @@ namespace Client.Views
|
||||
{
|
||||
this.viewModel = new ViewModelGame();
|
||||
DataContext = this.viewModel;
|
||||
Closing += this.viewModel.LeaveGame;
|
||||
InitializeComponent();
|
||||
|
||||
}
|
||||
@@ -52,6 +58,5 @@ namespace Client.Views
|
||||
{
|
||||
viewModel.Color_Picker(e, this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,8 @@ using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
@@ -24,7 +21,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.
|
||||
@@ -48,57 +45,49 @@ namespace Server.Models
|
||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||
return;
|
||||
|
||||
try
|
||||
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
{
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
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);
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// start reading for a new message
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,21 +99,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:
|
||||
@@ -133,31 +122,23 @@ namespace Server.Models
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
//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
|
||||
};
|
||||
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
|
||||
//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));
|
||||
// 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!");
|
||||
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;
|
||||
@@ -181,46 +162,13 @@ 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);
|
||||
ServerCommunication.INSTANCE.LeaveLobby(User, id);
|
||||
sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
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>
|
||||
/// sends a message to the tcp client
|
||||
/// </summary>
|
||||
|
||||
@@ -16,7 +16,6 @@ namespace Server.Models
|
||||
public bool Started = false;
|
||||
public List<Lobby> lobbies;
|
||||
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
||||
internal Action DisconnectClientAction;
|
||||
public Action newClientAction;
|
||||
|
||||
|
||||
@@ -90,7 +89,7 @@ namespace Server.Models
|
||||
/// send a message to all tcp clients in the list
|
||||
/// </summary>
|
||||
/// <param name="message">the message to send</param>
|
||||
public async void sendToAll(byte[] message)
|
||||
public void sendToAll(byte[] message)
|
||||
{
|
||||
foreach (ServerClient sc in serverClients)
|
||||
{
|
||||
@@ -98,26 +97,6 @@ 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)
|
||||
{
|
||||
foreach (ServerClient sc in serverClients)
|
||||
@@ -126,14 +105,6 @@ 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)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
@@ -142,7 +113,6 @@ namespace Server.Models
|
||||
{
|
||||
foreach (ServerClient sc in serverClientsInlobbies[l])
|
||||
{
|
||||
Debug.WriteLine("[SERVERCLIENT] Sending message");
|
||||
sc.sendMessage(message);
|
||||
}
|
||||
break;
|
||||
@@ -191,7 +161,6 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int HostForLobby(User user)
|
||||
{
|
||||
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
||||
@@ -202,58 +171,17 @@ namespace Server.Models
|
||||
return lobby.ID;
|
||||
}
|
||||
|
||||
public void JoinLobby(User user, int id, out bool isHost)
|
||||
public void JoinLobby(User user, int id)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,16 +6,6 @@
|
||||
<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" />
|
||||
|
||||
@@ -33,10 +33,6 @@ namespace Server.ViewModels
|
||||
{
|
||||
InformationModel.ClientsConnected++;
|
||||
};
|
||||
serverCommunication.DisconnectClientAction = () =>
|
||||
{
|
||||
InformationModel.ClientsConnected--;
|
||||
};
|
||||
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
|
||||
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"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,11 +1,9 @@
|
||||
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;
|
||||
|
||||
@@ -17,7 +15,6 @@ 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
|
||||
{
|
||||
@@ -136,19 +133,13 @@ namespace SharedClientServer
|
||||
return dynamicAsObject.ToObject<Lobby>();
|
||||
}
|
||||
|
||||
public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost)
|
||||
public static byte[] ConstructLobbyJoinSuccessMessage()
|
||||
{
|
||||
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;
|
||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// constructs a message that can be sent to the clients or server
|
||||
/// </summary>
|
||||
@@ -169,39 +160,7 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
|
||||
namespace SharedClientServer
|
||||
{
|
||||
class User : IEquatable<User>
|
||||
class User
|
||||
{
|
||||
private string _username;
|
||||
private int _score;
|
||||
@@ -28,42 +27,6 @@ namespace SharedClientServer
|
||||
_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
|
||||
{
|
||||
get { return _username; }
|
||||
|
||||
Reference in New Issue
Block a user