public LobbyViewModel(GTTcpClient client, PlayerListViewModel playerList) { SelectedGameStage = GameStage.First; _playerList = playerList; IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); HostIp = ipHostInfo.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).First().ToString(); ConnectInProgress = false; Port = DefaultPort; Ip = HostIp; _client = client; _client.BuildingBegun += Client_BuildingBegun; _playerList.LostConnection += PlayerList_LostConnection; HostCommand = new DelegateCommand(param => Server == null, param => { try { Server = new GTTcpListener(Port, SelectedGameStage); Task.Factory.StartNew(() => Server.Start(), TaskCreationOptions.RunContinuationsAsynchronously | TaskCreationOptions.LongRunning); Ip = "127.0.0.1"; ConnectCommand.Execute(null); } catch (SocketException) { Error = "Az adott port már használatban van!"; } }); ConnectCommand = new DelegateCommand(param => !ConnectInProgress && !IsConnected, param => Connect()); ReadyCommand = new DelegateCommand(param => { try { _client.ToggleReady(ServerStage.Lobby); } catch (Exception e) { Error = $"Hiba a szerverrel való kommunikáció közben:\n{e.Message}"; } }); BackToMenuCommand = new DelegateCommand(param => { UnsubscribeFromEvents(); BackToMenu?.Invoke(this, Server != null); }); StartBuildingCommand = new DelegateCommand(param => Server != null && !Server.NotReadyPlayers.Any() && _client.PlayerInfos.Count > 1, param => { Task.Factory.StartNew(() => Server.StartBuildStage(), TaskCreationOptions.LongRunning); }); }
private void LobbyViewModel_BuildingStarted(object sender, bool isHost) { if (isHost) { _listener = _lobbyViewModel.Server; } Dispatcher.Invoke(() => { _buildViewModel = new BuildViewModel(_client, _playerListViewModel, _lobbyViewModel.SelectedLayout); _buildViewModel.FlightBegun += BuildViewModel_FlightBegun; BuildControl buildControl = new BuildControl { DataContext = _buildViewModel }; _mainWindow.Content = buildControl; _mainWindow.WindowState = WindowState.Maximized; _mainWindow.WindowStyle = WindowStyle.None; }); }