Ejemplo n.º 1
0
        /// <summary>
        /// Returns the Id of the Business that won
        /// </summary>
        /// <param name="eligibleBusinessWinners"></param>
        /// <returns></returns>

        public List <int?> RunBusinessAlgorithm(IEnumerable <Business> eligibleBusinessWinners)
        {
            if (eligibleBusinessWinners == null || eligibleBusinessWinners.Count() < 1)
            {
                return(null);
            }
            //Repeat the luckyme's occurance in the selection list based on the amount staked.
            //The Selection List;
            List <int> SelectionList = new List <int>();

            //Get the stakes that are not from dummy users
            var originalStakers     = eligibleBusinessWinners.Where(i => i.User.UserType != 2);
            var totalOriginalAmount = originalStakers.Sum(i => i.AmountToWin);

            var fixedWinners = eligibleBusinessWinners.Where(i => i.Status.ToLower() == "wins");

            //Add fixed winner to the selection list
            if (fixedWinners.Count() > 0)
            {
                SelectionList.AddRange(fixedWinners.Select(i => i.Id));
            }

            //Add Eligible original winners to the selection list
            foreach (var item in eligibleBusinessWinners)
            {
                //If any item has a status of wins.. put only that item in the list
                //if (item.Status == "wins")
                //{
                //    SelectionList.Clear();
                //    SelectionList.Add(item.Id);
                //    break;
                //}

                if (item.User.UserType != 2 && item.AmountToWin > ((Constants.WinnerTreshold) * totalOriginalAmount))
                {
                    continue;
                }
                //Divide staked amount by minimum possible stake amount
                //var flooredAmount = item.Amount / 5;
                //for (var i = flooredAmount; i > 0; i--)
                //{
                //Rund only this since all amount is the same
                SelectionList.Add(item.Id);
                //}
            }


            //Choose a winner randomly
            //Get the list length
            int selectionListLength = SelectionList.Count;
            //choose a random index between 0 and the list count inclusive
            List <int?> WinnerIds = new List <int?>();

            if (selectionListLength > 0)
            {
                //Get count of winners
                var winnersCounts = DataHub.GetWinnersCount(eligibleBusinessWinners.Count());

                //Iterate over the potential winner indices in the selection list
                for (int i = 0; i < winnersCounts; i++)
                {
                    selectionListLength = SelectionList.Count;
                    if (selectionListLength < 1)
                    {
                        break;
                    }
                    //Get the random winner
                    int selectedWinnerIndex = new Random().Next(0, selectionListLength - 1);
                    //Add the winner's Id to the List
                    WinnerIds.Add(SelectionList[selectedWinnerIndex]);
                    //Remove the winner from the list
                    SelectionList.RemoveAt(selectedWinnerIndex);
                }
            }
            //Set Winner and set Loosers
            foreach (var item in eligibleBusinessWinners)
            {
                item.DateDeclared = DateTime.Now.ToLongDateString();
                var user = context.Users.Where(u => u.Id == item.UserId).Include("BankDetails").Include("MomoDetails").FirstOrDefault();
                if (WinnerIds.Contains(item.Id))
                {
                    item.Status = "won";

                    try
                    {
                        if (user.UserType == 0)
                        {
                            MessagingService.SendMail($"{user.FirstName} {user.LastName}", user.Email, "Ntoboafund Winner", $"Congratulations {user.FirstName} {user.LastName}, your Business Ntoboa of {item.Amount} Cedi(s) on {item.Date} has yielded an amount of {item.AmountToWin} Cedi(s) which would be paid directly into your {user.PreferedMoneyReceptionMethod} account");

                            MessagingService.SendSms(user.PhoneNumber, $"Congratulations {user.FirstName} {user.LastName}, your Business Ntoboa of {item.Amount} Cedi(s) on {item.Date} has yielded an amount of {item.AmountToWin} Cedi(s) which would be paid directly into your {user.PreferedMoneyReceptionMethod} account");

                            MessagingService.SendSms(Constants.MasterNumber, $"{ user.FirstName} { user.LastName} has won {item.AmountToWin} Cedi(s) with {item.Period} Business Ntoboa of {item.Amount} Cedi(s) with Transfer Id {item.TransferId}", "NTB Winners");
                        }
                        //await StakersHub.Clients.All.SendAsync("addscholarshipwinner", new ScholarshipParticipantDTO[]{
                        //    new ScholarshipParticipantDTO
                        //    {
                        //        UserId = user.Id,
                        //        UserName = user.FirstName + " " + user.LastName,
                        //        AmountStaked = item.Amount.ToString("0.##"),
                        //        AmountToWin = item.AmountToWin.ToString("0.##")

                        //    }
                        //});
                    }
                    catch
                    {
                    }
                    continue;
                }

                item.Status = "lost";

                if (user.UserType == 0)
                {
                    MessagingService.SendMail($"{user.FirstName} {user.LastName}", user.Email, "Ntoboafund Yeild Failure", $"Sorry {user.FirstName} {user.LastName}, your Business Ntoboa of {item.Amount} Cedi(s) on {item.Date} has won you {item.Amount * Constants.PointConstant} points. Invest more to increase your chances of winning. Please Try Again");

                    MessagingService.SendSms(user.PhoneNumber, $"Sorry {user.FirstName} {user.LastName}, your Business Ntoboa of {item.Amount} Cedi(s) on {item.Date} has won you {item.Amount * Constants.PointConstant} points. Invest more to increase your chances of winning. Please Try Again");
                }
                context.Entry(item).State = EntityState.Modified;
            }

            return(WinnerIds);
        }