コード例 #1
0
        /// <summary>
        /// Return 0 if the reward cannot be assigned to this user due to whathever he cannot
        /// receive or loose more points in this category
        /// </summary>
        /// <param name="rewardCategory"></param>
        /// <param name="rewardAcomulate"></param>
        /// <param name="isPlus">Receive or Loose points</param>
        /// <returns>allowed points</returns>
        private async Task <int> AllowedPointsAsync(RewardCategory rewardCategory, RewardAcumulate rewardAcomulate, bool isPlus)
        {
            var currentAcumulate = rewardAcomulate.Points;
            var points           = 0;

            if (isPlus)
            {
                if (rewardCategory.MaxPointsAllowed != -1)
                {
                    var newAcumulate = currentAcumulate + rewardCategory.PointsToIncrement;
                    if (newAcumulate > rewardCategory.MaxPointsAllowed)
                    {
                        points = rewardCategory.MaxPointsAllowed - currentAcumulate;
                    }
                    else
                    {
                        points = rewardCategory.PointsToIncrement;
                    }
                }
                else
                {
                    points = rewardCategory.PointsToIncrement;
                }
            }
            else
            {
                if (currentAcumulate == 0)
                {
                    points = 0;
                }
                else
                {
                    var diff = currentAcumulate - rewardCategory.PointsToDecrement;
                    if (diff < 0)
                    {
                        points = currentAcumulate;
                    }
                    else
                    {
                        points = rewardCategory.PointsToDecrement;
                    }
                }
            }

            // Know if the total allow this reward, should only be not allowed if total gets under 0
            if (points == 0)
            {
                return(0);
            }

            var allowedPoints = await _userStatisticsService.AllowedPointsAsync(rewardAcomulate.UserId, isPlus?points : points * -1);

            return(allowedPoints);
        }
コード例 #2
0
        /// <summary>
        /// Init acumulate if the user have no one for this category
        /// </summary>
        /// <param name="user"></param>
        /// <param name="rewardCategory"></param>
        /// <returns></returns>
        private async Task <RewardAcumulate> GetOrCreateRewardAcomulate(User user, RewardCategory rewardCategory)
        {
            var acumulate = await _uow.RewardAcumulateRepository.GetAll()
                            .Where(d => d.UserId == user.Id && d.RewardCategoryId == rewardCategory.Id)
                            .FirstOrDefaultAsync();

            if (acumulate == null)
            {
                acumulate = new RewardAcumulate
                {
                    RewardCategory = rewardCategory,
                    User           = user,
                    Points         = 0,
                    CreatedAt      = DateTime.UtcNow,
                    ModifiedAt     = DateTime.UtcNow
                };

                await _uow.RewardAcumulateRepository.AddAsync(acumulate);
            }

            return(acumulate);
        }