/// <summary> /// Handle a player accepting our challenge. /// </summary> /// <param name="playerInfo">The player.</param> private void OnChallengeAccepted(PlayerInfo playerInfo) { _currentOpponent = playerInfo; DiagnosticsHelper.SafeShow(String.Format("Player '{0}' Accepted Your Challenge", playerInfo.PlayerName)); string uriString = String.Format("/Game.xaml?player={0}&opponent={1}", _playerName, playerInfo.PlayerName); (Application.Current as App).RootFrame.Navigate(new Uri(uriString, UriKind.RelativeOrAbsolute)); }
private void ChallengButton_Click(object sender, RoutedEventArgs e) { if (PlayersListBox.SelectedItem == null) { DiagnosticsHelper.SafeShow("Please select an opponent from the list"); } else { string opponentName = ((PlayerInfo)PlayersListBox.SelectedItem).PlayerName; App.GamePlay.Challenge(opponentName); } }
void GamePlay_LeftGame(object sender, PlayerEventArgs e) { // If the opponent leaves the game, back out of the game screen, since we will want // to choose another opponent. DiagnosticsHelper.SafeShow(String.Format("Player '{0}' has left the game", e.playerInfo.PlayerName)); // Opponent has left, so clear the opponent name _opponent = null; if (NavigationService.CanGoBack) { NavigationService.GoBack(); } }
/// <summary> /// Send a message to the given opponent to challenge him to a game. /// </summary> /// <param name="opponentName">The identifier for the opponent to challenge.</param> public void Challenge(string opponentName) { // Look for this opponent in the list of oppoennts in the group PlayerInfo opponent = App.Players.Where(player => player.PlayerName == opponentName).SingleOrDefault(); if (opponent != null) { this.Channel.SendTo(opponent.PlayerEndPoint, GameCommands.ChallengeFormat, _playerName); } else { DiagnosticsHelper.SafeShow("Opponent is null!"); } }
private void JoinButton_Click(object sender, RoutedEventArgs e) { if (String.IsNullOrWhiteSpace(UsernameTextBox.Text)) { // Display a message. Diagnostics.SafeShow is a wrapper that makes sure // we call the messagebox on the UI thread. DiagnosticsHelper.SafeShow("Please enter a username to join the game"); } else { // Make sure the name we chose shows up in the players list. App.Players.Add(new PlayerInfo(UsernameTextBox.Text)); // Go to the players list. NavigationService.Navigate(new Uri("/Players.xaml?player=" + UsernameTextBox.Text, UriKind.RelativeOrAbsolute)); } }
/// <summary> /// Sends the specified format. This is a unicast message, sent to the member of the multicast group at the given endPoint. /// </summary> /// /// <param name="format">The destination.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void SendTo(IPEndPoint endPoint, string format, params object[] args) { try { if (this.IsJoined) { string msg = string.Format(format, args); byte[] data = Encoding.UTF8.GetBytes(msg); this.Client.BeginSendTo(data, 0, data.Length, endPoint, new AsyncCallback(SendToCallback), null); } else { DiagnosticsHelper.SafeShow("Not joined!"); } } catch (SocketException socketEx) { // See if we can do something when a SocketException occurs. HandleSocketException(socketEx); } }
/// <summary> /// If a Socketexception occurs, it is possible to handle these exceptions gracefully. /// </summary> /// <remarks> /// This method contains examples of what you can do when a SocketException occurs. /// However, it is not exhaustive and you should handle these exceptions according /// to your applications specific behavior. /// </remarks> /// <param name="socketEx"></param> private void HandleSocketException(SocketException socketEx) { if (socketEx.SocketErrorCode == SocketError.NetworkDown) { DiagnosticsHelper.SafeShow("A SocketExeption has occurred. Please make sure your device is on a Wi-Fi network and the Wi-Fi network is operational"); } else if (socketEx.SocketErrorCode == SocketError.ConnectionReset) { // Try to re-join the multi-cast group. // No retry count has been implemented here. This is left as an exercise. this.IsJoined = false; this.Open(); } else if (socketEx.SocketErrorCode == SocketError.AccessDenied) { DiagnosticsHelper.SafeShow("An error occurred. Try Again."); } else { // Just display the message. DiagnosticsHelper.SafeShow(socketEx.Message); } }
/// <summary> /// Handle a player rejecting our challenge. /// </summary> /// <param name="playerInfo">The player.</param> private void OnChallengeRejected(PlayerInfo playerInfo) { _currentOpponent = null; DiagnosticsHelper.SafeShow(String.Format("Player '{0}' Rejected Your Challenge", playerInfo.PlayerName)); }