Merge branch 'lobbies' into setupBranch

This commit is contained in:
Sem van der Hoeven
2020-10-21 20:44:11 +02:00
10 changed files with 316 additions and 32 deletions

View File

@@ -4,9 +4,11 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using static SharedClientServer.JSONConvert;
namespace Client
{
public delegate void OnLobbyCreated(int id);
class Client : ObservableObject
{
private TcpClient tcpClient;
@@ -18,6 +20,11 @@ namespace Client
public bool Connected = false;
private string username;
public Callback OnSuccessfullConnect;
public Callback OnLobbiesListReceived;
public Callback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost;
public OnLobbyCreated OnLobbyCreated;
public Lobby[] Lobbies { get; set; }
public Client(string username)
{
@@ -70,9 +77,9 @@ namespace Client
private void handleData(byte[] message)
{
byte id = message[0];
byte[] payload = new byte[message.Length - 1];
Array.Copy(message, 1, payload, 0, message.Length - 1);
byte id = message[4];
byte[] payload = new byte[message.Length - 5];
Array.Copy(message, 5, payload, 0, message.Length - 5);
switch (id)
{
case JSONConvert.LOGIN:
@@ -89,6 +96,26 @@ namespace Client
case JSONConvert.LOBBY:
// 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
break;
case JSONConvert.CANVAS:

View File

@@ -9,6 +9,8 @@ using System.Diagnostics;
using System.Windows;
using System.Collections.ObjectModel;
using Client.Views;
using System.Linq;
using System.Windows.Data;
namespace Client
{
@@ -21,21 +23,101 @@ namespace Client
public Lobby SelectedLobby { get; set; }
private Client client;
private bool wantToBeHost = false;
private int wantToBeHostId = 0;
public ViewModel()
{
_model = new Model();
_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]);
// }
//}
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
_lobbies.Clear();
foreach (Lobby l in lobbiesArr)
{
_lobbies.Add(l);
}
});
}
private void startGameInLobby()
@@ -43,11 +125,18 @@ namespace Client
if (SelectedLobby != null)
{
ClientData.Instance.Lobby = SelectedLobby;
_model.CanStartGame = false;
startGameWindow();
}
}
private void startGameWindow()
{
_model.CanStartGame = false;
Application.Current.Dispatcher.Invoke(delegate
{
GameWindow window = new GameWindow();
window.Show();
}
});
}
private void ClickCheck()

View File

@@ -113,5 +113,10 @@ namespace Client.Views
string user = data.User.Username;
SentMessage.AppendText($"{user}: {message}\n");
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
}

View File

@@ -34,6 +34,7 @@ namespace Client.Views
Application.Current.Dispatcher.Invoke(delegate {
data.User = user;
data.Client = client;
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
MainWindow startWindow = new MainWindow();
startWindow.Show();
this.Close();

View File

@@ -33,18 +33,7 @@ namespace Client
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();
}
}
}
}