Compare commits
1 Commits
feature/di
...
feature/se
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35ded2b5ea |
@@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert;
|
|||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
public delegate void LobbyCallback(int id);
|
public delegate void OnLobbyCreated(int id);
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
@@ -23,8 +23,7 @@ namespace Client
|
|||||||
public Callback OnLobbiesListReceived;
|
public Callback OnLobbiesListReceived;
|
||||||
public Callback OnLobbyJoinSuccess;
|
public Callback OnLobbyJoinSuccess;
|
||||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||||
public LobbyCallback OnLobbyCreated;
|
public OnLobbyCreated OnLobbyCreated;
|
||||||
public LobbyCallback OnLobbyLeave;
|
|
||||||
public Lobby[] Lobbies { get; set; }
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
@@ -119,10 +118,6 @@ namespace Client
|
|||||||
case LobbyIdentifier.JOIN_SUCCESS:
|
case LobbyIdentifier.JOIN_SUCCESS:
|
||||||
OnLobbyJoinSuccess?.Invoke();
|
OnLobbyJoinSuccess?.Invoke();
|
||||||
break;
|
break;
|
||||||
case LobbyIdentifier.LEAVE:
|
|
||||||
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
|
|
||||||
OnLobbyLeave?.Invoke(lobbyLeaveID);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
//TODO fill lobby with the data received
|
//TODO fill lobby with the data received
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ 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);
|
||||||
@@ -42,13 +41,6 @@ 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);
|
||||||
@@ -105,6 +97,19 @@ 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)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ 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.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
@@ -121,12 +120,6 @@ namespace Client.ViewModels
|
|||||||
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e)
|
|
||||||
{
|
|
||||||
Debug.WriteLine("Leaving...");
|
|
||||||
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
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">
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
@@ -17,7 +24,6 @@ namespace Client.Views
|
|||||||
{
|
{
|
||||||
this.viewModel = new ViewModelGame();
|
this.viewModel = new ViewModelGame();
|
||||||
DataContext = this.viewModel;
|
DataContext = this.viewModel;
|
||||||
Closing += this.viewModel.LeaveGame;
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -52,6 +58,5 @@ namespace Client.Views
|
|||||||
{
|
{
|
||||||
viewModel.Color_Picker(e, this);
|
viewModel.Color_Picker(e, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ 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 static SharedClientServer.JSONConvert;
|
using static SharedClientServer.JSONConvert;
|
||||||
@@ -46,8 +45,7 @@ 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)
|
||||||
@@ -89,13 +87,6 @@ namespace Server.Models
|
|||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,12 +166,6 @@ namespace Server.Models
|
|||||||
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
|
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
|
||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
break;
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ 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;
|
||||||
|
|
||||||
|
|
||||||
@@ -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)
|
public void SendToAllExcept(string username, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (ServerClient sc in serverClients)
|
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)
|
public void SendToLobby(Lobby lobby, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -190,7 +161,6 @@ 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);
|
||||||
@@ -213,40 +183,5 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,10 +33,6 @@ 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));
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ namespace SharedClientServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
#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>
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
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 : IEquatable<User>
|
class User
|
||||||
{
|
{
|
||||||
private string _username;
|
private string _username;
|
||||||
private int _score;
|
private int _score;
|
||||||
@@ -28,42 +27,6 @@ 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