Compare commits
5 Commits
lobbies
...
feature/br
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4e45b1a6b | ||
|
|
74f8e868f6 | ||
|
|
381c142eaa | ||
|
|
4d161391b1 | ||
|
|
7dbdcc8bcc |
@@ -4,11 +4,9 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using static SharedClientServer.JSONConvert;
|
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
public delegate void OnLobbyCreated(int id);
|
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
@@ -20,11 +18,6 @@ namespace Client
|
|||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
private string username;
|
private string username;
|
||||||
public Callback OnSuccessfullConnect;
|
public Callback OnSuccessfullConnect;
|
||||||
public Callback OnLobbiesListReceived;
|
|
||||||
public Callback OnLobbyJoinSuccess;
|
|
||||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
|
||||||
public OnLobbyCreated OnLobbyCreated;
|
|
||||||
public Lobby[] Lobbies { get; set; }
|
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
{
|
{
|
||||||
@@ -72,14 +65,13 @@ namespace Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleData(byte[] message)
|
private void handleData(byte[] message)
|
||||||
{
|
{
|
||||||
byte id = message[4];
|
byte id = message[4];
|
||||||
byte[] payload = new byte[message.Length - 5];
|
byte[] payload = new byte[message.Length - 1];
|
||||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
Array.Copy(message, 1, payload, 0, message.Length - 1);
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
{
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
@@ -90,37 +82,21 @@ namespace Client
|
|||||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||||
string textUsername = combo.Item1;
|
string textUsername = combo.Item1;
|
||||||
string textMsg = combo.Item2;
|
string textMsg = combo.Item2;
|
||||||
//TODO display username and message in chat window
|
|
||||||
|
|
||||||
|
//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);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload);
|
|
||||||
switch (lobbyIdentifier)
|
|
||||||
{
|
|
||||||
case LobbyIdentifier.LIST:
|
|
||||||
Debug.WriteLine("got lobbies list");
|
|
||||||
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
|
|
||||||
OnLobbiesListReceived?.Invoke();
|
|
||||||
OnLobbiesReceivedAndWaitingForHost?.Invoke();
|
|
||||||
break;
|
|
||||||
case LobbyIdentifier.HOST:
|
|
||||||
// we receive this when the server has made us a host of a new lobby
|
|
||||||
// TODO get lobby id
|
|
||||||
Debug.WriteLine("[CLIENT] got lobby object");
|
|
||||||
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
|
|
||||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
|
||||||
break;
|
|
||||||
case LobbyIdentifier.JOIN_SUCCESS:
|
|
||||||
OnLobbyJoinSuccess?.Invoke();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
//TODO fill lobby with the data received
|
//TODO fill lobby with the data received
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
// canvas data
|
// canvas data
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ namespace Client
|
|||||||
private User _user;
|
private User _user;
|
||||||
private Client _client;
|
private Client _client;
|
||||||
private Lobby _lobby;
|
private Lobby _lobby;
|
||||||
|
private string _message;
|
||||||
|
|
||||||
private ClientData()
|
private ClientData()
|
||||||
{
|
{
|
||||||
@@ -55,5 +56,17 @@ namespace Client
|
|||||||
set { _lobby = value; }
|
set { _lobby = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String Message
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _message;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_message = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ using System.Diagnostics;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using Client.Views;
|
using Client.Views;
|
||||||
using System.Linq;
|
|
||||||
using System.Windows.Data;
|
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
@@ -23,101 +21,21 @@ namespace Client
|
|||||||
|
|
||||||
public Lobby SelectedLobby { get; set; }
|
public Lobby SelectedLobby { get; set; }
|
||||||
|
|
||||||
private Client client;
|
|
||||||
|
|
||||||
private bool wantToBeHost = false;
|
|
||||||
private int wantToBeHostId = 0;
|
|
||||||
|
|
||||||
public ViewModel()
|
public ViewModel()
|
||||||
{
|
{
|
||||||
_model = new Model();
|
_model = new Model();
|
||||||
_lobbies = new ObservableCollection<Lobby>();
|
_lobbies = new ObservableCollection<Lobby>();
|
||||||
client = ClientData.Instance.Client;
|
|
||||||
client.OnLobbiesListReceived = updateLobbies;
|
|
||||||
|
|
||||||
|
_lobbies.Add(new Lobby(50, 3, 8));
|
||||||
|
_lobbies.Add(new Lobby(69, 1, 9));
|
||||||
|
_lobbies.Add(new Lobby(420, 7, 7));
|
||||||
|
|
||||||
OnHostButtonClick = new RelayCommand(hostGame);
|
OnHostButtonClick = new RelayCommand(() =>
|
||||||
|
|
||||||
JoinSelectedLobby = new RelayCommand(joinLobby, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void hostGame()
|
|
||||||
{
|
|
||||||
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
|
|
||||||
client.SendMessage(JSONConvert.ConstructLobbyHostMessage());
|
|
||||||
client.OnLobbyCreated = becomeHostForLobby;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void becomeHostForLobby(int id)
|
|
||||||
{
|
|
||||||
|
|
||||||
Debug.WriteLine($"got host succes with data {id} ");
|
|
||||||
wantToBeHost = true;
|
|
||||||
wantToBeHostId = id;
|
|
||||||
client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void hostLobbiesReceived()
|
|
||||||
{
|
|
||||||
if (wantToBeHost)
|
|
||||||
foreach (Lobby l in Lobbies)
|
|
||||||
{
|
|
||||||
if (l.ID == wantToBeHostId)
|
|
||||||
{
|
|
||||||
Debug.WriteLine("found lobby that we want to be host of: " + l.ID + ", joining..");
|
|
||||||
SelectedLobby = l;
|
|
||||||
startGameInLobby();
|
|
||||||
wantToBeHost = false;
|
|
||||||
client.OnLobbiesReceivedAndWaitingForHost = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
startGameInLobby();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void updateLobbies()
|
|
||||||
{
|
|
||||||
Debug.WriteLine("updating lobbies...");
|
|
||||||
Lobby[] lobbiesArr = client.Lobbies;
|
|
||||||
Application.Current.Dispatcher.Invoke(delegate
|
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("Host button clicked");
|
||||||
//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();
|
|
||||||
|
|
||||||
foreach (Lobby l in lobbiesArr)
|
|
||||||
{
|
|
||||||
_lobbies.Add(l);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startGameInLobby()
|
private void startGameInLobby()
|
||||||
@@ -125,18 +43,11 @@ namespace Client
|
|||||||
if (SelectedLobby != null)
|
if (SelectedLobby != null)
|
||||||
{
|
{
|
||||||
ClientData.Instance.Lobby = SelectedLobby;
|
ClientData.Instance.Lobby = SelectedLobby;
|
||||||
startGameWindow();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startGameWindow()
|
_model.CanStartGame = false;
|
||||||
{
|
|
||||||
_model.CanStartGame = false;
|
|
||||||
Application.Current.Dispatcher.Invoke(delegate
|
|
||||||
{
|
|
||||||
GameWindow window = new GameWindow();
|
GameWindow window = new GameWindow();
|
||||||
window.Show();
|
window.Show();
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClickCheck()
|
private void ClickCheck()
|
||||||
|
|||||||
@@ -1,10 +1,71 @@
|
|||||||
using System.Collections.ObjectModel;
|
using GalaSoft.MvvmLight.Command;
|
||||||
|
using SharedClientServer;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
namespace Client.ViewModels
|
namespace Client.ViewModels
|
||||||
{
|
{
|
||||||
class ViewModelGame : INotifyPropertyChanged
|
class ViewModelGame : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
ClientData data = ClientData.Instance;
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||||
|
|
||||||
|
private dynamic _payload;
|
||||||
|
|
||||||
|
private string _username;
|
||||||
|
|
||||||
|
private string _message;
|
||||||
|
public string Message
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _message;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_message = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ICommand OnKeyDown { get; set; }
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ChatBox_KeyDown()
|
||||||
|
{
|
||||||
|
//if enter then clear textbox and send message.
|
||||||
|
if (Message != string.Empty) AddMessage(Message);
|
||||||
|
Message = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void AddMessage(string message)
|
||||||
|
{
|
||||||
|
Messages.Add($"{data.User.Username}: {message}");
|
||||||
|
|
||||||
|
_payload = new
|
||||||
|
{
|
||||||
|
username = data.User.Username,
|
||||||
|
message = message
|
||||||
|
};
|
||||||
|
|
||||||
|
//Broadcast the message after adding it to the list!
|
||||||
|
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,8 +48,13 @@
|
|||||||
</Border>
|
</Border>
|
||||||
|
|
||||||
<Grid Grid.Column="2" Grid.Row="1">
|
<Grid Grid.Column="2" Grid.Row="1">
|
||||||
<TextBox Name="SentMessage" IsReadOnly="True"/>
|
<ListBox Name ="TextBox" ItemsSource="{Binding Path=Messages}" Margin="0,0,0,69"/>
|
||||||
<TextBox x:Name="ChatBox" Keyboard.KeyDown="ChatBox_KeyDown" Width="200" Height="50" ToolTip="Message goes here" ToolTipService.IsEnabled="True" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
|
|
||||||
|
<TextBox Name="ChatBox" Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,465,0,0">
|
||||||
|
<TextBox.InputBindings>
|
||||||
|
<KeyBinding Key="Return" Command="{Binding OnKeyDown}"/>
|
||||||
|
</TextBox.InputBindings>
|
||||||
|
</TextBox>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace Client.Views
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class GameWindow : Window
|
public partial class GameWindow : Window
|
||||||
{
|
{
|
||||||
ClientData data = ClientData.Instance;
|
|
||||||
public GameWindow()
|
public GameWindow()
|
||||||
{
|
{
|
||||||
DataContext = new ViewModelGame();
|
DataContext = new ViewModelGame();
|
||||||
@@ -82,7 +82,6 @@ namespace Client.Views
|
|||||||
// var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
|
// var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
|
||||||
// TEST.Children.Add(deepCopy);
|
// TEST.Children.Add(deepCopy);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
|
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
|
||||||
@@ -95,28 +94,14 @@ namespace Client.Views
|
|||||||
color = colorSelected;
|
color = colorSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ChatBox_KeyDown(object sender, KeyEventArgs e)
|
///*
|
||||||
{
|
// * Writes the current client's message to the chatbox.
|
||||||
//if enter then clear textbox and send message.
|
// */
|
||||||
if (e.Key.Equals(Key.Enter))
|
//private void WriteToChat(string message)
|
||||||
{
|
//{
|
||||||
WriteToChat(ChatBox.Text);
|
// string user = data.User.Username;
|
||||||
ChatBox.Clear();
|
// SentMessage.AppendText($"{user}: {message}\n");
|
||||||
}
|
// data.User.Message = message;
|
||||||
}
|
//}
|
||||||
|
|
||||||
/*
|
|
||||||
* Writes the current client's message to the chatbox.
|
|
||||||
*/
|
|
||||||
private void WriteToChat(string message)
|
|
||||||
{
|
|
||||||
string user = data.User.Username;
|
|
||||||
SentMessage.AppendText($"{user}: {message}\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ namespace Client.Views
|
|||||||
Application.Current.Dispatcher.Invoke(delegate {
|
Application.Current.Dispatcher.Invoke(delegate {
|
||||||
data.User = user;
|
data.User = user;
|
||||||
data.Client = client;
|
data.Client = client;
|
||||||
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
|
|
||||||
MainWindow startWindow = new MainWindow();
|
MainWindow startWindow = new MainWindow();
|
||||||
startWindow.Show();
|
startWindow.Show();
|
||||||
this.Close();
|
this.Close();
|
||||||
|
|||||||
@@ -32,8 +32,18 @@ namespace Client
|
|||||||
|
|
||||||
private void Button_Click(object sender, RoutedEventArgs e)
|
private void Button_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
Lobby lobbySelected = LobbyList.SelectedItem as Lobby;
|
||||||
|
if(lobbySelected != null)
|
||||||
|
{
|
||||||
|
testLabel.Content = lobbySelected.ID;
|
||||||
|
colorSelection.IsEnabled = false;
|
||||||
|
joinButton.IsEnabled = false;
|
||||||
|
hostButton.IsEnabled = false;
|
||||||
|
|
||||||
|
GameWindow window = new GameWindow();
|
||||||
|
window.Show();
|
||||||
|
Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
|
|
||||||
using Client;
|
using Newtonsoft.Json.Linq;
|
||||||
using SharedClientServer;
|
using SharedClientServer;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using static SharedClientServer.JSONConvert;
|
|
||||||
|
|
||||||
namespace Server.Models
|
namespace Server.Models
|
||||||
{
|
{
|
||||||
@@ -18,6 +17,7 @@ namespace Server.Models
|
|||||||
private byte[] totalBuffer = new byte[1024];
|
private byte[] totalBuffer = new byte[1024];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public User User { get; set; }
|
public User User { get; set; }
|
||||||
|
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -118,13 +118,15 @@ 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);
|
||||||
|
|
||||||
// todo handle sending to all except this user the username and message to display in chat
|
// todo handle sending to all except this user the username and message to display in chat
|
||||||
|
serverCom.SendToLobby(User.Lobby,payload);
|
||||||
|
Debug.WriteLine("Payload has been sent!");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
|
||||||
handleLobbyMessage(payload,l);
|
|
||||||
break;
|
break;
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
// canvas data
|
// canvas data
|
||||||
@@ -134,30 +136,7 @@ namespace Server.Models
|
|||||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
//TODO implement ways to handle the message
|
||||||
|
|
||||||
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
|
|
||||||
{
|
|
||||||
switch (l)
|
|
||||||
{
|
|
||||||
case LobbyIdentifier.REQUEST:
|
|
||||||
Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies...");
|
|
||||||
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
|
||||||
break;
|
|
||||||
case LobbyIdentifier.HOST:
|
|
||||||
// add new lobby and add this serverclient to it
|
|
||||||
int createdLobbyID = ServerCommunication.INSTANCE.HostForLobby(this.User);
|
|
||||||
Debug.WriteLine("[SERVERCLIENT] created lobby");
|
|
||||||
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(createdLobbyID));
|
|
||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
|
||||||
break;
|
|
||||||
case LobbyIdentifier.JOIN:
|
|
||||||
int id = JSONConvert.GetLobbyID(payload);
|
|
||||||
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
|
|
||||||
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
|
|
||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
|
using Client;
|
||||||
using Client;
|
|
||||||
using SharedClientServer;
|
using SharedClientServer;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -33,10 +32,7 @@ namespace Server.Models
|
|||||||
listener = new TcpListener(IPAddress.Any, port);
|
listener = new TcpListener(IPAddress.Any, port);
|
||||||
serverClients = new List<ServerClient>();
|
serverClients = new List<ServerClient>();
|
||||||
lobbies = new List<Lobby>();
|
lobbies = new List<Lobby>();
|
||||||
Lobby temp = new Lobby(1, 7, 8);
|
|
||||||
lobbies.Add(temp);
|
|
||||||
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
||||||
serverClientsInlobbies.Add(temp, new List<ServerClient>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -121,18 +117,6 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Lobby GetLobbyForUser(User user)
|
|
||||||
{
|
|
||||||
foreach (Lobby l in lobbies)
|
|
||||||
{
|
|
||||||
if (l.Users.Contains(user))
|
|
||||||
{
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddToLobby(Lobby lobby, User user)
|
public void AddToLobby(Lobby lobby, User user)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -141,7 +125,6 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
bool succ;
|
bool succ;
|
||||||
l.AddUser(user, out succ);
|
l.AddUser(user, out succ);
|
||||||
Debug.WriteLine("[SERVERCOMM] added user to lobby, now contains " + l.PlayersIn);
|
|
||||||
if (!succ)
|
if (!succ)
|
||||||
{
|
{
|
||||||
// TODO send lobby full message
|
// TODO send lobby full message
|
||||||
@@ -161,28 +144,5 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int HostForLobby(User user)
|
|
||||||
{
|
|
||||||
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
|
||||||
lobbies.Add(lobby);
|
|
||||||
serverClientsInlobbies.Add(lobby, new List<ServerClient>());
|
|
||||||
user.Host = true;
|
|
||||||
AddToLobby(lobby, user);
|
|
||||||
return lobby.ID;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void JoinLobby(User user, int id)
|
|
||||||
{
|
|
||||||
foreach (Lobby l in lobbies)
|
|
||||||
{
|
|
||||||
if (l.ID == id)
|
|
||||||
{
|
|
||||||
AddToLobby(l, user);
|
|
||||||
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,5 @@ namespace SharedClientServer
|
|||||||
Array.Copy(stringAsBytes, 0, res, 1, stringAsBytes.Length);
|
Array.Copy(stringAsBytes, 0, res, 1, stringAsBytes.Length);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
using Client;
|
using Client;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
@@ -16,13 +14,11 @@ namespace SharedClientServer
|
|||||||
public const byte LOBBY = 0x03;
|
public const byte LOBBY = 0x03;
|
||||||
public const byte CANVAS = 0x04;
|
public const byte CANVAS = 0x04;
|
||||||
|
|
||||||
public enum LobbyIdentifier
|
enum LobbyIdentifier
|
||||||
{
|
{
|
||||||
HOST,
|
HOST,
|
||||||
JOIN,
|
ADD,
|
||||||
JOIN_SUCCESS,
|
|
||||||
LEAVE,
|
LEAVE,
|
||||||
LIST,
|
|
||||||
REQUEST
|
REQUEST
|
||||||
}
|
}
|
||||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||||
@@ -47,98 +43,11 @@ namespace SharedClientServer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#region lobby messages
|
public static byte[] ConstructLobbyDataMessage(Lobby lobby)
|
||||||
|
|
||||||
public static byte[] ConstructLobbyHostMessage()
|
|
||||||
{
|
{
|
||||||
return GetMessageToSend(LOBBY, new
|
return null;
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.HOST
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new
|
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.HOST,
|
|
||||||
id = lobbyID
|
|
||||||
}) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyRequestMessage()
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new
|
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.REQUEST
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new
|
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.LIST,
|
|
||||||
lobbies = lobbiesList
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new
|
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.JOIN,
|
|
||||||
id = lobbyID
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyLeaveMessage(int lobbyID)
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new
|
|
||||||
{
|
|
||||||
identifier = LobbyIdentifier.LEAVE,
|
|
||||||
id = lobbyID
|
|
||||||
});
|
|
||||||
}
|
|
||||||
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
|
|
||||||
{
|
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
|
||||||
return payload.identifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Lobby[] GetLobbiesFromMessage(byte[] json)
|
|
||||||
{
|
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
|
||||||
JArray lobbiesArray = payload.lobbies;
|
|
||||||
Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString());
|
|
||||||
Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>();
|
|
||||||
Debug.WriteLine("lobbies in array: ");
|
|
||||||
foreach (Lobby l in lobbiesTemp)
|
|
||||||
{
|
|
||||||
Debug.WriteLine("players: " + l.PlayersIn);
|
|
||||||
}
|
|
||||||
return lobbiesTemp;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int GetLobbyID(byte[] json)
|
|
||||||
{
|
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
|
||||||
return payload.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Lobby GetLobby(byte[] json)
|
|
||||||
{
|
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
|
||||||
JObject dynamicAsObject = payload.lobby;
|
|
||||||
return dynamicAsObject.ToObject<Lobby>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyJoinSuccessMessage()
|
|
||||||
{
|
|
||||||
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
|
|
||||||
}
|
|
||||||
|
|
||||||
#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
|
||||||
@@ -160,7 +69,5 @@ namespace SharedClientServer
|
|||||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,29 +64,16 @@ namespace Client
|
|||||||
|
|
||||||
public int PlayersIn
|
public int PlayersIn
|
||||||
{
|
{
|
||||||
get { return _users.Count; }
|
get { return _playersIn; }
|
||||||
set { _playersIn = value; }
|
set { _playersIn = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set(Lobby lobby)
|
|
||||||
{
|
|
||||||
this._id = lobby._id;
|
|
||||||
this._users = lobby._users;
|
|
||||||
this._maxPlayers = lobby._maxPlayers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int MaxPlayers
|
public int MaxPlayers
|
||||||
{
|
{
|
||||||
get { return _maxPlayers; }
|
get { return _maxPlayers; }
|
||||||
set { _maxPlayers = value; }
|
set { _maxPlayers = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<User> Users
|
|
||||||
{
|
|
||||||
get { return _users; }
|
|
||||||
set { _users = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Newtonsoft.Json;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -10,8 +9,8 @@ namespace SharedClientServer
|
|||||||
private string _username;
|
private string _username;
|
||||||
private int _score;
|
private int _score;
|
||||||
private bool _host;
|
private bool _host;
|
||||||
|
private string _message;
|
||||||
|
|
||||||
[JsonConstructor]
|
|
||||||
public User(string username, int score, bool host)
|
public User(string username, int score, bool host)
|
||||||
{
|
{
|
||||||
_username = username;
|
_username = username;
|
||||||
|
|||||||
Reference in New Issue
Block a user