[FIX] fixed showing of players in lobbies when joining and hosting

This commit is contained in:
Sem van der Hoeven
2020-10-21 20:34:11 +02:00
parent fc5d51a876
commit 3a13314519
5 changed files with 64 additions and 35 deletions

View File

@@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert;
namespace Client namespace Client
{ {
public delegate void OnLobbyCreated(int id, int playersIn, int playersMax); public delegate void OnLobbyCreated(int id);
class Client : ObservableObject class Client : ObservableObject
{ {
private TcpClient tcpClient; private TcpClient tcpClient;
@@ -22,6 +22,7 @@ namespace Client
public Callback OnSuccessfullConnect; public Callback OnSuccessfullConnect;
public Callback OnLobbiesListReceived; public Callback OnLobbiesListReceived;
public Callback OnLobbyJoinSuccess; public Callback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost;
public OnLobbyCreated OnLobbyCreated; public OnLobbyCreated OnLobbyCreated;
public Lobby[] Lobbies { get; set; } public Lobby[] Lobbies { get; set; }
@@ -102,13 +103,14 @@ namespace Client
Debug.WriteLine("got lobbies list"); Debug.WriteLine("got lobbies list");
Lobbies = JSONConvert.GetLobbiesFromMessage(payload); Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
OnLobbiesListReceived?.Invoke(); OnLobbiesListReceived?.Invoke();
OnLobbiesReceivedAndWaitingForHost?.Invoke();
break; break;
case LobbyIdentifier.HOST: case LobbyIdentifier.HOST:
// we receive this when the server has made us a host of a new lobby // 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"); Debug.WriteLine("[CLIENT] got lobby object");
Lobby lobby = JSONConvert.GetLobby(payload); int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers); OnLobbyCreated?.Invoke(lobbyCreatedID);
break; break;
case LobbyIdentifier.JOIN_SUCCESS: case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke(); OnLobbyJoinSuccess?.Invoke();

View File

@@ -10,6 +10,7 @@ using System.Windows;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using Client.Views; using Client.Views;
using System.Linq; using System.Linq;
using System.Windows.Data;
namespace Client namespace Client
{ {
@@ -24,6 +25,9 @@ namespace Client
private Client client; private Client client;
private bool wantToBeHost = false;
private int wantToBeHostId = 0;
public ViewModel() public ViewModel()
{ {
_model = new Model(); _model = new Model();
@@ -44,21 +48,40 @@ namespace Client
client.OnLobbyCreated = becomeHostForLobby; 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} "); Debug.WriteLine($"got host succes with data {id} ");
Lobby newLobby = new Lobby(id, players, maxplayers); wantToBeHost = true;
SelectedLobby = newLobby; wantToBeHostId = id;
Application.Current.Dispatcher.Invoke(delegate client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
{ // lobby id krijgen waarvan je host bent
_lobbies.Add(newLobby); // wachten totdat list van lobbies is gekomen
startGameInLobby(); // 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() private void joinLobby()
{ {
// lobby die je wilt joinen verwijderen
// nieuwe binnengekregen lobby toevoegen
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
} }
@@ -77,25 +100,29 @@ namespace Client
Application.Current.Dispatcher.Invoke(delegate 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]; _lobbies.Add(l);
Debug.WriteLine(lobby.PlayersIn);
if (i < _lobbies.Count && _lobbies[i].ID == lobby.ID)
{
_lobbies[i].Set(lobby);
} else
{
_lobbies.Add(lobbiesArr[i]);
}
} }
}); });
} }
private void startGameInLobby() private void startGameInLobby()
{ {
if (SelectedLobby != null) if (SelectedLobby != null)

View File

@@ -146,16 +146,16 @@ namespace Server.Models
break; break;
case LobbyIdentifier.HOST: case LobbyIdentifier.HOST:
// add new lobby and add this serverclient to it // 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"); Debug.WriteLine("[SERVERCLIENT] created lobby");
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created)); sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(createdLobbyID));
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break; break;
case LobbyIdentifier.JOIN: case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload); int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User,id); ServerCommunication.INSTANCE.JoinLobby(this.User,id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break; break;
} }
} }

View File

@@ -33,7 +33,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, 1, 8); Lobby temp = new Lobby(1, 7, 8);
lobbies.Add(temp); lobbies.Add(temp);
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>(); serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
serverClientsInlobbies.Add(temp, new 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); Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
lobbies.Add(lobby); lobbies.Add(lobby);
serverClientsInlobbies.Add(lobby, new List<ServerClient>()); serverClientsInlobbies.Add(lobby, new List<ServerClient>());
user.Host = true; user.Host = true;
AddToLobby(lobby, user); AddToLobby(lobby, user);
return lobby; return lobby.ID;
} }
public void JoinLobby(User user, int id) public void JoinLobby(User user, int id)

View File

@@ -57,12 +57,12 @@ namespace SharedClientServer
}); });
} }
public static byte[] ConstructLobbyHostCreatedMessage(Lobby l) public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID)
{ {
return GetMessageToSend(LOBBY, new return GetMessageToSend(LOBBY, new
{ {
identifier = LobbyIdentifier.HOST, identifier = LobbyIdentifier.HOST,
lobby = l id = lobbyID
}) ; }) ;
} }