Ejemplo n.º 1
0
        private void GiveAward(RaffledType type)
        {
            var losers = _userCodeRepository.GetAll().Include(x => x.Code).Where(x => !x.Code.IsWinning);

            if (type == RaffledType.PerDay)
            {
                losers = losers.Where(x => x.SentAt.Date == DateTime.Now.Date);
            }

            var losersList = losers.ToList();

            var userCodeAward = _userCodeAwardRepository.GetAll().ToList();

            losersList = losersList.Where(x => userCodeAward.All(y => y.UserCodeId != x.Id)).ToList();

            if (!losersList.Any())
            {
                return;
            }

            var rnd = new Random();
            var randomLoserIndex = rnd.Next(0, losersList.Count - 1);
            var winningUser      = losersList[randomLoserIndex];

            var randomAward = GetRandomAward(type);

            _userCodeAwardRepository.Insert(new UserCodeAward
            {
                Award    = randomAward,
                UserCode = winningUser,
                WonAt    = DateTime.Now
            });
        }
Ejemplo n.º 2
0
        private Award GetRandomAward(RaffledType type)
        {
            var awards        = _awardRepository.GetAll().Where(x => x.RaffledType == (byte)type).ToList();
            var awardedAwards = _userCodeAwardRepository
                                .GetAll()
                                .Where(x => x.Award.RaffledType == (byte)type)
                                .Select(x => x.Award)
                                .GroupBy(x => x.Id)
                                .ToList();

            var availableAwards = new List <Award>();

            foreach (var award in awards)
            {
                var numberOfAwardedAwards = awardedAwards
                                            .FirstOrDefault(x => x.Key == award.Id)?.Count() ?? 0;
                var awardsLeft = award.AwardQuantity - numberOfAwardedAwards;
                availableAwards.AddRange(Enumerable.Repeat(award, awardsLeft));
            }

            if (availableAwards.Count == 0)
            {
                throw new ApplicationException("We are out of awards. Sorry!");
            }

            var rnd = new Random();
            var randomAwardIndex = rnd.Next(0, availableAwards.Count);

            return(availableAwards[randomAwardIndex]);
        }
Ejemplo n.º 3
0
        private int GetAwardQuantityPerType(RaffledType type)
        {
            var awardsQuantity = _awardRepository.GetAll().Where(x => x.RuffledType == (byte)type)
                                 .Select(x => x.Quantity).Sum();

            return(awardsQuantity);
        }
Ejemplo n.º 4
0
        private void GiveAwards(RaffledType type)
        {
            var awardsQuantity = GetAwardQuantityPerType(type);

            for (int i = 0; i < awardsQuantity; i++)
            {
                GiveAward(type);
            }
        }
Ejemplo n.º 5
0
        private void ProcessAwards(RaffledType type)
        {
            var numberOfAwards = GetAwardQuantityPerType(type);

            for (var i = 0; i < numberOfAwards; i++)
            {
                GiveAward(type);
            }
        }
Ejemplo n.º 6
0
        public void GiveAwards(RaffledType type)
        {
            var numberOfAwards = GetAwardQuantityPerType(type);


            for (var i = 0; i < numberOfAwards; i++)
            {
                GiveAward(type);
            }

            //var users = _userCodeRepository.GetAll().Include(x => x.Code).Where(x => !x.Code.IsWinning);

            //if (type == RaffledType.PerDay)
            //{
            //    //all users from one day, suitable for a daily draw
            //    users = users.Where(x => x.SentAt.Date == DateTime.Now.Date);
            //}

            //var usersList = users.ToList();

            //var numberOfAwards = GetAwardQuantityPerType(type);

            //for (var i = 0; i < numberOfAwards; i++)
            //{
            //    if (usersList.Count() >= numberOfAwards)
            //    {
            //        var rnd = new Random();
            //        var randomAwardIndex = rnd.Next(0, usersList.Count - 1);
            //        var winningUser = usersList[randomAwardIndex];
            //        var award = GetRandomAward(type);

            //        var userCode = new UserCode
            //        {
            //            Code = winningUser.Code,
            //            FirstName = winningUser.FirstName,
            //            LastName = winningUser.LastName,
            //            EMail = winningUser.EMail,
            //            SentAt = winningUser.SentAt
            //        };

            //        _userCodeAwardRepository.Insert(new UserCodeAward()
            //        {
            //            Award = award,
            //            UserCode = userCode,
            //            WonAt = DateTime.Now
            //        });

            //        usersList.Remove(userCode);
            //    }
            //}
        }
Ejemplo n.º 7
0
        private void GiveAward(RaffledType type)
        {
            //TODO: get all not winning users per type of submission (daily, immediately, finally)
            //these are all users, suitable for a final draw
            var users = _userCodeRepository.GetAll().Include(x => x.Code).Where(x => !x.Code.IsWinning);

            if (type == RaffledType.PerDay)
            {
                //all users from one day, suitable for a daily draw
                users = users.Where(x => x.SentAt.Date == DateTime.Now.Date);
            }

            var usersList = users.ToList();

            //check that all in usersList have not already won an award, are not in the UserCodeAwards repository with an award
            var userCodeAwards = _userCodeAwardRepository.GetAll().ToList();                  //get all winners

            usersList = usersList.Where(x => userCodeAwards.All(y => y.Id != x.Id)).ToList(); //filter usersList to check if they do not contain winners, remove any winners

            if (!usersList.Any())
            {
                return;
            }

            //TODO: get random user from list above
            var rnd = new Random();
            var randomAwardIndex = rnd.Next(0, usersList.Count - 1);
            var winningUser      = usersList[randomAwardIndex];

            //TODO: get random award per type
            var randomAward = GetRandomAward(type);

            //TODO: match user with award and insert into UserCodeAward repository/table
            _userCodeAwardRepository.Insert(new UserCodeAward()
            {
                Award    = randomAward,
                UserCode = winningUser,
                WonAt    = DateTime.Now
            });
        }
Ejemplo n.º 8
0
        private Award GetRandomAward(RaffledType type)
        {
            var awards        = _awardRepository.GetAll().Where(x => x.RaffledType == (byte)type).ToList();
            var awardedAwards = _userCodeAwardRepository
                                .GetAll()
                                .Where(x => x.Award.RaffledType == (byte)type); //filter awarded awarda by type

            if (type == RaffledType.PerDay)
            {
                awardedAwards.Where(x => x.WonAt.Date == DateTime.Now.Date); //for the daily raffle, include only the awards that have been awarded that same day
            }

            var awardedAwardsList = awardedAwards
                                    .Select(x => x.Award)
                                    .GroupBy(x => x.Id)
                                    .ToList();

            var availableAwards = new List <Award>();

            foreach (var award in awards)
            {
                var numberOfAwardedAwards = awardedAwardsList
                                            .FirstOrDefault(x => x.Key == award.Id)?.Count() ?? 0;
                var awardsLeft = award.AwardQuantity - numberOfAwardedAwards;
                availableAwards.AddRange(Enumerable.Repeat(award, awardsLeft));
            }

            if (availableAwards.Count == 0)
            {
                throw new ApplicationException("We are out of awards. Sorry!");
            }

            var rnd = new Random();
            var randomAwardIndex = rnd.Next(0, availableAwards.Count - 1);

            return(availableAwards[randomAwardIndex]);
        }