Esempio n. 1
0
        /// <summary>
        /// Populates an quest view model with information about a single quest
        /// </summary>
        /// <param name="id">The id of the quest</param>
        /// <param name="work">The Unit of Work for DB access.  If null, one will be created</param>
        /// <returns>Info about a single quest</returns>
        public static QuestViewModel Populate(int id, UnitOfWork work = null)
        {
            if (work == null)
            {
                work = new UnitOfWork();
            }

            bool     currentUserEarned     = false;
            DateTime?currentUserEarnedDate = null;

            if (WebSecurity.IsAuthenticated)
            {
                quest_instance instance = (from qi in work.EntityContext.quest_instance
                                           where qi.quest_id == id && qi.user_id == WebSecurity.CurrentUserId
                                           select qi).FirstOrDefault();
                if (instance != null)
                {
                    currentUserEarnedDate = instance.completed_date;
                    currentUserEarned     = true;
                }
            }

            // Base query
            bool isAuthenticated = WebSecurity.IsAuthenticated;
            int  currentUserID   = WebSecurity.CurrentUserId;
            bool tracking        = isAuthenticated ? (from t in work.EntityContext.quest_tracking where t.user_id == currentUserID && t.quest_id == id select t).Any() : false;

            return((from qt in work.EntityContext.quest_template
                    where qt.id == id
                    select new QuestViewModel()
            {
                ID = qt.id,
                Title = qt.title,
                Image = qt.icon,
                Description = qt.description,
                Threshold = qt.threshold == null ? 0 : qt.threshold.Value,
                CreationDate = qt.created_date,
                UserCreated = qt.user_generated,
                Tracking = tracking,
                Author = new QuestAuthor()
                {
                    ID = qt.creator.id,
                    DisplayName = qt.creator.display_name,
                    Image = qt.creator.image,
                    IsPlayer = qt.creator.is_player
                },
                State = qt.state,
                Achievements = from step in qt.quest_achievement_step
                               select new AssociatedAchievement()
                {
                    ID = step.achievement_id,
                    Image = step.achievement_template.icon,
                    Title = step.achievement_template.title
                },
                CurrentUserEarnedDate = currentUserEarnedDate,
                CurrentUserHasEarned = currentUserEarned
            }).FirstOrDefault());
        }
        private void CompleteQuest(int questID, user userToCheck, int?achievementID, bool autoSave)
        {
            if (userToCheck.status != (int)JPPConstants.UserStatus.Active || !userToCheck.is_player)
            {
                return;
            }

            //Check if the quest exists and is active, and if an instance already exists
            quest_template questTemplate = _dbContext.quest_template.Find(questID);

            if (questTemplate.state == (int)JPPConstants.AchievementQuestStates.Retired || _dbContext.quest_instance.Any(qi => qi.quest_id == questTemplate.id && qi.user_id == userToCheck.id))
            {
                return;
            }

            quest_instance newInstance = new quest_instance()
            {
                quest_id          = questID,
                user_id           = userToCheck.id,
                completed_date    = DateTime.Now,
                comments_disabled = true
            };

            _dbContext.quest_instance.Add(newInstance);
            _unitOfWork.SystemRepository.AddNotification(
                userToCheck.id,
                userToCheck.id,
                "You completed the quest [" + questTemplate.title + "]",
                questTemplate.icon,
                new UrlHelper(HttpContext.Current.Request.RequestContext).Action(
                    "IndividualQuest",
                    "Quests",
                    new { id = questTemplate.id }
                    ) + "#" + newInstance.id + "-1",
                false);

            LoggerModel logQuestUnlock = new LoggerModel()
            {
                Action    = Logger.QuestInstanceLogType.QuestUnlocked.ToString(),
                UserID    = userToCheck.id,
                IPAddress = HttpContext.Current.Request.UserHostAddress,
                TimeStamp = DateTime.Now,
                ID1       = questID,
                IDType1   = Logger.LogIDType.QuestTemplate.ToString(),
                ID2       = achievementID,
                IDType2   = Logger.LogIDType.AchievementTemplate.ToString(),
                Value1    = "ID2 represents the ID of the achievement that triggered the quest unlock"
            };

            Logger.LogSingleEntry(logQuestUnlock, _dbContext);

            if (autoSave)
            {
                Save();
            }
        }
        /// <summary>
        /// Checks for completion of quests that have the specified achievement as a quest step
        /// </summary>
        /// <param name="achievementID">ID of the achievement</param>
        /// <param name="userID"></param>
        /// <param name="autoSave"></param>
        public void CheckAssociatedQuestCompletion(int achievementID, user userToCheck, List <achievement_instance> userAchievements, bool autoSave, bool achievementRevoked = false)
        {
            //Get a list of all the quests that have the passed in achievement as one of its steps
            List <quest_template> questTemplateList = (from t in _dbContext.quest_template
                                                       join qs in _dbContext.quest_achievement_step on t.id equals qs.quest_id
                                                       where qs.achievement_id == achievementID
                                                       select t).ToList();

            if (userAchievements == null)
            {
                userAchievements = _dbContext.achievement_instance.Where(ai => ai.user_id == userToCheck.id).ToList();
            }

            foreach (quest_template questTemplate in questTemplateList)
            {
                var steps = (from a in _dbContext.quest_achievement_step
                             where a.quest_id == questTemplate.id
                             select a);

                // Get a count of achievement instances
                int instanceCount = (from a in userAchievements
                                     from s in steps
                                     where a.achievement_id == s.achievement_id
                                     select a).Count();

                int threshold = questTemplate.threshold != null ? (int)questTemplate.threshold : steps.Count();

                // Check the current instance count against the threshold
                if (instanceCount >= threshold)
                {
                    CompleteQuest(questTemplate.id, userToCheck, achievementID, autoSave);
                }
                else
                {
                    //Only try and revoke if an achievement was revoked from the player. If the quest was updated by and admin
                    //and the player no longer meets the requirements, they still keep the quest.
                    if (achievementRevoked)
                    {
                        quest_instance questInstance = _dbContext.quest_instance.SingleOrDefault(qi => qi.quest_id == questTemplate.id && qi.user_id == userToCheck.id);
                        if (questInstance != null)
                        {
                            RevokeQuest(questInstance, achievementID, false);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Helper for enabling or disabling comments on earnings
        /// </summary>
        /// <param name="earningID">The id of the earning</param>
        /// <param name="earningIsAchievement">Is this earning an achievement?  If not, assume quest.</param>
        /// <param name="newState">The new state (true = DISABLED, false = ENABLED)</param>
        /// <returns>True if successful, false otherwise</returns>
        private Boolean EnableDisable(int earningID, bool earningIsAchievement, bool newState)
        {
            UnitOfWork work = new UnitOfWork();

            // Get the instance, check the user and alter
            if (earningIsAchievement)
            {
                // Only instance owners or admins
                achievement_instance instance = work.EntityContext.achievement_instance.Find(earningID);
                if (instance.user_id != WebSecurity.CurrentUserId && !Roles.IsUserInRole(JPPConstants.Roles.FullAdmin))
                {
                    return(false);
                }

                // Already enabled?
                if (instance.comments_disabled == newState)
                {
                    return(false);
                }

                instance.comments_disabled = newState;
            }
            else
            {
                // Only instance owners or admins
                quest_instance instance = work.EntityContext.quest_instance.Find(earningID);
                if (instance.user_id != WebSecurity.CurrentUserId && !Roles.IsUserInRole(JPPConstants.Roles.FullAdmin))
                {
                    return(false);
                }

                // Already enabled?
                if (instance.comments_disabled == newState)
                {
                    return(false);
                }

                instance.comments_disabled = newState;
            }

            work.SaveChanges();
            return(true);
        }
        private void RevokeQuest(quest_instance questInstance, int achievementID, bool autoSave)
        {
            #region Log Quest Revoke
            LoggerModel logQuestRevoke = new LoggerModel()
            {
                Action    = Logger.QuestInstanceLogType.QuestRevoked.ToString(),
                UserID    = questInstance.user_id,
                IPAddress = HttpContext.Current.Request.UserHostAddress,
                TimeStamp = DateTime.Now,
                ID1       = questInstance.quest_id,
                IDType1   = Logger.LogIDType.QuestTemplate.ToString(),
                Value1    = "Achievement:" + achievementID + " was revoked.",
            };
            Logger.LogSingleEntry(logQuestRevoke, _dbContext, false);
            #endregion

            _dbContext.quest_instance.Remove(questInstance);

            if (autoSave)
            {
                Save();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Determines if the logged in user has access to the earning
        /// </summary>
        /// <param name="earningID">The ID of the earning</param>
        /// <param name="earningIsAchievement">Is the earning an achievement?  If not, it's a quest</param>
        /// <param name="work">The DB access</param>
        /// <returns>True if the user can access, false otherwise</returns>
        private bool UserCanAccessEarning(int earningID, bool earningIsAchievement, UnitOfWork work, out user earningUser, out object earningTemplate)
        {
            // Set up out param
            earningUser     = null;
            earningTemplate = null;

            // Assume invalid, then check
            bool valid = false;

            if (earningIsAchievement)
            {
                // Base query for achievements
                var q = from e in work.EntityContext.achievement_instance
                        where e.id == earningID
                        select e;

                // Can the user "see" this earning?
                var mine = from e in q
                           where e.user_id == WebSecurity.CurrentUserId
                           select e;

                var publicUsers = from e in q
                                  where e.user.privacy_settings != (int)JPPConstants.PrivacySettings.FriendsOnly
                                  select e;

                var friendOnlyFriends = from e in q
                                        join f in work.EntityContext.friend
                                        on e.user_id equals f.source_id
                                        where e.user.privacy_settings == (int)JPPConstants.PrivacySettings.FriendsOnly &&
                                        f.destination_id == WebSecurity.CurrentUserId
                                        select e;

                // Concat the queries and see if we have anything
                achievement_instance instance = mine.Concat(publicUsers).Concat(friendOnlyFriends).FirstOrDefault();
                if (instance != null)
                {
                    valid           = true;
                    earningUser     = instance.user;
                    earningTemplate = instance.achievement_template;
                }
            }
            else
            {
                // Base query for quests
                var q = from e in work.EntityContext.quest_instance
                        where e.id == earningID
                        select e;

                // Check different scenarios
                var mine = from e in q
                           where e.user_id == WebSecurity.CurrentUserId
                           select e;

                var publicUsers = from e in q
                                  where e.user.privacy_settings != (int)JPPConstants.PrivacySettings.FriendsOnly
                                  select e;

                var friendOnlyFriends = from e in q
                                        join f in work.EntityContext.friend
                                        on e.user_id equals f.source_id
                                        where e.user.privacy_settings == (int)JPPConstants.PrivacySettings.FriendsOnly &&
                                        f.destination_id == WebSecurity.CurrentUserId
                                        select e;

                // Concat the queries and see if we have anything
                quest_instance instance = mine.Concat(publicUsers).Concat(friendOnlyFriends).FirstOrDefault();
                if (instance != null)
                {
                    valid           = true;
                    earningUser     = instance.user;
                    earningTemplate = instance.quest_template;
                }
            }

            return(valid);
        }
Esempio n. 7
0
        private void RevokeQuest(quest_instance questInstance, int achievementID, bool autoSave)
        {
            #region Log Quest Revoke
            LoggerModel logQuestRevoke = new LoggerModel()
            {
                Action = Logger.QuestInstanceLogType.QuestRevoked.ToString(),
                UserID = questInstance.user_id,
                IPAddress = HttpContext.Current.Request.UserHostAddress,
                TimeStamp = DateTime.Now,
                ID1 = questInstance.quest_id,
                IDType1 = Logger.LogIDType.QuestTemplate.ToString(),
                Value1 = "Achievement:" + achievementID + " was revoked.",
            };
            Logger.LogSingleEntry(logQuestRevoke, _dbContext, false);
            #endregion

            _dbContext.quest_instance.Remove(questInstance);

            if(autoSave)
                Save();
        }
Esempio n. 8
0
        private void CompleteQuest(int questID, user userToCheck, int? achievementID, bool autoSave)
        {
            if (userToCheck.status != (int)JPPConstants.UserStatus.Active || !userToCheck.is_player)
                return;

            //Check if the quest exists and is active, and if an instance already exists
            quest_template questTemplate = _dbContext.quest_template.Find(questID);
            if (questTemplate.state == (int)JPPConstants.AchievementQuestStates.Retired || _dbContext.quest_instance.Any(qi => qi.quest_id == questTemplate.id && qi.user_id == userToCheck.id))
                return;

            quest_instance newInstance = new quest_instance()
            {
                quest_id = questID,
                user_id = userToCheck.id,
                completed_date = DateTime.Now,
                comments_disabled = true
            };

            _dbContext.quest_instance.Add(newInstance);
            _unitOfWork.SystemRepository.AddNotification(
                userToCheck.id,
                userToCheck.id,
                "You completed the quest [" + questTemplate.title + "]",
                questTemplate.icon,
                new UrlHelper(HttpContext.Current.Request.RequestContext).Action(
                    "IndividualQuest",
                    "Quests",
                    new { id = questTemplate.id }
                ) + "#" + newInstance.id + "-1",
                false);

            LoggerModel logQuestUnlock = new LoggerModel()
            {
                Action = Logger.QuestInstanceLogType.QuestUnlocked.ToString(),
                UserID = userToCheck.id,
                IPAddress = HttpContext.Current.Request.UserHostAddress,
                TimeStamp = DateTime.Now,
                ID1 = questID,
                IDType1 = Logger.LogIDType.QuestTemplate.ToString(),
                ID2 = achievementID,
                IDType2 = Logger.LogIDType.AchievementTemplate.ToString(),
                Value1 = "ID2 represents the ID of the achievement that triggered the quest unlock"
            };

            Logger.LogSingleEntry(logQuestUnlock, _dbContext);

            if (autoSave)
                Save();
        }