Ejemplo n.º 1
0
        private async Task <Dailies> GetDailiesAsync(string url)
        {
            var response = await Api.Net.DownloadStringAsync(url);

            var date = response.Date;
            var now  = DateTime.UtcNow;

            var dailies = new Dailies()
            {
                Date = now
            };

            if (date != DateTime.MinValue)
            {
                if (date.Day != now.Day)
                {
                    var offset = now.Subtract(date).TotalMinutes;
                    if (offset > 0)
                    {
                        //server is behind

                        var secondsToNextDay = (int)(((date.Ticks / TICKS_PER_DAY + 1) * TICKS_PER_DAY - date.Ticks) / 10000) / 1000;
                        if (secondsToNextDay < 300)
                        {
                            nextUpdate   = now.AddSeconds(secondsToNextDay + 1);
                            dailies.Date = date;
                        }
                        else
                        {
                            //there's more than 5 minutes until the next day
                            // -- can't trust the local or server clock.
                            nextUpdate   = now.AddMinutes(5);
                            dailies.Date = date;
                        }
                    }
                    else
                    {
                        //server is ahead

                        if (offset > -10)
                        {
                            dailies.Date = date;
                        }
                        else
                        {
                            //server is more than 10 minutes ahead
                            // -- local clock must be wrong
                        }
                    }
                }
                else
                {
                    dailies.Date = date;
                }
            }

            var ids = ParseDailies(response.Data, dailies);

            var achievements = new Dictionary <ItemID, Achievement>(ids.Count);
            var icons        = new Dictionary <ItemID, Icon>();

            if (clearCache)
            {
                clearCache = false;
                await cache.ClearAsync();
            }

            await PopulateAchievements(ids, achievements, icons);

            var images = new System.Drawing.Image[icons.Count];
            int i      = 0;

            foreach (var icon in icons.Values)
            {
                var image = icon.GetImage();
                if (image == null)
                {
                    continue;
                }
                images[i++] = image;
            }
            if (i != images.Length)
            {
                var _images = new System.Drawing.Image[i];
                if (i > 0)
                {
                    Array.Copy(images, _images, i);
                }
                images = _images;
            }
            dailies.Icons = images;

            int count = 0,
                cii   = 0;

            for (int ci = 0, cl = dailies.Categories.Length; ci < cl; ci++)
            {
                var c     = dailies.Categories[ci];
                var items = c.Dailies;
                i = 0;

                for (int j = 0, l = items.Length; j < l; j++)
                {
                    var         d = items[j];
                    Achievement a;
                    if (achievements.TryGetValue(new ItemID(CacheType.Achievement, d.ID), out a))
                    {
                        d.Name        = a.name;
                        d.Description = a.description;
                        d.Requirement = a.requirement;

                        if (a.icon != null || a.iconId != 0 && icons.TryGetValue(new ItemID(CacheType.Icon, a.iconId), out a.icon) && a.icon != null)
                        {
                            d.Icon = a.icon.GetImage();
                        }

                        if (i != j)
                        {
                            items[i] = d;
                        }
                        i++;
                    }
                    else
                    {
                        //daily has no data
                    }
                }

                if (i > 0)
                {
                    if (i != items.Length)
                    {
                        c.Dailies = new Daily[i];
                        if (i > 0)
                        {
                            Array.Copy(items, c.Dailies, i);
                        }
                    }

                    count += i;

                    if (cii != ci)
                    {
                        dailies.Categories[cii] = c;
                    }
                    cii++;
                }
            }

            if (cii != dailies.Categories.Length)
            {
                var c = dailies.Categories;
                dailies.Categories = new Category[cii];
                if (cii > 0)
                {
                    Array.Copy(c, dailies.Categories, cii);
                }
            }

            dailies.Count = count;

            return(dailies);
        }