[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)
|
||||
|
||||
@@ -146,16 +146,16 @@ namespace Server.Models
|
||||
break;
|
||||
case LobbyIdentifier.HOST:
|
||||
// add new lobby and add this serverclient to it
|
||||
Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User);
|
||||
int createdLobbyID = ServerCommunication.INSTANCE.HostForLobby(this.User);
|
||||
Debug.WriteLine("[SERVERCLIENT] created lobby");
|
||||
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created));
|
||||
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
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());
|
||||
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace Server.Models
|
||||
listener = new TcpListener(IPAddress.Any, port);
|
||||
serverClients = new List<ServerClient>();
|
||||
lobbies = new List<Lobby>();
|
||||
Lobby temp = new Lobby(1, 1, 8);
|
||||
Lobby temp = new Lobby(1, 7, 8);
|
||||
lobbies.Add(temp);
|
||||
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
||||
serverClientsInlobbies.Add(temp, new List<ServerClient>());
|
||||
@@ -150,14 +150,14 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public Lobby HostForLobby(User user)
|
||||
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;
|
||||
return lobby.ID;
|
||||
}
|
||||
|
||||
public void JoinLobby(User user, int id)
|
||||
|
||||
@@ -57,12 +57,12 @@ namespace SharedClientServer
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] ConstructLobbyHostCreatedMessage(Lobby l)
|
||||
public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
|
||||
{
|
||||
return GetMessageToSend(LOBBY, new
|
||||
{
|
||||
identifier = LobbyIdentifier.HOST,
|
||||
lobby = l
|
||||
id = lobbyID
|
||||
}) ;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user