public Promise ApplyInvitation()
        {
            string   invitationToken = ResolveInvitationToken();
            Deferred deferred        = new Deferred();

            if (string.IsNullOrEmpty(invitationToken))
            {
                deferred.Reject();
                return(deferred.Promise());
            }
            InvitationSaveData invitationSaveData =
                ServiceLocator.Resolve <IStorageService>().ResolveData <InvitationSaveData>();

            ServiceLocator.Resolve <IServerService>().CallTokenFull <object>("ApplyInvitation", new ApplyInvitationData
            {
                InvitorToken    = invitationToken,
                InvitationToken = ServiceLocator.Resolve <IUserManagementService>().GetSystemId()
            }).Then(o =>
            {
                invitationSaveData.Applied         = true;
                invitationSaveData.InvitationToken = invitationToken;
                ServiceLocator.Resolve <IStorageService>().UpdateData(invitationSaveData);
                ServiceLocator.Resolve <IAnalyticsService>().StatInvitationApplied();
                deferred.Resolve();
            }, () =>
            {
                invitationSaveData.Applied         = true;
                invitationSaveData.InvitationToken = invitationToken;
                ServiceLocator.Resolve <IStorageService>().UpdateData(invitationSaveData);
                deferred.Reject();
            });
            return(deferred.Promise());
        }
Esempio n. 2
0
        public static Promise <PromiseAnyResult, object[]> Any(IPromise[] promises)
        {
            int resolveCount = 0;

            object[] promiseResult = new object[promises.Length];

            Deferred <PromiseAnyResult, object[]> result = new Deferred <PromiseAnyResult, object[]>();
            int counter = 0;

            foreach (IPromise promise in promises)
            {
                int index = counter;
                Promise <object, object> innerPromise = (Promise <object, object>)promise.GetType().GetField("_internalPromise").GetValue(promise);
                innerPromise.Then(o =>
                {
                    result.Resolve(new PromiseAnyResult
                    {
                        Index  = index,
                        Result = o
                    });
                }, o =>
                {
                    promiseResult[index] = o;
                    resolveCount++;
                    if (resolveCount == promises.Length)
                    {
                        result.Reject(promiseResult);
                    }
                });
                counter++;
            }
            return(result.Promise());
        }
Esempio n. 3
0
        public Promise DownloadAllAssetBundles(Action <AllAssetBundleDownloadProgressInfo> progress)
        {
            Deferred deferred = new Deferred();

            string[] notDownloadedAssetBundles =
                _serverAssetBundleVersions.Keys.Where(s => !IsAssetBundleDownloaded(s)).ToArray();
            Action <int> download = null;

            long[]        sizes             = notDownloadedAssetBundles.Select(s => _serverAssetBundleVersions[s].Size).ToArray();
            List <string> downloadedBundles = new List <string>();
            long          downloadedSize    = 0;

            download = i =>
            {
                if (i == notDownloadedAssetBundles.Length)
                {
                    deferred.Resolve();
                }
                else
                {
                    DownloadAssetBundle(notDownloadedAssetBundles[i], p =>
                    {
                        progress(new AllAssetBundleDownloadProgressInfo(sizes, downloadedSize + p.Progress, p.Progress,
                                                                        notDownloadedAssetBundles, downloadedBundles.ToArray(), i));
                    }).Then(() =>
                    {
                        downloadedBundles.Add(notDownloadedAssetBundles[i]);
                        downloadedSize += _serverAssetBundleVersions[notDownloadedAssetBundles[i]].Size;
                        download(i + 1);
                    }, () => deferred.Reject());
                }
            };
            download(0);
            return(deferred.Promise());
        }
Esempio n. 4
0
 void Update()
 {
     lock (_defferedLockObject)
     {
         while (_resolvedPromise.Count > 0)
         {
             Deferred deferred = _resolvedPromise.Dequeue();
             deferred.Resolve();
         }
     }
 }
