Esempio n. 1
0
        /// <summary>
        /// 随机抽取Winner
        /// </summary>
        /// <param name="participants">参与者</param>
        /// <param name="nums">抽取人数</param>
        /// <returns></returns>
        private List <Winner> GetWinners(List <Participant> participants, Int32 nums)
        {
            List <Winner> winner = new List <Winner>(nums);

            if (nums >= participants.Count)
            {
                participants.ForEach(p => winner.Add(Winner.FromParticipant(p)));
            }
            else
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());

                for (var i = 0; i < nums; i++)
                {
                    winner.Add(Winner.FromParticipant(participants[i]));
                }

                for (var i = nums; i < participants.Count; i++)
                {
                    Int32 r = random.Next(i + 1);
                    if (r < nums)
                    {
                        winner[r] = Winner.FromParticipant(participants[i]);
                    }
                }
            }
            return(winner);
        }
Esempio n. 2
0
        /// <summary>
        /// 随机抽取Winner
        /// </summary>
        /// <param name="participants">参与者</param>
        /// <param name="nums">抽取人数</param>
        /// <returns></returns>
        private IEnumerable <Winner> GetWinnersOld(List <Participant> participants, Int32 nums)
        {
            List <Participant> wp     = new List <Participant>();
            Random             random = new Random(Guid.NewGuid().GetHashCode());

            while (wp.Count < nums)
            {
                Int32 n = random.Next(0, participants.Count() - 1);
                if (!wp.Contains(participants[n]))
                {
                    wp.Add(participants[n]);
                }
            }

            foreach (var p in wp)
            {
                yield return(Winner.FromParticipant(p));
            }
        }
Esempio n. 3
0
        private async static Task InsertData(MCRollDbContext context)
        {
            Roll roll1 = new Roll()
            {
                Name = "roll1",
                Description = "熔炼炉熔炼炉熔炼炉熔炼炉熔熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉",
                Creator = "roll1c",
                CreateTime = DateTime.Now,
                EndTime = DateTime.Now + TimeSpan.FromDays(1),
                WinnerNumber = 1,
                IsOpen = true
            };
            Roll roll2 = new Roll()
            {
                Name = "roll2",
                Description = "roll 2",
                Creator = "roll2c",
                CreateTime = DateTime.Now - TimeSpan.FromDays(1),
                EndTime = DateTime.Now,
                WinnerNumber = 1,
                IsOpen = false,
            };
            Roll roll3 = new Roll()
            {
                Name = "roll3",
                Description = "熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉熔炼炉",
                Creator = "roll3c",
                CreateTime = DateTime.Now - TimeSpan.FromDays(1),
                EndTime = DateTime.Now,
                WinnerNumber = 1,
                IsOpen = false
            };
            Roll roll4 = new Roll()
            {
                Name = "roll4",
                Description = "roll 4",
                Creator = "roll4c",
                CreateTime = DateTime.Now,
                EndTime = DateTime.Now + TimeSpan.FromDays(1),
                WinnerNumber = 1,
                IsOpen = true
            };
            context.Add(roll1);
            context.Add(roll2);
            context.Add(roll3);
            context.Add(roll4);

            Participant participant1 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User1",
                Roll = roll1
            };
            Participant participant2 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User2",
                Roll = roll1
            };
            Participant participant3 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User1",
                Roll = roll2
            };
            Participant participant4 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User2",
                Roll = roll2
            };
            Participant participant5 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User1",
                Roll = roll3
            };
            Participant participant6 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User2",
                Roll = roll3
            };
            Participant participant7 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User1",
                Roll = roll4
            };
            Participant participant8 = new Participant()
            {
                Username = "******",
                Email = "*****@*****.**",
                Comment = "I am Test User2",
                Roll = roll4
            };
            context.Add(participant1);
            context.Add(participant2);
            context.Add(participant3);
            context.Add(participant4);
            context.Add(participant5);
            context.Add(participant6);
            context.Add(participant7);
            context.Add(participant8);

            Winner winner1 = Winner.FromParticipant(participant3);
            Winner winner2 = Winner.FromParticipant(participant6);
            context.Add(winner1);
            context.Add(winner2);

            await context.SaveChangesAsync();
        }