Пример #1
0
        /// <summary>
        /// Assigns the new achievements.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <param name="achievements">All achievements for the given user.</param>
        public int AssignAchievements(string steamUserId, IEnumerable <Achievement> achievements)
        {
            // get the achievement ids for the games in the given achievements
            IEnumerable <int> unassignedAchievementIds = GetUnassignedAchievementIds(steamUserId, achievements);

            if (!unassignedAchievementIds.Any())
            {
                return(0);
            }

            // create the unassigned achievements and insert them
            IEnumerable <UserAchievement> newUserAchievements =
                from id in unassignedAchievementIds
                select new UserAchievement
            {
                SteamUserId   = steamUserId,
                AchievementId = id,
                Date          = DateTime.Now
            };

            _repository.InsertAllOnSubmit(newUserAchievements);
            _repository.SubmitChanges();

            return(newUserAchievements.Count());
        }
        /// <summary>
        /// Assigns the new achievements.
        /// </summary>
        /// <param name="achievements">All achievements for the given user.</param>
        /// <returns></returns>
        public int AssignAchievements(IEnumerable <UserAchievement> achievements)
        {
            if (achievements == null)
            {
                throw new ArgumentNullException("achievements");
            }

            if (!achievements.Any())
            {
                return(0);
            }

            // get the achievement ids for the games in the given achievements
            long facebookUserId = achievements.First().FacebookUserId;
            IEnumerable <Achievement> unassignedAchievements =
                GetUnassignedAchievements(facebookUserId, achievements.Select(achievement => achievement.Achievement));

            if (!unassignedAchievements.Any())
            {
                return(0);
            }

            // create the unassigned achievements and insert them
            IEnumerable <UserAchievement> newUserAchievements =
                from achievement in unassignedAchievements
                join userAchievement in achievements on
                new
            {
                Name = achievement.Name.ToUpper(),
                achievement.GameId,
                achievement.Description
            } equals
            new
            {
                Name = userAchievement.Achievement.Name.ToUpper(),
                userAchievement.Achievement.GameId,
                userAchievement.Achievement.Description
            }
            select new UserAchievement
            {
                FacebookUserId = facebookUserId,
                AchievementId  = achievement.Id,
                Date           = ValidateDate(userAchievement.Date),
                Published      = false
            };

            _repository.InsertAllOnSubmit(newUserAchievements);
            _repository.SubmitChanges();

            return(newUserAchievements.Count());
        }
        /// <summary>
        /// Assigns the new achievements.
        /// </summary>
        /// <param name="steamUserId">The steam user id.</param>
        /// <param name="achievements">All achievements for the given user.</param>
        public int AssignAchievements(IEnumerable <UserAchievement> achievements)
        {
            if (achievements == null)
            {
                throw new ArgumentNullException("achievements");
            }

            if (!achievements.Any())
            {
                return(0);
            }

            // get the achievement ids for the games in the given achievements
            string steamUserId = achievements.First().SteamUserId;
            IEnumerable <Achievement> unassignedAchievements =
                GetUnassignedAchievements(steamUserId, achievements.Select(achievement => achievement.Achievement));

            if (!unassignedAchievements.Any())
            {
                return(0);
            }

            // create the unassigned achievements and insert them
            IEnumerable <UserAchievement> newUserAchievements =
                from achievement in unassignedAchievements
                select new UserAchievement
            {
                SteamUserId   = steamUserId,
                AchievementId = achievement.Id,
                Date          = DateTime.Now       //TODO: use achievement date
            };

            _repository.InsertAllOnSubmit(newUserAchievements);
            _repository.SubmitChanges();

            return(newUserAchievements.Count());
        }
Пример #4
0
        /// <summary>
        /// Updates the user. Passing a new user with FacebookUserId and SteamUserId will insert a new User.
        /// </summary>
        /// <param name="user">The user.</param>
        public void UpdateUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(user.SteamUserId))
            {
                throw new ArgumentException("user.SteamUserId cannot be null or empty", "user");
            }

            if (user.FacebookUserId == 0)
            {
                throw new ArgumentException("user.FacebookUserId cannot be 0", "user");
            }

            bool exists = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Any();

            if (!exists)
            {
                // the user does not exist, create a new one.
                User newUser = new User
                {
                    FacebookUserId = user.FacebookUserId,
                    SteamUserId    = user.SteamUserId,
                    AccessToken    = user.AccessToken,
                    AutoUpdate     = user.AutoUpdate
                };

                _repository.InsertOnSubmit(newUser);
                _repository.SubmitChanges();

                return;
            }

            User existingUser = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();

            string existingSteamUserId = existingUser.SteamUserId;
            bool   steamIdChanged      = existingSteamUserId.ToUpper() != user.SteamUserId.ToUpper();
            bool   hasAchievements     = false;
            List <UserAchievement> userAchievements = null;

            if (steamIdChanged)
            {
                // delete the user's acheivements
                userAchievements = (from u in _repository.UserAchievements
                                    where u.SteamUserId.ToUpper() == existingSteamUserId.ToUpper()
                                    select u).ToList();

                hasAchievements = userAchievements.Any();
                if (hasAchievements)
                {
                    _repository.DeleteAllOnSubmit(userAchievements);
                    _repository.SubmitChanges();
                }
            }

            // update
            existingUser             = _repository.Users.Where(u => u.FacebookUserId == user.FacebookUserId).Single();
            existingUser.AccessToken = user.AccessToken;
            existingUser.AutoUpdate  = user.AutoUpdate;
            existingUser.SteamUserId = user.SteamUserId;

            _repository.SubmitChanges();

            if (steamIdChanged && hasAchievements)
            {
                // insert the user's updated achievements
                List <UserAchievement> updatedUserAchievements = new List <UserAchievement>();

                foreach (UserAchievement userAchievement in userAchievements)
                {
                    UserAchievement updated =
                        new UserAchievement
                    {
                        AchievementId = userAchievement.AchievementId,
                        Date          = userAchievement.Date,
                        Id            = userAchievement.Id,
                        Published     = userAchievement.Published,
                        SteamUserId   = user.SteamUserId
                    };

                    updatedUserAchievements.Add(updated);
                }

                _repository.InsertAllOnSubmit(updatedUserAchievements);
                _repository.SubmitChanges();
            }
        }