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

View File

@@ -34,7 +34,7 @@ namespace Client
OnHostButtonClick = new RelayCommand(hostGame);
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
JoinSelectedLobby = new RelayCommand(joinLobby, true);
}
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()
@@ -85,8 +96,11 @@ namespace Client
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

@@ -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();
}
}
}
}

View File

@@ -124,7 +124,21 @@ namespace Server.Models
case JSONConvert.LOBBY:
// lobby data
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
switch(l)
handleLobbyMessage(payload,l);
break;
case JSONConvert.CANVAS:
// canvas data
// todo send canvas data to all other serverclients in lobby
break;
default:
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
break;
}
}
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
{
switch (l)
{
case LobbyIdentifier.REQUEST:
Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies...");
@@ -136,14 +150,10 @@ namespace Server.Models
Debug.WriteLine("[SERVERCLIENT] created lobby");
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created));
break;
}
break;
case JSONConvert.CANVAS:
// canvas data
// todo send canvas data to all other serverclients in lobby
break;
default:
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
break;
}
}

View File

@@ -33,10 +33,10 @@ namespace Server.Models
listener = new TcpListener(IPAddress.Any, port);
serverClients = new List<ServerClient>();
lobbies = new List<Lobby>();
lobbies.Add(new Lobby(1,1,1));
lobbies.Add(new Lobby(2, 2, 2));
lobbies.Add(new Lobby(3, 3, 3));
Lobby temp = new Lobby(1, 1, 8);
lobbies.Add(temp);
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
serverClientsInlobbies.Add(temp, new List<ServerClient>());
}
/// <summary>
@@ -157,5 +157,18 @@ namespace Server.Models
AddToLobby(lobby, user);
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,
JOIN,
JOIN_SUCCESS,
LEAVE,
LIST,
REQUEST
@@ -126,6 +127,11 @@ namespace SharedClientServer
return dynamicAsObject.ToObject<Lobby>();
}
public static byte[] ConstructLobbyJoinSuccessMessage()
{
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS});
}
#endregion
/// <summary>