[ADD] added joining lobbies

This commit is contained in:
Sem van der Hoeven
2020-10-20 22:49:25 +02:00
parent 7b30911cd7
commit 76cd392525
7 changed files with 72 additions and 32 deletions

View File

@@ -21,6 +21,7 @@ namespace Client
private string username; private string username;
public Callback OnSuccessfullConnect; public Callback OnSuccessfullConnect;
public Callback OnLobbiesListReceived; public Callback OnLobbiesListReceived;
public Callback OnLobbyJoinSuccess;
public OnLobbyCreated OnLobbyCreated; public OnLobbyCreated OnLobbyCreated;
public Lobby[] Lobbies { get; set; } public Lobby[] Lobbies { get; set; }
@@ -107,7 +108,9 @@ namespace Client
Debug.WriteLine("[CLIENT] got lobby object"); Debug.WriteLine("[CLIENT] got lobby object");
Lobby lobby = JSONConvert.GetLobby(payload); Lobby lobby = JSONConvert.GetLobby(payload);
OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers); OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers);
break;
case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke();
break; break;
} }
//TODO fill lobby with the data received //TODO fill lobby with the data received

View File

@@ -34,7 +34,7 @@ namespace Client
OnHostButtonClick = new RelayCommand(hostGame); OnHostButtonClick = new RelayCommand(hostGame);
JoinSelectedLobby = new RelayCommand(startGameInLobby, true); JoinSelectedLobby = new RelayCommand(joinLobby, true);
} }
private void hostGame() private void hostGame()
@@ -57,6 +57,17 @@ namespace Client
}); });
} }
private void joinLobby()
{
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
}
private void OnLobbyJoinSuccess()
{
startGameInLobby();
}
private void updateLobbies() private void updateLobbies()
@@ -85,8 +96,11 @@ namespace Client
private void startGameWindow() private void startGameWindow()
{ {
_model.CanStartGame = false; _model.CanStartGame = false;
GameWindow window = new GameWindow(); Application.Current.Dispatcher.Invoke(delegate
window.Show(); {
GameWindow window = new GameWindow();
window.Show();
});
} }
private void ClickCheck() private void ClickCheck()

View File

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

View File

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

View File

@@ -124,19 +124,7 @@ namespace Server.Models
case JSONConvert.LOBBY: case JSONConvert.LOBBY:
// lobby data // lobby data
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
switch(l) handleLobbyMessage(payload,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
Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User);
Debug.WriteLine("[SERVERCLIENT] created lobby");
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created));
break;
}
break; break;
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
// canvas data // canvas data
@@ -148,6 +136,28 @@ namespace Server.Models
} }
} }
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
Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User);
Debug.WriteLine("[SERVERCLIENT] created lobby");
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created));
break;
case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
break;
}
}
/// <summary> /// <summary>
/// sends a message to the tcp client /// sends a message to the tcp client
/// </summary> /// </summary>

View File

@@ -33,10 +33,10 @@ 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>();
lobbies.Add(new Lobby(1,1,1)); Lobby temp = new Lobby(1, 1, 8);
lobbies.Add(new Lobby(2, 2, 2)); lobbies.Add(temp);
lobbies.Add(new Lobby(3, 3, 3));
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>(); serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
serverClientsInlobbies.Add(temp, new List<ServerClient>());
} }
/// <summary> /// <summary>
@@ -157,5 +157,18 @@ namespace Server.Models
AddToLobby(lobby, user); AddToLobby(lobby, user);
return lobby; return lobby;
} }
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;
}
}
}
} }
} }

View File

@@ -20,6 +20,7 @@ namespace SharedClientServer
{ {
HOST, HOST,
JOIN, JOIN,
JOIN_SUCCESS,
LEAVE, LEAVE,
LIST, LIST,
REQUEST REQUEST
@@ -126,6 +127,11 @@ namespace SharedClientServer
return dynamicAsObject.ToObject<Lobby>(); return dynamicAsObject.ToObject<Lobby>();
} }
public static byte[] ConstructLobbyJoinSuccessMessage()
{
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
}
#endregion #endregion
/// <summary> /// <summary>