private void ButtonJoinClick(object sender, RoutedEventArgs e) { if (WindowManager.PlayWindow != null) { MessageBox.Show( "You are currently in a game or game lobby. Please leave before you join game.", "OCTGN", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var hostedgame = ListViewGameList.SelectedItem as HostedGameViewModel; if (hostedgame == null) { return; } if (hostedgame.Status == EHostedGame.GameInProgress && hostedgame.Spectator == false) { TopMostMessageBox.Show( "You can't join a game in progress.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (hostedgame.GameSource == "Online") { var client = new Octgn.Site.Api.ApiClient(); if (!client.IsGameServerRunning(Program.LobbyClient.Username, Program.LobbyClient.Password)) { TopMostMessageBox.Show( "The game server is currently down. Please try again later.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } } var game = GameManager.Get().GetById(hostedgame.GameId); if (game == null) { TopMostMessageBox.Show("You don't currently have that game installed.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } var spectate = hostedgame.Status == EHostedGame.GameInProgress && hostedgame.Spectator == true; var task = new Task(() => this.StartJoinGame(hostedgame, game, spectate)); task.ContinueWith((t) => { this.Dispatcher.Invoke(new Action(() => this.FinishJoinGame(t))); }); BorderButtons.IsEnabled = false; task.Start(); }
private void StartJoinGame(HostedGameViewModel hostedGame, DataNew.Entities.Game game, bool spectate) { if (hostedGame.GameSource == "Online") { var client = new Octgn.Site.Api.ApiClient(); if (!client.IsGameServerRunning(Program.LobbyClient.Username, Program.LobbyClient.Password)) { throw new UserMessageException("The game server is currently down. Please try again later."); } } Log.InfoFormat("Starting to join a game {0} {1}", hostedGame.GameId, hostedGame.Name); Program.IsHost = false; Program.IsMatchmaking = false; var password = ""; if (hostedGame.HasPassword) { Dispatcher.Invoke(new Action(() => { var dlg = new InputDlg("Password", "Please enter this games password", ""); password = dlg.GetString(); })); } var username = (Program.LobbyClient.IsConnected == false || Program.LobbyClient.Me == null || Program.LobbyClient.Me.UserName == null) ? Prefs.Nickname : Program.LobbyClient.Me.UserName; Program.GameEngine = new GameEngine(game, username, spectate, password); Program.CurrentOnlineGameName = hostedGame.Name; IPAddress hostAddress = hostedGame.IPAddress; if (hostAddress == null) { Log.WarnFormat("Dns Error, couldn't resolve {0}", AppConfig.GameServerPath); throw new UserMessageException("There was a problem with your DNS. Please try again."); } try { Log.InfoFormat("Creating client for {0}:{1}", hostAddress, hostedGame.Port); Program.Client = new ClientSocket(hostAddress, hostedGame.Port); Log.InfoFormat("Connecting client for {0}:{1}", hostAddress, hostedGame.Port); Program.Client.Connect(); } catch (Exception e) { Log.Warn("Start join game error ", e); throw new UserMessageException("Could not connect. Please try again."); } }
private void ButtonJoinClick(object sender, RoutedEventArgs e) { if (Program.IsInMatchmakingQueue) { MessageBox.Show( "You are currently matchmaking. Please leave before you join game.", "OCTGN", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (WindowManager.PlayWindow != null) { MessageBox.Show( "You are currently in a game or game lobby. Please leave before you join game.", "OCTGN", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var hostedgame = ListViewGameList.SelectedItem as HostedGameViewModel; if (hostedgame == null) return; if (hostedgame.Status == EHostedGame.GameInProgress && hostedgame.Spectator == false) { TopMostMessageBox.Show( "You can't join a game in progress.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (hostedgame.GameSource == "Online") { var client = new Octgn.Site.Api.ApiClient(); if (!client.IsGameServerRunning(Program.LobbyClient.Username, Program.LobbyClient.Password)) { TopMostMessageBox.Show( "The game server is currently down. Please try again later.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } } var game = GameManager.Get().GetById(hostedgame.GameId); if (game == null) { TopMostMessageBox.Show("You don't currently have that game installed.", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } var spectate = hostedgame.Status == EHostedGame.GameInProgress && hostedgame.Spectator == true; var task = new Task(() => this.StartJoinGame(hostedgame, game, spectate)); task.ContinueWith((t) => { this.Dispatcher.Invoke(new Action(() => this.FinishJoinGame(t))); }); BorderButtons.IsEnabled = false; task.Start(); }