Exemplo n.º 1
0
        protected override void Awake()
        {
            var playFabAuth   = new PlayFabAuth(_titleId);
            var playFabUser   = new PlayFabUser(playFabAuth);
            var playFabWallet = new PlayFabWallet()
            {
                CurrencyMap = PlayFabWalletCurrencyMap
            };
            var playFabInventory = new PlayFabInventory(_catalogVersion, playFabUser, playFabWallet)
            {
                ItemMap = PlayFabInventoryItemMap
            };
            var playFabStore = new PlayFabStore(_catalogVersion, _storeId)
            {
                PriceMap   = PlayFabStorePriceMap,
                ProductMap = PlayFabStoreProductMap
            };

            var customPlayFabAuth = new CustomPlayFabAuth(playFabAuth, _customId);

            Auth      = customPlayFabAuth;
            Inventory = playFabInventory;
            Store     = playFabStore;
            User      = playFabUser;
            Wallet    = playFabWallet;
        }
        void ILink.Link(string linkKey, Action onComplete, Action onFailure)
        {
            var resultException   = default(Exception);
            var sourceLoginResult = CustomPlayFabAuth.LoginResult;
            var targetLoginResult = default(LoginResult);

            LoginWithCustomId();

            void LoginWithCustomId()
            {
                try
                {
                    PlayFabClientAPI.LoginWithCustomID(
                        new LoginWithCustomIDRequest()
                    {
                        CreateAccount         = false,
                        CustomId              = linkKey,
                        InfoRequestParameters = new GetPlayerCombinedInfoRequestParams()
                        {
                            GetUserAccountInfo = true
                        },
                        TitleId = CustomPlayFabAuth.TitleId
                    },
                        result =>
                    {
                        targetLoginResult             = result;
                        CustomPlayFabAuth.LoginResult = result;

                        UnlinkCustomId();
                    },
                        error =>
                    {
                        PlayFabErrorHandler.Process(error);

                        onFailure();
                    });
                }
                catch (Exception exception)
                {
                    ExceptionHandler.Process(exception);

                    onFailure();
                }
            }

            void UnlinkCustomId()
            {
                try
                {
                    PlayFabClientAPI.UnlinkCustomID(
                        new UnlinkCustomIDRequest()
                    {
                        CustomId = linkKey
                    },
                        result =>
                    {
                        CheckLinkKeyExpirationTime();
                    },
                        error =>
                    {
                        PlayFabErrorHandler.Process(error);

                        onFailure();
                    });
                }
                catch (Exception exception)
                {
                    ExceptionHandler.Process(exception);

                    onFailure();
                }
            }

            void CheckLinkKeyExpirationTime()
            {
                try
                {
                    PlayFabClientAPI.GetUserData(
                        new GetUserDataRequest()
                    {
                        Keys = new List <string>()
                        {
                            LinkKeyExpirationTime
                        }
                    },
                        result =>
                    {
                        var data = result.Data;

                        if (data.TryGetValue(LinkKeyExpirationTime, out var record))
                        {
                            var now            = DateTime.Now;
                            var expirationTime = DateTime.Parse(record.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

                            if (now > expirationTime)
                            {
                                resultException = new Exception($"The \"{LinkKey}\" is out of date!");

                                Restore();
                            }
                            else
                            {
                                CheckCustomId();
                            }
                        }
                        else
                        {
                            resultException = new Exception($"The \"{LinkKeyExpirationTime}\" is not found!");

                            Restore();
                        }
                    },
                        error =>
                    {
                        PlayFabErrorHandler.Process(error);

                        onFailure();
                    });
                }
                catch (Exception exception)
                {
                    ExceptionHandler.Process(exception);

                    onFailure();
                }
            }

            void CheckCustomId()
            {
                var targetCustomId = GetCustomId(targetLoginResult);

                if (targetCustomId == null || targetCustomId == linkKey)
                {
                    LinkCustomID();
                }
                else
                {
                    var sourceCustomId = GetCustomId(sourceLoginResult);

                    resultException = sourceCustomId == targetCustomId
                        ? new Exception("The source account already linked to the target account!")
                        : new Exception("The target account already has the linked account!");

                    Restore();
                }
            }

            string GetCustomId(LoginResult loginResult)
            {
                var infoResultPayload = loginResult?.InfoResultPayload;
                var accountInfo       = infoResultPayload?.AccountInfo;
                var customIdInfo      = accountInfo?.CustomIdInfo;

                return(customIdInfo?.CustomId);
            }

            void LinkCustomID()
            {
                try
                {
                    var customId = GetCustomId(sourceLoginResult);

                    PlayFabClientAPI.LinkCustomID(
                        new LinkCustomIDRequest()
                    {
                        CustomId  = customId,
                        ForceLink = true
                    },
                        result =>
                    {
                        onComplete();
                    },
                        error =>
                    {
                        PlayFabErrorHandler.Process(error);

                        onFailure();
                    });
                }
                catch (Exception exception)
                {
                    ExceptionHandler.Process(exception);

                    onFailure();
                }
            }

            void Restore()
            {
                Logout();

                void Logout()
                {
                    CustomPlayFabAuth.Logout(Login, ProcessException);
                }

                void Login()
                {
                    CustomPlayFabAuth.Login(ProcessException, ProcessException);
                }
            }

            void ProcessException()
            {
                ExceptionHandler.Process(resultException);

                onFailure();
            }
        }