Ejemplo n.º 1
0
        public async Task UpdateDailyTasks(PopeAIDB Context)
        {
            foreach (DailyTask task in Context.DailyTasks)
            {
                if (DateTime.UtcNow.Day != task.LastDayUpdated.Day)
                {
                    Context.DailyTasks.Remove(task);
                }
            }
            await Context.SaveChangesAsync();

            foreach (User user in Context.Users)
            {
                PlanetMember member = await Cache.GetPlanetMember(user.UserId, user.PlanetId);

                if (await Context.DailyTasks.FirstOrDefaultAsync(x => x.MemberId == member.Id) == null)
                {
                    // create 3 new tasks
                    await GenerateNewDailyTasks(Context, member.Id);
                }
            }
            await Context.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task UpdateRoleIncomes(List <Planet> planets, bool force, PopeAIDB Context)
        {
            RoleIncomes first = await Context.RoleIncomes.FirstOrDefaultAsync();

            if (first == null)
            {
                first             = new RoleIncomes();
                first.LastPaidOut = DateTime.UtcNow;
                first.RoleId      = 0;
            }
            if (DateTime.UtcNow > first.LastPaidOut.AddHours(1) || first.RoleId == 0 || force)
            {
                List <PlanetMemberInfo>         memberinfo        = new List <PlanetMemberInfo>();
                Dictionary <ulong, RoleIncomes> RoleIncomeRoleIds = new Dictionary <ulong, RoleIncomes>();
                List <ulong> PlanetIds = new List <ulong>();
                foreach (RoleIncomes roleincome in Context.RoleIncomes)
                {
                    RoleIncomeRoleIds.Add(roleincome.RoleId, roleincome);
                    roleincome.LastPaidOut = DateTime.UtcNow;
                    if (PlanetIds.Contains(roleincome.PlanetId) == false)
                    {
                        PlanetIds.Add(roleincome.PlanetId);
                    }
                }
                foreach (ulong planetid in PlanetIds)
                {
                    CurrentStat current = await Context.CurrentStats.FirstOrDefaultAsync(x => x.PlanetId == planetid);

                    foreach (PlanetMember member in await(await Cache.GetPlanet(planetid)).GetMembers())
                    {
                        foreach (ulong roleid in member.RoleIds)
                        {
                            if (RoleIncomeRoleIds.ContainsKey(roleid))
                            {
                                RoleIncomes roleincome = RoleIncomeRoleIds[roleid];
                                (await Context.Users.FirstOrDefaultAsync(x => x.UserId == member.User_Id && x.PlanetId == member.Planet_Id)).Coins += roleincome.Income;
                                current.NewCoins += roleincome.Income;
                            }
                        }
                    }
                }
            }
            await Context.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task UpdateStats(PopeAIDB Context)
        {
            CurrentStat first = await Context.CurrentStats.FirstOrDefaultAsync();

            if (first == null)
            {
                first = new CurrentStat();
                first.LastStatUpdate = DateTime.UtcNow;
            }
            if (Context.CurrentStats.Count() == 0)
            {
                return;
            }
            Random rnd = new Random();

            if (DateTime.UtcNow > first.LastStatUpdate.AddHours(1))
            {
                foreach (CurrentStat currentstat in Context.CurrentStats)
                {
                    Stat newstat = new Stat();
                    newstat.Time              = DateTime.UtcNow;
                    newstat.NewCoins          = currentstat.NewCoins;
                    newstat.MessagesSent      = currentstat.MessagesSent;
                    newstat.PlanetId          = currentstat.PlanetId;
                    newstat.MessagesUsersSent = currentstat.MessagesUsersSent;
                    ulong num = (ulong)rnd.Next(1, int.MaxValue);
                    while (await Context.Stats.FirstOrDefaultAsync(x => x.Id == num) != null)
                    {
                        num = (ulong)rnd.Next(1, int.MaxValue);
                    }
                    newstat.Id = num;
                    await Context.Stats.AddAsync(newstat);

                    currentstat.NewCoins          = 0;
                    currentstat.MessagesSent      = 0;
                    currentstat.MessagesUsersSent = 0;
                    currentstat.LastStatUpdate    = DateTime.UtcNow;
                }
            }
            await Context.SaveChangesAsync();
        }
Ejemplo n.º 4
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();
        }