Exemplo n.º 1
0
        public async Task <DestinyPlayerData> GetPlayerDataAsync(DestinyPlayer player)
        {
            var bungie         = new BungieService(Shared.SharedData.BungieApiKey);
            int platform       = player.MembershipType;
            var accountId      = player.MembershipId;
            var destinyProfile = await bungie.GetDestinyProfile(platform, accountId);

            int mode = 5;

            await bungie.DownloadDestinyManifest();

            var nameplates           = new List <CharacterNameplateViewModel>();
            var activityHistoryLists = new List <List <PgcrCardViewModel> >();
            var characterIds         = destinyProfile.Data.CharacterIds;

            for (int i = 0; i < characterIds.Count; i++)
            {
                var characterId = destinyProfile.Data.CharacterIds[i];
                var history     = await bungie.GetActivityHistory(platform, accountId, characterId, mode);

                var character = await bungie.GetDestinyCharacter(platform, accountId, characterId);

                var nameplate = new CharacterNameplateViewModel
                {
                    ElementVisibility = Visibility.Visible,
                    ClassName         = (await bungie.GetDestinyClassDefinitionAsync(character.Data.ClassHash)).DisplayProperties.Name,
                    Race   = (await bungie.GetDestinyRaceDefinitionAsync(character.Data.RaceHash)).DisplayProperties.Name,
                    Gender = (await bungie.GetDestinyGenderDefinitionAsync(character.Data.GenderHash)).DisplayProperties.Name,
                    Level  = character.Data.BaseCharacterLevel,
                    Power  = character.Data.Light
                };

                var emblemBrush = new ImageBrush();
                emblemBrush.ImageSource = await bungie.GetDestinyImage(character.Data.EmblemBackgroundPath);

                nameplate.Emblem = emblemBrush;

                var historyList = new List <PgcrCardViewModel>();
                foreach (var pgcr in history)
                {
                    historyList.Add(new PgcrCardViewModel
                    {
                        Pgcr        = pgcr,
                        CharacterId = characterId
                    });
                }

                nameplates.Add(nameplate);
                activityHistoryLists.Add(historyList);
            }

            return(new DestinyPlayerData
            {
                CharacterNameplates = nameplates,
                ActivityHistoryLists = activityHistoryLists,
                CharacterIDs = characterIds,
                MembershipID = player.MembershipId,
                MembershipType = player.MembershipType
            });
        }
Exemplo n.º 2
0
        private async void btnSyncActivities_Click(object sender, RoutedEventArgs e)
        {
            //tbStatus.Text = "In progress...";

            try
            {
                var bungie = new BungieService(SharedData.BungieApiKey);

                int    platform    = 1;
                string accountName = "BlackDragon1999";

                var destinyPlayer  = (await bungie.GetDestinyPlayers(platform, accountName)).First();
                var accountId      = destinyPlayer.MembershipId;
                var destinyProfile = await bungie.GetDestinyProfile(platform, accountId);

                int mode = 5;

                await bungie.DownloadDestinyManifest();

                foreach (var characterId in destinyProfile.Data.CharacterIds)
                {
                    var history = await bungie.GetActivityHistory(platform, accountId, characterId, mode);

                    await Task.WhenAll(from pgcr in history
                                       select Task.Run(async() =>
                    {
                        var getActivityDefinition = bungie.GetActivityDefinitionAsync(pgcr.ActivityDetails.ReferenceId);
                        var getModeDefinition     = bungie.GetActivityModeDefinitionAsync(pgcr.ActivityDetails.Mode);

                        var activity = new DestinyUserActivity(pgcr, await getActivityDefinition, await getModeDefinition, characterId);

                        var msGraph = new MsGraphService(SharedData.MsGraphClientId);
                        await msGraph.CreateOrReplaceActivityAsync(activity.Activity);
                    }));
                }

                //tbStatus.Text = "Done";
            }
            catch (Exception ex)
            {
                //tbStatus.Text = $"Failed: {ex.ToString()}";
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            try
            {
                var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();

                if (NetworkInterface.GetIsNetworkAvailable() &&
                    (connectionCost.NetworkCostType == NetworkCostType.Unknown ||
                     connectionCost.NetworkCostType == NetworkCostType.Unrestricted))
                {
                    ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

                    var player = JsonConvert.DeserializeObject <DestinyPlayerInformation>((string)localSettings.Values["player"]);

                    int    platform  = player.MembershipType;
                    string accountId = player.MembershipID;
                    int    mode      = 5;

                    var bungie = new BungieService(SharedData.BungieApiKey);
                    await bungie.DownloadDestinyManifest();

                    foreach (var characterId in player.CharacterIDs)
                    {
                        var history = await bungie.GetActivityHistory(platform, accountId, characterId, mode);

                        await Task.WhenAll(from pgcr in history
                                           select Task.Run(async() =>
                        {
                            var getActivityDefinition = bungie.GetActivityDefinitionAsync(pgcr.ActivityDetails.ReferenceId);
                            var getModeDefinition     = bungie.GetActivityModeDefinitionAsync(pgcr.ActivityDetails.Mode);

                            var activity = new DestinyUserActivity(pgcr, await getActivityDefinition, await getModeDefinition, characterId);

                            var msGraph = new MsGraphService(SharedData.MsGraphClientId);
                            await msGraph.CreateOrReplaceActivityAsync(activity.Activity);
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                ToastContent toastContent = new ToastContent()
                {
                    Launch = "refresh-activity",
                    Visual = new ToastVisual()
                    {
                        BindingGeneric = new ToastBindingGeneric()
                        {
                            Children =
                            {
                                new AdaptiveText
                                {
                                    Text = "An error occured while refreshing Destiny 2 activity history"
                                }
                            }
                        }
                    }
                };

                var toast = new ToastNotification(toastContent.GetXml());
                toast.ExpirationTime = DateTime.Now.AddDays(2);
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }

            deferral.Complete();
        }