Esempio n. 5
0
        public static Promise <PromiseAllResult[], PromiseAllResult[]> ThenAll(IPromise[] promises)
        {
            int resolveCount = 0;

            PromiseAllResult[] promiseAllResult = new PromiseAllResult[promises.Length];

            Deferred <PromiseAllResult[], PromiseAllResult[]> result = new Deferred <PromiseAllResult[], PromiseAllResult[]>();

            Action checkToResolve = () =>
            {
                if (resolveCount != promises.Length)
                {
                    return;
                }
                if (promiseAllResult.All(result1 => result1.Succeded))
                {
                    result.Resolve(promiseAllResult);
                }
                else
                {
                    result.Reject(promiseAllResult);
                }
            };
            int counter = 0;

            foreach (IPromise promise in promises)
            {
                int index = counter;
                InternalPromise <object, object> innerPromise = (InternalPromise <object, object>)promise.GetType().GetField("_internalPromise", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(promise);
                innerPromise.Then(o =>
                {
                    resolveCount++;
                    promiseAllResult[index] = new PromiseAllResult
                    {
                        Result   = o,
                        Succeded = true
                    };
                    checkToResolve();
                }, o =>
                {
                    resolveCount++;
                    promiseAllResult[index] = new PromiseAllResult
                    {
                        Result   = o,
                        Succeded = false
                    };
                    checkToResolve();
                });
                counter++;
            }
            return(result.Promise());
        }
Esempio n. 6
0
        private Promise InternalFullUpdate()
        {
            Deferred deferred = new Deferred();

            if (!IsRegistered)
            {
                deferred.Reject();
                return(deferred.Promise());
            }
            UserInfo userInfo = ServiceLocator.Resolve <IStorageService>().ResolveData <UserInfo>() ?? new UserInfo();

            if (!userInfo.AddedScoreBalance.IsEmpty())
            {
                userInfo.NotSyncedScoreBalances.Add(Guid.NewGuid().ToString(), userInfo.AddedScoreBalance);
            }
            userInfo.AddedScoreBalance = new ScoreBalance();
            FullData fullData = new FullData();

            fullData.AddScoreDatas =
                userInfo.NotSyncedScoreBalances.Select(
                    pair => new AddScoreData {
                Token = pair.Key, Score = pair.Value.Score, Values = pair.Value.Balance
            })
                .ToArray();
            fullData.AchievementIds =
                ((AchievementService)ServiceLocator.Resolve <IAchievementService>()).GetNotSyncAchievementIds();
            fullData.PurchasedItemIds = ((StoreService)ServiceLocator.Resolve <IStoreService>()).GetNotPurchasableIds();
            if (fullData.AddScoreDatas.Length == 0 && fullData.AchievementIds.Length == 0 &&
                fullData.PurchasedItemIds.Length == 0)
            {
                deferred.Resolve();
                return(deferred.Promise());
            }
            ServiceLocator.Resolve <IServerService>().CallTokenFull <FullDataResult>("FullUpdateData", fullData).Then(
                result =>
            {
                UserInfo info = ServiceLocator.Resolve <IStorageService>().ResolveData <UserInfo>() ?? new UserInfo();
                info.Score    = result.UserData.Score;
                info.Balance  = result.UserData.Values;
                if (!userInfo.AddedScoreBalance.IsEmpty())
                {
                    info.Score   += userInfo.AddedScoreBalance.Score;
                    info.Balance += userInfo.AddedScoreBalance.Balance;
                }
                info.NotSyncedScoreBalances = new Dictionary <string, ScoreBalance>();
                ServiceLocator.Resolve <IStorageService>().UpdateData(info);
                ServiceLocator.Resolve <IServerService>().Call <ServerAchievement[]>("GetAchievements", null).Then(
                    achievements =>
                {
                    ServiceLocator.Resolve <IServerService>()
                    .Call <string[]>("GetClaimedAchievements", null)
                    .Then(
                        claimedAchievements =>
                    {
                        ((AchievementService)ServiceLocator.Resolve <IAchievementService>())
                        .OnServerAchievementResolved(
                            achievements.ToDictionary(achievement => achievement.AchievementId,
                                                      achievement =>
                                                      new AchievementService.ServerAchievementInfo
                        {
                            Balance = new Balance {
                                Values = achievement.Values
                            },
                            Score = achievement.Score
                        }), claimedAchievements);
                        ServiceLocator.Resolve <IServerService>().Call <ServerPurchasableItem[]>("GetItems", null).Then(
                            items =>
                        {
                            ServiceLocator.Resolve <IServerService>().Call <string[]>("GetPurchasedItems", null).Then(
                                purchasedItems =>
                            {
                                ((StoreService)ServiceLocator.Resolve <IStoreService>())
                                .OnServerPurchaseResolved(
                                    items.ToDictionary(item => item.ItemId,
                                                       item => new ItemCosts
                                {
                                    Purchase =
                                        new Balance {
                                        Values = item.PurchaseCost
                                    },
                                    Rent = new Balance {
                                        Values = item.RentCost
                                    }
                                }), purchasedItems);
                                deferred.Resolve();
                            }, () => deferred.Reject());
                        }, () => deferred.Reject());
                    }, () => deferred.Reject());
                }, () => deferred.Reject());
            }, () => deferred.Reject());
            return(deferred.Promise());
        }
Esempio n. 7
0
        public Promise DownloadAssetBundle(string assetBundle, Action <DownloadProgress> progress)
        {
            Deferred deferred            = new Deferred();
            Uri      bundleServerAddress = GetBundleServerAddress(assetBundle);

            InfoResolver.Resolve <FortInfo>().ServerConnectionProvider.UserConnection.LoadFromStorage(bundleServerAddress, progress).Then(s => deferred.Resolve(), deferred.Reject);
            return(deferred.Promise());
        }