/// <summary>
        /// Gets the unpublished achievements newer than the given date.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <param name="oldestDate">The oldest date.</param>
        /// <param name="language">The language.</param>
        /// <returns>
        /// The achievements that haven't been published yet.
        /// </returns>
        public ICollection <Achievement> GetUnpublishedAchievements(string steamUserId, DateTime?oldestDate, string language = null)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            if (language == null)
            {
                language = CultureHelper.GetLanguage();
            }

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

            ICollection <Data.Achievement> dataAchievements;

            if (oldestDate == null)
            {
                dataAchievements = _achievementManager.GetUnpublishedAchievements(steamUserId);
            }
            else
            {
                dataAchievements = _achievementManager.GetUnpublishedAchievements(steamUserId, oldestDate.Value);
            }

            IEnumerable <Data.Achievement> missingNames =
                dataAchievements.Where(a => !a.AchievementNames.Where(n => n.Language == language).Any());

            if (missingNames.Any())
            {
                IEnumerable <Achievement> communityAchievements =
                    _communityService.GetAchievements(steamUserId, language)
                    .Select(ua => ua.Achievement);

                foreach (Data.Achievement achievement in missingNames)
                {
                    Achievement missing =
                        communityAchievements
                        .Where(a => a.Game.Id == achievement.GameId && a.ApiName == achievement.ApiName)
                        .SingleOrDefault();

                    if (missing != null)
                    {
                        achievement.AchievementNames.Add(new AchievementName
                        {
                            Language    = language,
                            Name        = missing.Name,
                            Description = missing.Description
                        });
                    }
                }
            }

            List <Achievement> achievements = dataAchievements.ToSimpleAchievementList(games, language);

            return(achievements);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates the achievements.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <returns>true if successful, else false.</returns>
        /// <remarks>jQuery/WCF requires a return value in order for jQuery to execute $.ajax.success.</remarks>
        public int UpdateAchievements(string steamUserId)
        {
            if (steamUserId == null)
            {
                throw new ArgumentNullException("steamUserId");
            }

            ICollection <UserAchievement> achievements = _communityService.GetAchievements(steamUserId);

            Data.User user = _achievementManager.GetUser(steamUserId);
            if (user == null)
            {
                throw new ArgumentException("User does not exist.", "steamUserId");
            }

            int updated = _achievementManager.UpdateAchievements(achievements.ToDataAchievements(user.FacebookUserId));

            return(updated);
        }
        /// <summary>
        /// Gets the unpublished achievements newer than the given date.
        /// </summary>
        /// <param name="facebookUserId"> </param>
        /// <param name="oldestDate">The oldest date.</param>
        /// <param name="language">The language.</param>
        /// <returns>
        /// The achievements that haven't been published yet.
        /// </returns>
        public ICollection <Models.Achievement> GetUnpublishedAchievements(long facebookUserId, DateTime?oldestDate,
                                                                           string language = null)
        {
            if (language == null)
            {
                language = CultureHelper.GetLanguage();
            }

            string steamUserId = GetSteamUserId(facebookUserId);

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

            ICollection <Data.Achievement> dataAchievements;

            if (oldestDate == null)
            {
                dataAchievements = _achievementManager.GetUnpublishedAchievements(facebookUserId);
            }
            else
            {
                dataAchievements = _achievementManager.GetUnpublishedAchievements(facebookUserId, oldestDate.Value);
            }

            IEnumerable <Data.Achievement> missingNames =
                dataAchievements.Where(a => !a.AchievementNames.Where(n => n.Language == language).Any());

            if (missingNames.Any())
            {
                IEnumerable <Models.Achievement> communityAchievements =
                    _communityService.GetAchievements(steamUserId, language)
                    .Select(ua => ua.Achievement);

                foreach (Data.Achievement achievement in missingNames)
                {
                    Models.Achievement missing =
                        communityAchievements
                        .Where(a => a.Game.Id == achievement.GameId && a.ApiName == achievement.ApiName)
                        .SingleOrDefault();

                    if (missing != null)
                    {
                        achievement.AchievementNames.Add(new AchievementName
                        {
                            Language    = language,
                            Name        = missing.Name,
                            Description = missing.Description
                        });
                    }
                }
            }

            ICollection <Models.Achievement> achievements = Mapper.Map <ICollection <Models.Achievement> >(dataAchievements);

            // set game
            foreach (var dataAchievement in dataAchievements)
            {
                var achievement = achievements.Where(a => a.Id == dataAchievement.Id).SingleOrDefault();
                if (achievement != null)
                {
                    achievement.Game = games.Where(g => g.Id == dataAchievement.GameId).SingleOrDefault();
                }
            }

            return(achievements.Where(a => a.Game != null)
                   .OrderBy(a => a.Game.Name).ThenBy(a => a.Name).ToList());
        }