示例#1
0
        public static async Task <Embed> GetLeaderboardAsync(ulong?guildId = null, Func <User, double> stat = null, int begin = 1, int end = 10)
        {
            var usersInGuild = guildId != null?Program.Client.GetGuild(guildId.Value).Users.Select(x => x.Id).ToHashSet() : null;

            var users = guildId != null?await(await StaticBase.Database.GetCollection <User>("Users").FindAsync(x => usersInGuild.Contains(x.Id))).ToListAsync()
                            : await(await StaticBase.Database.GetCollection <User>("Users").FindAsync(x => true)).ToListAsync();

            if (stat == null)
            {
                stat = x => x.CalcCurLevelDouble();
            }

            users = users.OrderByDescending(x => stat(x)).Skip(begin - 1).Take(end - (begin - 1)).ToList();

            List <KeyValuePair <string, double> > stats = new List <KeyValuePair <string, double> >();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < end - (begin - 1); i++)
            {
                if (end - begin < 10)
                {
                    sb.Append($"#{begin+i}: {(await StaticBase.GetUserAsync(users[i].Id))?.Mention ?? $"<@{users[i].Id}>"}\n");
                }
                stats.Add(KeyValuePair.Create("" + (begin + i), stat(users[i])));
            }

            var embed = new EmbedBuilder();

            return(embed.WithCurrentTimestamp().WithImageUrl(ColumnPlot.DrawPlotSorted(guildId + "Leaderboard", stats))
                   .WithDescription(sb.ToString()).Build());
        }
        public async Task <Embed> GetLeaderboardAsync(Func <TwitchUser, double> stat = null, int begin = 1, int end = 10)
        {
            if (stat == null)
            {
                stat = x => x.Points;
            }

            users = users.OrderByDescending(x => stat(x)).Skip(begin - 1).Take(end - (begin - 1)).ToList();

            List <KeyValuePair <string, double> > stats = new List <KeyValuePair <string, double> >();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < end - (begin - 1); i++)
            {
                if (end - begin < 10)
                {
                    sb.Append($"#{begin + i}: {(await StaticBase.GetUserAsync(users[i].DiscordId))?.Mention ?? $"<@{users[i].DiscordId}>"}\n");
                }
                stats.Add(KeyValuePair.Create("" + (begin + i), stat(users[i])));
            }

            var embed = new EmbedBuilder();

            return(embed.WithCurrentTimestamp().WithImageUrl(ColumnPlot.DrawPlotSorted(DiscordId + "Leaderboard", stats))
                   .WithDescription(sb.ToString()).Build());
        }