Esempio n. 1
0
        public async Task UpdateLotteries(Dictionary <ulong, Lottery> lotterycache, PopeAIDB Context)
        {
            foreach (Lottery lottery in Context.Lotteries)
            {
                if (DateTime.UtcNow > lottery.EndDate)
                {
                    lotterycache.Remove(lottery.PlanetId);
                    int total = (int)await Context.LotteryTickets.SumAsync(x => (double)x.Tickets);

                    Random rnd = new Random();
                    ulong  WinningTicketNum = (ulong)rnd.Next(1, total + 1);
                    ulong  currentnum       = 1;
                    foreach (LotteryTicket ticket in Context.LotteryTickets.Where(x => x.PlanetId == lottery.PlanetId))
                    {
                        if (currentnum + ticket.Tickets >= WinningTicketNum)
                        {
                            if (lottery.Type == "message")
                            {
                                await Context.AddStat("Coins", lottery.Jackpot, lottery.PlanetId, Context);
                            }
                            User winninguser = await Context.Users.FirstOrDefaultAsync(x => x.PlanetId == lottery.PlanetId && x.UserId == ticket.UserId);

                            winninguser.Coins += lottery.Jackpot;
                            PlanetMember planetuser = await winninguser.GetAuthor();

                            //await Program.PostMessage(lottery.ChannelId, lottery.PlanetId, $"{planetuser.Nickname} has won the lottery with a jackpot of over {(ulong)lottery.Jackpot} coins!");
                            Context.LotteryTickets.Remove(ticket);
                        }
                        else
                        {
                            currentnum += ticket.Tickets;
                        }
                        Context.Lotteries.Remove(lottery);
                    }
                }
            }
            await Context.SaveChangesAsync();
        }