[ADD] added hosting a lobby

This commit is contained in:
Sem van der Hoeven
2020-10-20 22:16:19 +02:00
parent 671951c35b
commit 7b30911cd7
6 changed files with 93 additions and 17 deletions

View File

@@ -8,6 +8,7 @@ using static SharedClientServer.JSONConvert;
namespace Client
{
public delegate void OnLobbyCreated(int id, int playersIn, int playersMax);
class Client : ObservableObject
{
private TcpClient tcpClient;
@@ -20,6 +21,7 @@ namespace Client
private string username;
public Callback OnSuccessfullConnect;
public Callback OnLobbiesListReceived;
public OnLobbyCreated OnLobbyCreated;
public Lobby[] Lobbies { get; set; }
public Client(string username)
@@ -98,6 +100,14 @@ namespace Client
case LobbyIdentifier.LIST:
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
OnLobbiesListReceived?.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
Debug.WriteLine("[CLIENT] got lobby object");
Lobby lobby = JSONConvert.GetLobby(payload);
OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers);
break;
}
//TODO fill lobby with the data received

View File

@@ -30,18 +30,35 @@ namespace Client
_lobbies = new ObservableCollection<Lobby>();
client = ClientData.Instance.Client;
client.OnLobbiesListReceived = updateLobbies;
//_lobbies.Add(new Lobby(50, 3, 8));
//_lobbies.Add(new Lobby(69, 1, 9));
//_lobbies.Add(new Lobby(420, 7, 7));
OnHostButtonClick = new RelayCommand(() =>
{
Debug.WriteLine("Host button clicked");
});
OnHostButtonClick = new RelayCommand(hostGame);
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
}
private void hostGame()
{
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
client.SendMessage(JSONConvert.ConstructLobbyHostMessage());
client.OnLobbyCreated = becomeHostForLobby;
}
private void becomeHostForLobby(int id, int players, int maxplayers)
{
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();
});
}
private void updateLobbies()
{
Lobby[] lobbiesArr = client.Lobbies;
@@ -61,13 +78,17 @@ namespace Client
if (SelectedLobby != null)
{
ClientData.Instance.Lobby = SelectedLobby;
_model.CanStartGame = false;
GameWindow window = new GameWindow();
window.Show();
startGameWindow();
}
}
private void startGameWindow()
{
_model.CanStartGame = false;
GameWindow window = new GameWindow();
window.Show();
}
private void ClickCheck()
{
if(!(_model.Status))