Esempio n. 1
0
 public UnityTask <UnlinkFacebookAccountResult> UnlinkFacebook()
 {
     return(PF.Do <UnlinkFacebookAccountRequest, UnlinkFacebookAccountResult>(new UnlinkFacebookAccountRequest(), PlayFabClientAPI.UnlinkFacebookAccount));
 }
 public UnityTask <GetUserInventoryResult> RefreshVirtualCurrency()
 {
     return(PF.Do(new GetUserInventoryRequest()));
 }
        private IEnumerator RefreshLeaderboardCoroutine(int?startPosition)
        {
            // Setting any profile constraints
            this.profileContraints.ShowDisplayName = true;
            this.profileContraints.ShowAvatarUrl   = this.shouldShowAvatarUrl;

            if (startPosition.HasValue == false)
            {
                this.moreBottomEntriesExist = true;
                this.moreTopEntiresExist    = true;
            }

            if (this.isFriendLeaderboard)
            {
                if (this.isCenteredAroundPlayer && startPosition.HasValue == false)
                {
                    var leaderboard = PF.Do(new GetFriendLeaderboardAroundPlayerRequest
                    {
                        IncludeFacebookFriends = this.includeFacebookFriends,
                        IncludeSteamFriends    = this.includeSteamFriends,
                        MaxResultsCount        = this.maxResultsCount,
                        StatisticName          = this.leaderboardName,
                        ProfileConstraints     = this.profileContraints,
                    });

                    yield return(leaderboard);

                    this.AppendLeaderboardResults(leaderboard, leaderboard.HasError == false ? leaderboard.Value.Leaderboard : null);
                }
                else
                {
                    var leaderboard = PF.Do(new GetFriendLeaderboardRequest
                    {
                        IncludeFacebookFriends = this.includeFacebookFriends,
                        IncludeSteamFriends    = this.includeSteamFriends,
                        MaxResultsCount        = this.maxResultsCount,
                        StatisticName          = this.leaderboardName,
                        StartPosition          = startPosition.HasValue ? startPosition.Value : 0,
                        ProfileConstraints     = this.profileContraints,
                    });

                    yield return(leaderboard);

                    this.AppendLeaderboardResults(leaderboard, leaderboard.HasError == false ? leaderboard.Value.Leaderboard : null);
                }
            }
            else
            {
                if (this.isCenteredAroundPlayer && startPosition.HasValue == false)
                {
                    var leaderboard = PF.Do(new GetLeaderboardAroundPlayerRequest
                    {
                        MaxResultsCount    = this.maxResultsCount,
                        StatisticName      = this.leaderboardName,
                        ProfileConstraints = this.profileContraints,
                    });

                    yield return(leaderboard);

                    this.AppendLeaderboardResults(leaderboard, leaderboard.HasError == false ? leaderboard.Value.Leaderboard : null);
                }
                else
                {
                    var leaderboard = PF.Do(new GetLeaderboardRequest
                    {
                        MaxResultsCount    = this.maxResultsCount,
                        StatisticName      = this.leaderboardName,
                        StartPosition      = startPosition.HasValue ? startPosition.Value : 0,
                        ProfileConstraints = this.profileContraints,
                    });

                    yield return(leaderboard);

                    this.AppendLeaderboardResults(leaderboard, leaderboard.HasError == false ? leaderboard.Value.Leaderboard : null);
                }
            }

            // Testing is we should put the current player in the center of the leaderboard
            if (startPosition.HasValue == false && this.isCenteredAroundPlayer)
            {
                for (int i = 0; i < this.entries.Count; i++)
                {
                    if (this.entries[i].PlayFabId == PF.User.PlayFabId)
                    {
                        this.CenterOnIndex(i);
                    }
                }
            }
        }
        private IEnumerator <bool> RegisterForIosPushNotificationsCoroutine()
        {
            var notifications = UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound;

            UnityEngine.iOS.NotificationServices.RegisterForNotifications(notifications, true);

            int retryCount = 0;

            while (this.deviceToken == null)
            {
                if (this.deviceToken == null && UnityEngine.iOS.NotificationServices.deviceToken != null)
                {
                    this.deviceToken = System.BitConverter.ToString(UnityEngine.iOS.NotificationServices.deviceToken).Replace("-", "").ToLower();
                }

                retryCount++;

                if (retryCount >= RetryCountMax)
                {
                    yield break;
                }

                // Waiting before we retry again...
                float waitTime = 0.0f;
                while (waitTime < RetryWaitTime)
                {
                    yield return(default(bool));

                    waitTime += UnityEngine.Time.deltaTime;
                }
            }

            // if we got here and still no deviceToken, then we timed out
            if (this.deviceToken == null)
            {
                UnityEngine.Debug.LogError("PlayFabIOSPushHandler timed out waiting for the deviceToken.");

                // cleaning up this iOS push notification handler so it doesn't take up any cycles
                yield break;
            }

            string registeredUser = LostPlayerPrefs.GetString(HasRegisteredUserForPushNotificationsKey, null);

            // Checking if we're already successfully registed this user with PlayFab for push notifications
            if (registeredUser == PF.User.PlayFabId)
            {
                CoroutineRunner.Instance.StartCoroutine(this.CheckForIosPushNotificationsCoroutine());
                yield return(true);

                yield break;
            }

            var register = PF.Do(new PlayFab.ClientModels.RegisterForIOSPushNotificationRequest {
                DeviceToken = this.deviceToken
            });

            while (register.IsDone == false)
            {
                yield return(default(bool));
            }

            if (register.HasError)
            {
                var playfabError = register.Exception as PlayFabException;

                if (playfabError != null)
                {
                    UnityEngine.Debug.Log("Error Registering for iOS Push Notifications!");
                    UnityEngine.Debug.Log(playfabError.Error.Error);
                    UnityEngine.Debug.Log(playfabError.Error.ErrorMessage);
                    UnityEngine.Debug.Log(playfabError.Error.ErrorDetails);
                }
            }
            else
            {
                // Saving that we've registed this user for PushNotifications with PlayFab successfully
                LostPlayerPrefs.GetString(HasRegisteredUserForPushNotificationsKey, PF.User.PlayFabId);

                CoroutineRunner.Instance.StartCoroutine(this.CheckForIosPushNotificationsCoroutine());

                UnityEngine.Debug.Log("Push Notification Registration Successful!");
                yield return(true);
            }
        }
