Android गेम में दोस्त

इस गाइड में, दोस्तों के एपीआई का इस्तेमाल करने का तरीका बताया गया है Android Studio के प्रोजेक्ट में.

दोस्तों की सूची लोड करना

मौजूदा उपयोगकर्ता के दोस्तों की सूची को वापस पाया जा सकता है और गेम में दिखाया जा सकता है. उपयोगकर्ता के तौर पर, यह कंट्रोल किया जा सकता है कि किन गेम के पास दोस्तों की सूची का ऐक्सेस हो. दोस्तों की सूची वापस पाने के लिए, आपको इस बात का ध्यान रखना होगा कि इसके लिए अनुमति की ज़रूरत हो सकती है. एपीआई में यह सब शामिल है, ताकि ऐक्सेस का अनुरोध करना और उसके बाद दोस्तों की सूची का इस्तेमाल करना आसान हो. दोस्तों की सूची लोड करने के लिए, यह तरीका अपनाएं:

  1. PlayersClient.loadFriends() तरीके को कॉल करें. यह एक एसिंक्रोनस कॉल है, जो Task ऑब्जेक्ट दिखाता है.
  2. अगर कॉल पूरा हो जाता है (उपयोगकर्ता ने पहले ही दोस्तों की सूची का ऐक्सेस दिया है), तो Google Play की गेम सेवाएं, एनोटेट किया गया PlayerBufferदिखाती हैं. यह उपयोगकर्ता के दोस्तों की सूची होती है.
  3. अगर खिलाड़ी को दोस्तों की सूची का ऐक्सेस देना है, तो कॉल a FriendsResolutionRequiredException के साथ फ़ेल हो जाता है. फ़िलहाल, कोई डायलॉग नहीं दिखते हैं.

    1. इस अपवाद में एक Intent शामिल होता है, जो खिलाड़ी से सहमति मांगने के लिए एक डायलॉग ट्रिगर करता है. सहमति संवाद खोलने के लिए, इस Intent को तुरंत लॉन्च किया जा सकता है. इस Intent का इस्तेमाल सिर्फ़ एक बार किया जा सकता है.
    2. अगर Intent की गतिविधि का नतीजा Activity.RESULT_OK है, तो सहमति मिल गई है. दोस्तों की सूची वापस पाने के लिए, loadFriends() को फिर से कॉल करें. अगर नतीजा Activity.RESULT_CANCELLED है, तो उपयोगकर्ता ने सहमति नहीं दी है और loadFriends() लगातार FriendsResolutionRequiredException दिखाएगा.

यहां दिए गए कोड से, दोस्तों की सूची लोड करने का तरीका पता चलता है:

// Attempt loading friends.
// Register a success listener to handle the successfully loaded friends list.
// Register a failure listener to handle asking for permission to access the list.
PlayGames.getPlayersClient(this)
    .loadFriends(PAGE_SIZE, /* forceReload= */ false)
    .addOnSuccessListener(
        new OnSuccessListener<AnnotatedData<PlayerBuffer>>() {
            @Override
            public void onSuccess(AnnotatedData<PlayerBuffer>  data) {
          PlayerBuffer playerBuffer = data.get();
          // ...
        })

    .addOnFailureListener(
        exception -> {
      if (exception instanceof FriendsResolutionRequiredException) {
        PendingIntent pendingIntent =
            ((FriendsResolutionRequiredException) task.getException())
            .getResolution();
        parentActivity.startIntentSenderForResult(
            pendingIntent.getIntentSender(),
            /* requestCode */ SHOW_SHARING_FRIENDS_CONSENT,
            /* fillInIntent */ null,
            /* flagsMask */ 0,
            /* flagsValues */ 0,
            /* extraFlags */ 0,
            /* options */ null);
     }
   });
 return;
}

यहां दिए गए कोड से, सहमति के अनुरोध के नतीजे को मैनेज करने का तरीका पता चलता है:

/** Handle the activity result from the request for consent. */
@Override
public void onActivityResult(int requestCode, int result, Intent data) {
  if (requestCode == SHOW_SHARING_FRIENDS_CONSENT) {
    if (result == Activity.RESULT_OK) {
      // We got consent from the user to access their friends. Retry loading the friends
      callLoadFriends();
    } else {
      // User did not grant consent.
    }
  }
}

किसी दूसरे खिलाड़ी की प्रोफ़ाइल देखना

अपने गेम में, किसी दूसरे खिलाड़ी की Play Games प्रोफ़ाइल का व्यू दिखाया जा सकता है. इस व्यू की मदद से, खिलाड़ी, देखी जा रही प्रोफ़ाइल वाले खिलाड़ी को दोस्तों की सूची में शामिल होने के न्योते भेज और स्वीकार कर सकते हैं. इस व्यू के लिए, दोस्तों की सूची के ऐक्सेस की ज़रूरत नहीं होती. इसके अलावा, अगर आपके गेम में खिलाड़ियों के नाम की अपनी अवधारणा है, जो Play Games के गेमर आईडी से अलग है, तो इन्हें प्रोफ़ाइल व्यू में पास किया जा सकता है. इससे, इन्हें दोस्तों की सूची में शामिल होने के न्योते में शामिल किया जा सकता है, ताकि ज़्यादा जानकारी दी जा सके.

किसी दूसरे खिलाड़ी की प्रोफ़ाइल दिखाने के लिए, यह तरीका अपनाएं:

  1. PlayersClient.getCompareProfileIntent() तरीके को कॉल करें. यह एक एसिंक्रोनस कॉल है, जो Task ऑब्जेक्ट दिखाता है.
  2. अगर कॉल पूरा हो जाता है, तो Google Play Games की सेवाएं एक Intent दिखाती हैं. इससे एक ऐसी स्क्रीन दिखेगी जहां उपयोगकर्ता, अपनी प्रोफ़ाइल की तुलना किसी दूसरे खिलाड़ी की प्रोफ़ाइल से कर सकता है.
  3. किसी गतिविधि को शुरू करने के लिए, पिछले चरण में मिले Intent का इस्तेमाल करें.
// Retrieve and launch an Intent to show a player profile within the game.
PlayGames.getPlayersClient(this)
    .getCompareProfileIntent(otherPlayerId)
    .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent  intent) {
          startActivityForResult(intent, RC_SHOW_PROFILE);
          // ...
        }});

अगर गेम में खिलाड़ियों के लिए अलग-अलग नाम हैं, तो इन्हें एपीआई कॉल में जोड़ा जा सकता है. इससे Play Games, आपके गेम से दोस्तों की सूची में शामिल होने के न्योते भेजने वाले खिलाड़ियों का निकनेम "<game-specific-name> from <your-game-name>" सेट कर सकता है. Play Games, "from <your-game-name>" को अपने-आप जोड़ता है:

// Show a player profile within the game, with additional hints containing the
// game-specific names for both players.
// - otherPlayerId is the Play Games playerId of the player to view.
// - otherPlayerInGameName is the game-specific name of the player being viewed.
// - currentPlayerInGameName is the game-specific name of the player who is authenticated.
//   Hence if the player sends an invitation to the profile they are viewing,
//   their game-specific name can be included.
PlayGames.PlayersClient(this)
    .getCompareProfileIntentWithAlternativeNameHints(otherPlayerId, otherPlayerInGameName, currentPlayerInGameName)
    .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent  intent) {
          startActivityForResult(intent, RC_SHOW_PROFILE);
          // ...
        }});