Untitled

 avatar
unknown
csharp
2 years ago
2.5 kB
5
Indexable
        private (int? MinPoints, int? MaxPoints, decimal Coefficient) GetCoefficient(GamificationEvent gamificationEvent, string userId)
        {
            var coefficient = 1.00m;

            var minPoints = gamificationEvent.MinPoints;
            var maxPoints = gamificationEvent.MaxPoints;

            var userTypes = efWrapper.AsNoTracking(db.GamificationUserTypeMappings)
                .Where(dbGamificationUserTypeMapping =>
                    dbGamificationUserTypeMapping.UserId == userId
                    && dbGamificationUserTypeMapping.StartDate < DateTime.Now
                    && (dbGamificationUserTypeMapping.EndDate == null || dbGamificationUserTypeMapping.EndDate > DateTime.Now))
                .Select(s => s.UserType)
                .ToList();

            userTypes.Add((int)GamificationUserTypeEnum.AllUsers);

            var rules = efWrapper.AsNoTracking(db.GamificationUserTypeEventRules)
                .Where(dbGamificationUserTypeEvent =>
                    userTypes.Contains(dbGamificationUserTypeEvent.UserType)
                    && dbGamificationUserTypeEvent.GamificationEventType == gamificationEvent.Type
                    && dbGamificationUserTypeEvent.StartDate < DateTime.Now
                    && (dbGamificationUserTypeEvent.EndDate == null || dbGamificationUserTypeEvent.EndDate > DateTime.Now)).ToList();

            var otherRules = rules.Where(rule => rule.UserType != (int)GamificationUserTypeEnum.AllUsers);

            var maxCoefficientRule = otherRules.OrderByDescending(o => o.Coefficient).FirstOrDefault();

            if (maxCoefficientRule != null)
            {
                coefficient *= maxCoefficientRule.Coefficient;
                minPoints = maxCoefficientRule.MinPoints.HasValue ? maxCoefficientRule.MinPoints : minPoints;
                maxPoints = maxCoefficientRule.MaxPoints.HasValue ? maxCoefficientRule.MaxPoints : maxPoints;
            }
            else
            {
                var generalRule = rules.SingleOrDefault(rule => rule.UserType == (int)GamificationUserTypeEnum.AllUsers);

                if (generalRule != null)
                {
                    coefficient *= generalRule.Coefficient;
                    minPoints = generalRule.MinPoints;
                    maxPoints = generalRule.MaxPoints;
                }
            }

            return (minPoints, maxPoints, coefficient);
        }
Editor is loading...
Leave a Comment