public static IEnumerable <PlayerAchievementViewModel> GetPlayerMaxStats()
        {
            IAchievementRepository repo = new EFAchievementRepository();

            IEnumerable <Achievement> types = repo.Achievements.GroupBy(a => a.AchievementType).Select(grp => grp.FirstOrDefault()).ToList();

            var output = new List <PlayerAchievementViewModel>();

            foreach (var t in types)
            {
                var a = repo.Achievements.Where(b => b.AchievementType == t.AchievementType).OrderByDescending(b => b.Amount).ThenBy(b => b.Timestamp).FirstOrDefault();

                if (a != null)
                {
                    var addMe = new PlayerAchievementViewModel
                    {
                        Player     = PlayerProcedures.GetPlayerFormViewModel_FromMembership(a.OwnerMembershipId),
                        Achivement = a,
                    };
                    output.Add(addMe);
                }
            }

            return(output);
        }
        /// <summary>
        /// Deletes all achivements for given types owned by a particular player
        /// </summary>
        /// <param name="player">Player whose achivements should be removed</param>
        /// <param name="achivements">The names of the achivements owned by the player to be removed</param>
        public static void DeleteAchivemenstOfTypeForPlayer(Player player, List <string> achivements)
        {
            IAchievementRepository repo = new EFAchievementRepository();
            var dbAchivements           = repo.Achievements.Where(a => a.OwnerMembershipId == player.MembershipId && achivements.Contains(a.AchievementType)).ToList();

            foreach (var a in dbAchivements)
            {
                repo.DeleteAchievement(a.Id);
            }
        }
        public static void AddStat(string membershipId, string type, float amount, float?cap = null)
        {
            // don't do anything if the achievement is marked as inactive or blank membershipId
            if (!StatsProcedures.StatTypesMap[type].Active || membershipId.IsNullOrEmpty())
            {
                return;
            }

            // don't keep stats for an AI characters
            if (PlayerProcedures.GetPlayerFromMembership(membershipId).BotId == AIStatics.ActivePlayerBotId)
            {
                IAchievementRepository repo = new EFAchievementRepository();
                var x = repo.Achievements.FirstOrDefault(a => a.OwnerMembershipId == membershipId && a.AchievementType == type);

                if (x == null)
                {
                    x = new Achievement
                    {
                        OwnerMembershipId = membershipId,
                        AchievementType   = type,
                        Amount            = cap.HasValue ? Math.Min(amount, cap.Value) : amount,
                        Timestamp         = DateTime.UtcNow,
                    };
                }
                else
                {
                    var newAmount = x.Amount + amount;

                    if (cap.HasValue)
                    {
                        newAmount = Math.Min(newAmount, cap.Value);
                    }

                    if (newAmount != x.Amount)
                    {
                        x.Amount    = newAmount;
                        x.Timestamp = DateTime.UtcNow;
                    }
                }

                repo.SaveAchievement(x);
            }
        }
        public static IEnumerable <PlayerAchievementViewModel> GetLeaderPlayersInStat(string type)
        {
            IAchievementRepository repo = new EFAchievementRepository();

            var x      = repo.Achievements.Where(a => a.AchievementType == type).OrderByDescending(s => s.Amount).ThenBy(a => a.Timestamp).Take(10).ToList();
            var output = new List <PlayerAchievementViewModel>();

            foreach (var a in x)
            {
                if (a != null)
                {
                    var addMe = new PlayerAchievementViewModel
                    {
                        Player     = PlayerProcedures.GetPlayerFormViewModel_FromMembership(a.OwnerMembershipId),
                        Achivement = a,
                    };
                    output.Add(addMe);
                }
            }
            return(output);
        }