/// <summary>
        /// Gets the closed achievements from http://steamcommunity.com/id/[customurl]/[game]/?xml=1.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <returns></returns>
        public ICollection <UserAchievement> GetAchievements(string steamUserId)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            List <UserAchievement> achievements = new List <UserAchievement>();

            IEnumerable <Game> games = GetGames(steamUserId);

            foreach (Game game in games.Where(g => g.PlayedRecently))
            {
                Uri xmlStatsUrl = new Uri(game.StatsUrl + "/?xml=1", UriKind.Absolute);

                string xml = _webClient.DownloadString(xmlStatsUrl);

                if (xml == null)
                {
                    continue;
                }

                IEnumerable <UserAchievement> gameAchievements;
                try
                {
                    gameAchievements = _achievementParser.ParseClosed(xml);
                }
                catch (XmlException ex)
                {
                    // log and ignore invalid achievement xml
                    HttpContext context = HttpContext.Current;
                    if (context != null)
                    {
                        ErrorSignal signal = ErrorSignal.FromContext(context);
                        if (signal != null)
                        {
                            string    message   = String.Format("Invalid xml for {0} stats: {1}.", game.Name, game.StatsUrl);
                            Exception exception = new InvalidStatsXmlException(message, ex);
                            signal.Raise(exception);
                        }
                    }

                    continue;
                }

                if (!gameAchievements.Any())
                {
                    continue;
                }

                List <UserAchievement> achievementList = gameAchievements.ToList();
                Game game1 = game;
                achievementList.ForEach(a => a.Game = game1);

                achievements.AddRange(achievementList);
            }

            return(achievements);
        }
        /// <summary>
        ///   Gets the achievements from http://steamcommunity.com/id/[customurl]/[game]/?xml=1.
        /// </summary>
        /// <param name="steamUserId"> The steam user id. </param>
        /// <param name="closedOnly"> if set to <c>true</c> [closed only]. </param>
        /// <param name="language"> The language. </param>
        /// <returns> </returns>
        private ICollection <UserAchievement> GetAchievements(string steamUserId, bool closedOnly, string language)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            List <UserAchievement> achievements = new List <UserAchievement>();

            IEnumerable <Game> games = GetGames(steamUserId, language);

            if (closedOnly)
            {
                games = games.Where(g => g.PlayedRecently);
            }
            foreach (Game game in games)
            {
                Uri xmlStatsUrl = GetStatsUrl(game.StatsUrl, language);
                Debug.WriteLine(xmlStatsUrl);

                string xml;
                try
                {
                    xml = _webClient.DownloadString(xmlStatsUrl);
                }
                catch (SteamCommunityDataException)
                {
                    //TODO: notify the user that achievements could not be retrieved for this game
                    continue;
                }

                if (xml == null)
                {
                    continue;
                }

                IEnumerable <UserAchievement> gameAchievements;
                try
                {
                    if (closedOnly)
                    {
                        gameAchievements = _achievementParser.ParseClosed(xml);
                    }
                    else
                    {
                        gameAchievements = _achievementParser.Parse(xml);
                    }
                }
                catch (XmlException ex)
                {
                    Exception exception = new InvalidStatsXmlException(steamUserId, new Uri(game.StatsUrl), ex);
                    _errorLogger.Log(exception);

                    continue;
                }

                if (!gameAchievements.Any())
                {
                    continue;
                }

                List <UserAchievement> achievementList = gameAchievements.ToList();
                Game game1 = game;
                achievementList.ForEach(a =>
                {
                    a.Achievement.Game     = game1;
                    a.Achievement.Language = language;
                });

                achievements.AddRange(achievementList);
            }

            return(achievements);
        }
Exemple #3
0
        /// <summary>
        /// Gets the achievements from http://steamcommunity.com/id/[customurl]/[game]/?xml=1.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <param name="closedOnly">if set to <c>true</c> [closed only].</param>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        private ICollection <UserAchievement> GetAchievements(string steamUserId, bool closedOnly, string language)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            List <UserAchievement> achievements = new List <UserAchievement>();

            IEnumerable <Game> games = GetGames(steamUserId, language);

            if (closedOnly)
            {
                games = games.Where(g => g.PlayedRecently);
            }
            foreach (Game game in games)
            {
                Uri xmlStatsUrl = GetStatsUrl(game.StatsUrl, language);
                Debug.WriteLine(xmlStatsUrl);

                string xml = _webClient.DownloadString(xmlStatsUrl);

                if (xml == null)
                {
                    continue;
                }

                IEnumerable <UserAchievement> gameAchievements;
                try
                {
                    if (closedOnly)
                    {
                        gameAchievements = _achievementParser.ParseClosed(xml);
                    }
                    else
                    {
                        gameAchievements = _achievementParser.Parse(xml);
                    }
                }
                catch (XmlException ex)
                {
                    // log and ignore invalid achievement xml
                    HttpContext context = HttpContext.Current;
                    if (context != null)
                    {
                        ErrorLog errorLog = ErrorLog.GetDefault(context);
                        if (errorLog != null)
                        {
                            Exception exception = new InvalidStatsXmlException(steamUserId, new Uri(game.StatsUrl), ex);
                            errorLog.Log(new Error(exception));
                        }
                    }

                    continue;
                }

                if (!gameAchievements.Any())
                {
                    continue;
                }

                List <UserAchievement> achievementList = gameAchievements.ToList();
                Game game1 = game;
                achievementList.ForEach(a =>
                {
                    a.Achievement.Game     = game1;
                    a.Achievement.Language = language;
                });

                achievements.AddRange(achievementList);
            }

            return(achievements);
        }