Esempio n. 1
0
        public virtual async Task LeaderboardAsync(LeaderboardSortMode mode = LeaderboardSortMode.points)
        {
            using (var db = new Database())
            {
                //Retrieve players in the current guild from database
                var users = db.Players.AsNoTracking().Where(x => x.GuildId == Context.Guild.Id);

                //Order players by score and then split them into groups of 20 for pagination
                IEnumerable <Player>[] userGroups;
                switch (mode)
                {
                case LeaderboardSortMode.point:
                    userGroups = users.OrderByDescending(x => x.Points).SplitList(20).ToArray();
                    break;

                case LeaderboardSortMode.wins:
                    userGroups = users.OrderByDescending(x => x.Wins).SplitList(20).ToArray();
                    break;

                case LeaderboardSortMode.losses:
                    userGroups = users.OrderByDescending(x => x.Losses).SplitList(20).ToArray();
                    break;

                case LeaderboardSortMode.wlr:
                    userGroups = users.OrderByDescending(x => x.Losses == 0 ? x.Wins : (double)x.Wins / x.Losses).SplitList(20).ToArray();
                    break;

                case LeaderboardSortMode.games:
                    userGroups = users.ToList().OrderByDescending(x => x.Games).SplitList(20).ToArray();
                    break;

                default:
                    return;
                }
                if (userGroups.Length == 0)
                {
                    await SimpleEmbedAsync("There are no registered users in this server yet.", Color.Blue);

                    return;
                }

                //Convert the groups into formatted pages for the response message
                var pages = GetPages(userGroups, mode);

                if (!Premium.IsPremium(Context.Guild.Id))
                {
                    pages = pages.Take(1).ToList();
                    pages.Add(new ReactivePage
                    {
                        Description = $"In order to access a complete leaderboard, consider joining ELO premium at {Premium.PremiumConfig.AltLink}, patrons must also be members of the ELO server at: {Premium.PremiumConfig.ServerInvite}"
                    });
                }

                //Construct a paginated message with each of the leaderboard pages
                var callback = new ReactivePager(pages).ToCallBack();
                callback.Precondition = async(x, y) => y.UserId == Context.User.Id;
                await PagedReplyAsync(callback.WithDefaultPagerCallbacks().WithJump());
            }
        }
Esempio n. 2
0
        public virtual async Task ShowLobbyLeaderboardAsync(ISocketMessageChannel channel = null)
        {
            if (channel == null)
            {
                channel = Context.Channel;
            }

            if (!PremiumService.IsPremium(Context.Guild.Id))
            {
                await SimpleEmbedAsync($"This is a premium only command. " +
                                       $"In order to get premium must become an ELO premium subscriber at {PremiumService.PremiumConfig.AltLink} join the server " +
                                       $"{PremiumService.PremiumConfig.ServerInvite} to recieve your role and then run the `claimpremium` command in your server.");

                return;
            }

            using (var db = new Database())
            {
                var lobby = db.GetLobby(channel);
                if (lobby == null)
                {
                    await SimpleEmbedAndDeleteAsync("Channel is not a lobby.", Color.Red);

                    return;
                }

                var updates = db.ScoreUpdates.AsNoTracking().Where(x => x.ChannelId == channel.Id).ToArray().GroupBy(x => x.UserId);
                var infos   = new Dictionary <ulong, int>();
                foreach (var group in updates)
                {
                    infos[group.Key] = group.Sum(x => x.ModifyAmount);
                }

                var groups = infos.OrderByDescending(x => x.Value).SplitList(20).ToArray();
                int index  = 1;
                var pages  = new List <ReactivePage>();
                foreach (var group in groups)
                {
                    var playerGroup = group.ToArray();
                    var lines       = group.Select(x => $"{index++}: {MentionUtils.MentionUser(x.Key)} - `{x.Value}`").ToArray();
                    //index += lines.Length;
                    var page = new ReactivePage();
                    page.Color       = Color.Blue;
                    page.Title       = $"{channel.Name} - Leaderboard";
                    page.Description = string.Join("\n", lines);
                    pages.Add(page);
                }

                await PagedReplyAsync(new ReactivePager
                {
                    Pages = pages
                }.ToCallBack().WithDefaultPagerCallbacks().WithJump());
            }
        }