示例#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 steamAuth = new SteamAuth(_appId);
            var steamUser = new SteamUser();

            var steamPlayFabAuth  = new SteamPlayFabAuth(playFabAuth, steamAuth);
            var steamPlayFabUser  = new SteamPlayFabUser(playFabUser, steamUser);
            var steamPlayFabStore = new SteamPlayFabStore(_catalogVersion, _storeId)
            {
                PriceMap   = PlayFabStorePriceMap,
                ProductMap = PlayFabStoreProductMap
            };

            Auth      = steamPlayFabAuth;
            Inventory = playFabInventory;
            Store     = steamPlayFabStore;
            User      = steamPlayFabUser;
            Wallet    = playFabWallet;
        }
示例#2
0
        void ILink.Link(string linkKey, Action onComplete, Action onFailure)
        {
            var resultException   = default(Exception);
            var sourceLoginResult = SteamPlayFabAuth.LoginResult;
            var targetLoginResult = default(LoginResult);

            LoginWithCustomId();

            void LoginWithCustomId()
            {
                try
                {
                    PlayFabClientAPI.LoginWithCustomID(
                        new LoginWithCustomIDRequest()
                    {
                        CreateAccount         = false,
                        CustomId              = linkKey,
                        InfoRequestParameters = new GetPlayerCombinedInfoRequestParams()
                        {
                            GetUserAccountInfo = true
                        },
                        TitleId = SteamPlayFabAuth.TitleId
                    },
                        result =>
                    {
                        targetLoginResult            = result;
                        SteamPlayFabAuth.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
                            {
                                CheckSteamId();
                            }
                        }
                        else
                        {
                            resultException = new Exception($"The \"{LinkKeyExpirationTime}\" is not found!");

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

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

                    onFailure();
                }
            }

            void CheckSteamId()
            {
                var targetSteamId = GetSteamId(targetLoginResult);

                if (targetSteamId == null)
                {
                    LinkSteamAccount();
                }
                else
                {
                    var sourceSteamId = GetSteamId(sourceLoginResult);

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

                    Restore();
                }
            }

            string GetSteamId(LoginResult loginResult)
            {
                var infoResultPayload = loginResult?.InfoResultPayload;
                var accountInfo       = infoResultPayload?.AccountInfo;
                var steamId           = accountInfo?.SteamInfo;

                return(steamId?.SteamId);
            }

            void LinkSteamAccount()
            {
                try
                {
                    var authSessionTicket = SteamPlayFabAuth.CreateAuthSessionTicket();

                    PlayFabClientAPI.LinkSteamAccount(
                        new LinkSteamAccountRequest()
                    {
                        ForceLink   = true,
                        SteamTicket = authSessionTicket
                    },
                        result =>
                    {
                        SteamPlayFabAuth.DestroyAuthSessionTicket(authSessionTicket);

                        onComplete();
                    },
                        error =>
                    {
                        SteamPlayFabAuth.DestroyAuthSessionTicket(authSessionTicket);
                        PlayFabErrorHandler.Process(error);

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

                    onFailure();
                }
            }

            void Restore()
            {
                Logout();

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

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

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

                onFailure();
            }
        }