[FIX] fixed showing of players in lobbies when joining and hosting
This commit is contained in:
@@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public delegate void OnLobbyCreated(int id, int playersIn, int playersMax);
|
||||
public delegate void OnLobbyCreated(int id);
|
||||
class Client : ObservableObject
|
||||
{
|
||||
private TcpClient tcpClient;
|
||||
@@ -22,6 +22,7 @@ namespace Client
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public Callback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public OnLobbyCreated OnLobbyCreated;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
@@ -102,13 +103,14 @@ namespace Client
|
||||
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 the new lobby and the id
|
||||
// TODO get lobby id
|
||||
Debug.WriteLine("[CLIENT] got lobby object");
|
||||
Lobby lobby = JSONConvert.GetLobby(payload);
|
||||
OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers);
|
||||
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
|
||||
OnLobbyCreated?.Invoke(lobbyCreatedID);
|
||||
break;
|
||||
case LobbyIdentifier.JOIN_SUCCESS:
|
||||
OnLobbyJoinSuccess?.Invoke();
|
||||
|
||||
@@ -10,6 +10,7 @@ using System.Windows;
|
||||
using System.Collections.ObjectModel;
|
||||
using Client.Views;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -24,6 +25,9 @@ namespace Client
|
||||
|
||||
private Client client;
|
||||
|
||||
private bool wantToBeHost = false;
|
||||
private int wantToBeHostId = 0;
|
||||
|
||||
public ViewModel()
|
||||
{
|
||||
_model = new Model();
|
||||
@@ -44,21 +48,40 @@ namespace Client
|
||||
client.OnLobbyCreated = becomeHostForLobby;
|
||||
}
|
||||
|
||||
private void becomeHostForLobby(int id, int players, int maxplayers)
|
||||
private void becomeHostForLobby(int id)
|
||||
{
|
||||
|
||||
Debug.WriteLine($"got host succes with data {id} {players} {maxplayers} ");
|
||||
Lobby newLobby = new Lobby(id, players, maxplayers);
|
||||
SelectedLobby = newLobby;
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
{
|
||||
_lobbies.Add(newLobby);
|
||||
startGameInLobby();
|
||||
});
|
||||
Debug.WriteLine($"got host succes with data {id} ");
|
||||
wantToBeHost = true;
|
||||
wantToBeHostId = id;
|
||||
client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
|
||||
// lobby id krijgen waarvan je host bent
|
||||
// wachten totdat list van lobbies is gekomen
|
||||
// uit die list de id zoeken en die joinen
|
||||
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
@@ -77,25 +100,29 @@ namespace Client
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
{
|
||||
|
||||
for (int i = 0; i < lobbiesArr.Length; i++)
|
||||
//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)
|
||||
{
|
||||
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.Add(l);
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void startGameInLobby()
|
||||
{
|
||||
if (SelectedLobby != null)
|
||||
|
||||
Reference in New Issue
Block a user