Esempio n. 5
0
        private IEnumerator <bool> ProcessPurchaseCoroutine(PurchaseEventArgs e, string storeId)
        {
            var catalogItem = PF.Catalog.GetCatalogItem(e.purchasedProduct.definition.id);

            Debug.AssertFormat(catalogItem != null, "Couln't find CatalogItem {0}", e.purchasedProduct.definition.id);

            Debug.AssertFormat(e.purchasedProduct.hasReceipt, "Purchased item {0} doesn't have a receipt", e.purchasedProduct.definition.id);
            var receipt = (Dictionary <string, object>)Lost.AppConfig.MiniJSON.Json.Deserialize(e.purchasedProduct.receipt);

            Debug.AssertFormat(receipt != null, "Unable to parse receipt {0}", e.purchasedProduct.receipt);

            var store         = (string)receipt["Store"];
            var transactionId = (string)receipt["TransactionID"];
            var payload       = (string)receipt["Payload"];

            Debug.AssertFormat(string.IsNullOrEmpty(store) == false, "Unable to parse store from {0}", e.purchasedProduct.receipt);
            Debug.AssertFormat(string.IsNullOrEmpty(transactionId) == false, "Unable to parse transactionID from {0}", e.purchasedProduct.receipt);
            Debug.AssertFormat(string.IsNullOrEmpty(payload) == false, "Unable to parse payload from {0}", e.purchasedProduct.receipt);

            var  currencyCode   = e.purchasedProduct.metadata.isoCurrencyCode;
            var  purchasePrice  = this.GetPurchasePrice(e);
            bool wasSuccessfull = false;

            if (Platform.CurrentDevicePlatform == DevicePlatform.iOS)
            {
                var validate = PF.Do(new ValidateIOSReceiptRequest()
                {
                    CurrencyCode  = currencyCode,
                    PurchasePrice = purchasePrice,
                    ReceiptData   = payload,
                });

                while (validate.IsDone == false)
                {
                    yield return(default(bool));
                }

                if (validate.HasError)
                {
                    Debug.LogErrorFormat("Unable to validate iOS IAP Purchase: {0}", validate?.Exception?.ToString());
                    this.LogErrorReceiptFailedInfo(catalogItem, e);
                    Debug.LogErrorFormat("ReceiptData = " + payload);

                    PlayFabMessages.HandleError(validate.Exception);
                }
                else
                {
                    wasSuccessfull = true;
                }
            }
            else if (Platform.CurrentDevicePlatform == DevicePlatform.Android)
            {
                var details = (Dictionary <string, object>)Lost.AppConfig.MiniJSON.Json.Deserialize(payload);
                Debug.AssertFormat(details != null, "Unable to parse Receipt Payload {0}", payload);

                Debug.AssertFormat(details.ContainsKey("json"), "Receipt Payload doesn't have \"json\" key: {0}", payload);
                Debug.AssertFormat(details.ContainsKey("signature"), "Receipt Payload doesn't have \"signature\" key: {0}", payload);

                var receiptJson = (string)details["json"];
                var signature   = (string)details["signature"];

                var validate = PF.Do(new ValidateGooglePlayPurchaseRequest()
                {
                    CurrencyCode  = currencyCode,
                    PurchasePrice = (uint)purchasePrice,
                    ReceiptJson   = receiptJson,
                    Signature     = signature,
                });

                while (validate.IsDone == false)
                {
                    yield return(default(bool));
                }

                if (validate.HasError)
                {
                    Debug.LogErrorFormat("Unable to validate Google Play IAP Purchase: {0}", validate?.Exception?.ToString());
                    this.LogErrorReceiptFailedInfo(catalogItem, e);
                    Debug.LogErrorFormat("Receipt Json = " + receiptJson);
                    Debug.LogErrorFormat("Signature = " + signature);

                    PlayFabMessages.HandleError(validate.Exception);
                }
                else
                {
                    wasSuccessfull = true;
                }
            }
            else
            {
                Debug.LogErrorFormat("Tried to make IAP Purchase on Unknown Platform {0}", Application.platform.ToString());
            }

            if (wasSuccessfull == false)
            {
                yield break;
            }

            float  price    = catalogItem.GetVirtualCurrenyPrice("RM") / 100.0f;
            string itemId   = e.purchasedProduct.definition.id;
            string itemType = catalogItem.ItemClass;
            string level    = SceneManager.GetActiveScene().name;

            Analytics.AnalyticsEvent.IAPTransaction(storeId, price, itemId, itemType, level, transactionId, new Dictionary <string, object>
            {
                { "localized_price", e.purchasedProduct.metadata.localizedPrice },
                { "iso_currency_code", e.purchasedProduct.metadata.isoCurrencyCode },
                { "store", store },
            });

            PF.Inventory.InvalidateUserInventory();

            yield return(true);
        }