/// <summary>
        /// Returns a Partner BadgeTypes corresponding to a particular non-Partner BadgeTypes.
        /// </summary>
        /// <param name="pBadgeType">The non-Partner version of the BadgeTypes to find a Partner version for.</param>
        /// <returns>A Partner version of the BadgeTypes passed in. If already a Partner version, it will be returned. null if none was found.</returns>
        public static BadgeTypes?GetPartnerBadgeType(BadgeTypes badgeType)
        {
            string badgeName = badgeType.ToString();

            //Check the last character for a "P"
            string checkP = badgeName.Substring(badgeName.Length - 1);

            //This is the Partner version, so return it
            if (checkP == "P")
            {
                return(badgeType);
            }

            //Add a "P" and see if there is a corresponding value
            string pBadgeName = badgeName + "P";

            BadgeTypes pBadgeType;
            bool       success = Enum.TryParse(pBadgeName, out pBadgeType);

            if (success == true)
            {
                return(pBadgeType);
            }
            return(null);
        }
        /// <summary>
        /// Finds the first instance of a Badge with a particular BadgeType
        /// </summary>
        /// <param name="badgeType">The BadgeType of the Badge</param>
        /// <param name="badgeFilter">The filter for finding the Badge</param>
        /// <returns>null if no Badge was found, otherwise the first Badge matching the parameters</returns>
        public Badge GetBadge(BadgeTypes badgeType, BadgeFilterType badgeFilter)
        {
            if (HasBadgeType(badgeType) == false)
            {
                return(null);
            }

            //Look through all Badges
            if (badgeFilter == BadgeFilterType.All)
            {
                return(AllBadges.Find((badge) => badge.BadgeType == badgeType));
            }
            //Look through all equipped Badges
            else if (badgeFilter == BadgeFilterType.Equipped)
            {
                return(GetActiveBadge(badgeType));
            }
            //Look through all unequipped Badges
            else if (badgeFilter == BadgeFilterType.UnEquipped)
            {
                return(AllBadges.Find((badge) => (badge.BadgeType == badgeType && badge.Equipped == false)));
            }

            return(null);
        }
        /// <summary>
        /// Returns a non-Partner BadgeTypes corresponding to a particular BadgeTypes.
        /// </summary>
        /// <param name="pBadgeType">The Partner version of the BadgeTypes to find a non-Partner version for.</param>
        /// <returns>A non-Partner version of the BadgeTypes passed in. If already a non-Partner version, it will be returned. null if none was found.</returns>
        public static BadgeTypes?GetNonPartnerBadgeType(BadgeTypes pBadgeType)
        {
            string pBadgeName = pBadgeType.ToString();

            //Check the last character for a "P"
            string checkP = pBadgeName.Substring(pBadgeName.Length - 1);

            //This is the non-Partner version, so return it
            if (checkP != "P")
            {
                return(pBadgeType);
            }

            //Remove the "P" and see if there is a corresponding value
            string nonPBadgeName = pBadgeName.Substring(0, pBadgeName.Length - 1);

            BadgeTypes nonPBadgeType;
            bool       success = Enum.TryParse(nonPBadgeName, out nonPBadgeType);

            if (success == true)
            {
                return(nonPBadgeType);
            }
            return(null);
        }
        /// <summary>
        /// Gets the number of active Badges of a particular BadgeType the Player has equipped.
        /// </summary>
        /// <param name="badgeType">The BadgeType to find.</param>
        /// <returns>The number of active Badges of the BadgeType.</returns>
        public int GetActiveBadgeCount(BadgeTypes badgeType)
        {
            int count = 0;

            ActiveBadgeCounts.TryGetValue(badgeType, out count);

            return(count);
        }
        /// <summary>
        /// Finds all instances of active Badges with a particular BadgeType.
        /// </summary>
        /// <param name="badgeType">The BadgeType of the Badges.</param>
        /// <returns>A list of all active Badges of the BadgeType, and an empty list if none were found.</returns>
        private List <Badge> GetActiveBadges(BadgeTypes badgeType)
        {
            if (IsBadgeTypeActive(badgeType) == false)
            {
                return(new List <Badge>());
            }

            return(ActiveBadges.FindAll((badge) => badge.BadgeType == badgeType));
        }
        /// <summary>
        /// Finds the first instance of an active Badge with a particular BadgeType.
        /// </summary>
        /// <param name="badgeType">The BadgeType of the Badge.</param>
        /// <returns>null if no Badge was found, otherwise the first active Badge matching the BadgeType.</returns>
        private Badge GetActiveBadge(BadgeTypes badgeType)
        {
            if (IsBadgeTypeActive(badgeType) == false)
            {
                return(null);
            }

            return(ActiveBadges.Find((badge) => badge.BadgeType == badgeType));
        }
        /// <summary>
        /// Gets the number of active Badges of a particular BadgeType
        /// </summary>
        /// <param name="badgeType">The BadgeType to find</param>
        /// <returns>The number of active Badges of the BadgeType</returns>
        public int GetActiveBadgeCount(BadgeTypes badgeType)
        {
            if (IsBadgeTypeActive(badgeType) == false)
            {
                return(0);
            }

            return(ActiveBadgeCounts[badgeType]);
        }
        /// <summary>
        /// Gets the number of Badges of a particular BadgeType in the Player's Inventory
        /// </summary>
        /// <param name="badgeType">The BadgeType to find</param>
        /// <returns>The number of Badges of the BadgeType in the Player's Inventory</returns>
        public int GetBadgeCount(BadgeTypes badgeType)
        {
            if (HasBadgeType(badgeType) == false)
            {
                return(0);
            }

            return(AllBadgeCounts[badgeType]);
        }
        /// <summary>
        /// Saves any state that was modified after the <see cref="M:System.Web.UI.WebControls.Style.TrackViewState" /> method was invoked.
        /// </summary>
        /// <returns>
        /// An object that contains the current view state of the control; otherwise, if there is no view state associated with the control, null.
        /// </returns>
        protected override object SaveViewState()
        {
            if (BadgeTypes != null && BadgeTypes.Any())
            {
                ViewState["BadgeTypeIds"] = BadgeTypes.Select(bt => bt.Id.ToString()).JoinStrings(",");
            }

            return(base.SaveViewState());
        }
        /// <summary>
        /// Finds all instances of a Badge with a particular BadgeType.
        /// </summary>
        /// <param name="badgeType">The BadgeType of the Badges.</param>
        /// <param name="badgeFilter">The filter for finding the Badges.</param>
        /// <returns>A list of all Badges matching the parameters, and an empty list if none were found.</returns>
        public List <Badge> GetBadges(BadgeTypes badgeType, BadgeFilterType badgeFilter)
        {
            if (HasBadgeType(badgeType) == true)
            {
                if (badgeFilter == BadgeFilterType.All)
                {
                    return(AllBadges.FindAll((badge) => badge.BadgeType == badgeType));
                }
                else if (badgeFilter == BadgeFilterType.Equipped)
                {
                    return(GetActiveBadges(badgeType));
                }
                else if (badgeFilter == BadgeFilterType.UnEquipped)
                {
                    return(AllBadges.FindAll((badge) => (badge.BadgeType == badgeType && badge.Equipped == false)));
                }
            }

            return(new List <Badge>());
        }
        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Controls.Clear();

            _badges.Clear();
            if (BadgeTypes != null)
            {
                var currentPerson = ((RockPage)Page).CurrentPerson;
                foreach (var badgeType in BadgeTypes.OrderBy(b => b.Order))
                {
                    if (badgeType.IsAuthorized(Authorization.VIEW, currentPerson))
                    {
                        var badgeControl = new BadgeControl();
                        badgeControl.Entity     = this.Entity;
                        badgeControl.BadgeCache = badgeType;
                        _badges.Add(badgeControl);
                        Controls.Add(badgeControl);
                    }
                }
            }
        }
