Exemplo n.º 1
0
        public async Task GetPlayerAsync(IGuildUser guildUser)
        {
            using (var context = new GuildContext())
            {
                var users = context.Users
                            .Include(x => x.Rank)
                            .Where(x => x.Guild.DBDiscordID == Context.Guild.Id.ToString() && (x.PointDisplay != PointDisplay.HideAll || x.PointDisplay != PointDisplay.HideTotal))
                            .OrderByDescending(x => x.TotalPoints)
                            .AsAsyncEnumerable();

                ProfessorMewData.Interfaces.Guild.IUser user = null;
                int ranking = 0;
                await foreach (var entry in users)
                {
                    if (entry.DiscordID == guildUser.Id)
                    {
                        user = entry;
                        user.GuildRanking = ranking;
                        break;
                    }

                    ranking++;
                }

                if (user is null)
                {
                    await ReplyAsync("User was not found in the database.");

                    return;
                }

                var embed = EmbedUtils.CreatePlayerDataEmbed(user);
                await ReplyAsync(embed : embed);
            }
        }
Exemplo n.º 2
0
        public async Task UpdatePointLeaderboardAsync()
        {
            const int topLeaderboardCount     = 10;
            const int monthlyLeaderboardCount = 3;

            using (var context = new GuildContext())
            {
                await context.Database.EnsureCreatedAsync();

                var guild = await context.Guilds
                            .Include(x => x.Users)
                            .ThenInclude(x => x.Rank)
                            .Include(x => x.Channels)
                            .SingleOrDefaultAsync(x => x.DBDiscordID == Context.Guild.Id.ToString());

                if (guild is null)
                {
                    await ReplyAsync("Guild was not found in the database");

                    return;
                }
                var savedChannel = guild.GetChannel("Points");
                var textChannel  = Context.Guild.GetTextChannel(savedChannel.DiscordID);
                await Misc.DiscordUtils.DeleteMessagesAsync(textChannel, 2);

                var playersMonthly = guild.GetUsers(PointDisplay.ShowAll, PointDisplay.HideTotal).ToList();
                var playersTotal   = guild.GetUsers(PointDisplay.ShowAll, PointDisplay.HideMonthly).ToList();
                if (playersMonthly is not null || playersMonthly.Count > 0)
                {
                    playersMonthly.RemoveNonExistingUsers(Context);
                    playersMonthly.SetNames(Context, false);
                    playersMonthly.Sort(delegate(ProfessorMewData.Interfaces.Guild.IUser u1, ProfessorMewData.Interfaces.Guild.IUser u2)
                    {
                        return(u2.MonthPoints.CompareTo(u1.MonthPoints));
                    });
                }
                if (playersTotal is not null || playersTotal.Count > 0)
                {
                    playersTotal.RemoveNonExistingUsers(Context);
                    playersTotal.SetNames(Context, false);
                    playersTotal.Sort(delegate(ProfessorMewData.Interfaces.Guild.IUser u1, ProfessorMewData.Interfaces.Guild.IUser u2)
                    {
                        return(u2.TotalPoints.CompareTo(u1.TotalPoints));
                    });
                }

                var monthlyEmbed = EmbedUtils.CreatePointLeaderboardEmbed("The Crystal Wolves monthly leaderboard", "Here you can see the top 3 players who have earned the most points this month so far", Discord.Color.DarkBlue, playersMonthly, monthlyLeaderboardCount);
                var totalEmbed   = EmbedUtils.CreatePointLeaderboardEmbed("The Crystal Wolves leaderboard", "Here you can see the top 10 players in our guild", Discord.Color.DarkPurple, playersTotal, topLeaderboardCount);

                if (monthlyEmbed is not null)
                {
                    await textChannel.SendMessageAsync(embed : totalEmbed);
                }
                if (totalEmbed is not null)
                {
                    await textChannel.SendMessageAsync(embed : monthlyEmbed);
                }
            }
        }
Exemplo n.º 3
0
        public async Task GetPlayerProfileAsync()
        {
            using (var context = new GuildContext())
            {
                await context.Database.EnsureCreatedAsync();

                var users = context.Users
                            .Include(x => x.Rank)
                            .Where(x => x.Guild.DBDiscordID == Context.Guild.Id.ToString() && (x.PointDisplay != PointDisplay.HideAll || x.PointDisplay != PointDisplay.HideTotal))
                            .OrderByDescending(x => x.TotalPoints)
                            .AsAsyncEnumerable();

                ProfessorMewData.Interfaces.Guild.IUser user = null;
                int ranking = 0;
                await foreach (var entry in users)
                {
                    if (entry.DiscordID == Context.User.Id)
                    {
                        user = entry;
                        user.GuildRanking = ranking;
                        break;
                    }

                    ranking++;
                }

                if (user is null)
                {
                    await ReplyAsync("User was not found in the database.");

                    return;
                }
                user.AvatarUrl = user.GetAvatarUrl(Context);
                user.Name      = user.GetName(Context);
                var profilePicture = await user.DownloadAvatarAsync(Services.GetRequiredService <HttpClient>());

                //string path = PointsProfileOLD.CreatePlayerProfile(user);
                //try
                //{
                //    await Context.Channel.SendFileAsync(path);
                //}
                //catch (Exception)
                //{
                //    await Context.Channel.SendMessageAsync(embed: EmbedUtils.CreatePointsProfileEmbed(user));
                //}
                try
                {
                    await Context.Channel.SendFileAsync(await PointsProfile.CreateProfileImage(user, profilePicture));
                }
                catch
                {
                    await Context.Channel.SendMessageAsync(embed : EmbedUtils.CreatePointsProfileEmbed(user));
                }
            }
        }
Exemplo n.º 4
0
        public async Task ReducePointsAsync(ulong discordID, int points, bool reduceMonthly = true)
        {
            using (var context = new GuildContext())
            {
                await context.Database.EnsureCreatedAsync();

                var guild = await context.Guilds
                            .Include(x => x.Ranks)
                            .Include(x => x.Links)
                            .SingleOrDefaultAsync(x => x.DBDiscordID == Context.Guild.Id.ToString());

                if (guild is null)
                {
                    await ReplyAsync("Guild was not found in the database.");

                    return;
                }
                var user = await context.Users
                           .Include(x => x.Rank)
                           .SingleOrDefaultAsync(x => x.DBDiscordID == discordID.ToString() && x.Guild == guild);

                if (user is null)
                {
                    await ReplyAsync("User was not found in the database");

                    return;
                }
                user.ReducePoints(points, reduceMonthly);
                if (user.RankChanged())
                {
                    var newRank = guild.GetRank(user.TotalPoints);
                    var link    = guild.GetLink("Rankdown");
                    user.Rank = newRank;
                    var guildUser = Context.Guild.GetUser(user.DiscordID);
                    await guildUser.SendMessageAsync(embed : EmbedUtils.CreateRankDownEmbed(user, link.URL));
                    await ReplyAsync((await Misc.DiscordUtils.TryAddRoleAsync(guildUser, newRank)).Value);
                }
                await context.SaveChangesAsync();
            }

            await ReplyAsync($"<@{discordID}> lost {points} points");
        }