Beispiel #12
0
        private static void AddBadges(int level, string userId, BadgeTypes type)
        {
            var db     = new ApplicationDbContext();
            var user   = db.Users.Include(x => x.Badges).Single(x => x.Id == userId);
            var badges = db.Badges.Where(x => x.BadgeType == type);
            var commit = false;

            foreach (var badge in badges)
            {
                // if user doesn't have badge and they've met the level e.g. 10 zombie kills, give them the badge
                if (level >= badge.Level && !user.Badges.Any(x => x.BadgeId == badge.BadgeId))
                {
                    AddBadgeToContext(db, user, badge);

                    commit = true;
                }
            }

            if (commit)
            {
                db.SaveChanges();
            }
        }
 /// <summary>
 /// Tells whether the Player has an active Badge of a particular type.
 /// </summary>
 /// <param name="badgeType">The BadgeType.</param>
 /// <returns>true if the Player owns the Badge and the Badge is active, false otherwise.</returns>
 public bool IsBadgeTypeActive(BadgeTypes badgeType)
 {
     return(ActiveBadgeCounts.ContainsKey(badgeType));
 }
 /// <summary>
 /// Tells whether the Player owns a Badge of a particular BadgeType or not.
 /// </summary>
 /// <param name="badgeType">The BadgeType.</param>
 /// <returns>true if the Player owns a Badge of the BadgeType, false if not.</returns>
 public bool HasBadgeType(BadgeTypes badgeType)
 {
     return(AllBadgeCounts.ContainsKey(badgeType));
 }