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()); } }
public List <ReactivePage> GetPages(IEnumerable <Player>[] groups, LeaderboardSortMode mode) { //Start the index at 1 because we are ranking players here ie. first place. int index = 1; var pages = new List <ReactivePage>(groups.Length); foreach (var group in groups) { var playerGroup = group.ToArray(); var lines = GetPlayerLines(playerGroup, index, mode); index = lines.Item1; var page = new ReactivePage(); page.Color = Color.Blue; page.Title = $"{Context.Guild.Name} - Leaderboard"; page.Description = lines.Item2; pages.Add(page); } return(pages); }
//Returns the updated index and the formatted player lines public (int, string) GetPlayerLines(Player[] players, int startValue, LeaderboardSortMode mode) { var sb = new StringBuilder(); //Iterate through the players and add their summary line to the list. foreach (var player in players) { switch (mode) { case LeaderboardSortMode.point: sb.AppendLine($"{startValue}: {player.GetDisplayNameSafe()} - `{player.Points}`"); break; case LeaderboardSortMode.wins: sb.AppendLine($"{startValue}: {player.GetDisplayNameSafe()} - `{player.Wins}`"); break; case LeaderboardSortMode.losses: sb.AppendLine($"{startValue}: {player.GetDisplayNameSafe()} - `{player.Losses}`"); break; case LeaderboardSortMode.wlr: var wlr = player.Losses == 0 ? player.Wins : (double)player.Wins / player.Losses; sb.AppendLine($"{startValue}: {player.GetDisplayNameSafe()} - `{Math.Round(wlr, 2, MidpointRounding.AwayFromZero)}`"); break; case LeaderboardSortMode.games: sb.AppendLine($"{startValue}: {player.GetDisplayNameSafe()} - `{player.Games}`"); break; } startValue++; } //Return the updated start value and the list of player lines. return(startValue, sb.ToString()); }
public Task LeaderboardAsync(LeaderboardSortMode sortMode = LeaderboardSortMode.Points) { var rgx = new Regex("[^a-zA-Z0-9 -#]"); List <string> userStrings; switch (sortMode) { case LeaderboardSortMode.Points: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Points).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Points: {x.Stats.Points}`").ToList(); break; } case LeaderboardSortMode.Win: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Wins).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Wins: {x.Stats.Wins}`").ToList(); break; } case LeaderboardSortMode.Loss: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Losses).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Losses: {x.Stats.Losses}`").ToList(); break; } case LeaderboardSortMode.Kills: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Kills).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Kills: {x.Stats.Kills}`").ToList(); break; } case LeaderboardSortMode.Deaths: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Deaths).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Deaths: {x.Stats.Deaths}`").ToList(); break; } case LeaderboardSortMode.Draws: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Draws).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Draws: {x.Stats.Draws}`").ToList(); break; } case LeaderboardSortMode.GamesPlayed: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.GamesPlayed).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Games: {x.Stats.GamesPlayed}`").ToList(); break; } default: { var users = Context.Server.Users.OrderByDescending(x => x.Stats.Points).Where(x => Context.Guild.GetUser(x.UserID) != null).ToList(); userStrings = users.Select(x => $"`{$"#{users.IndexOf(x) + 1} - {rgx.Replace(x.Username, "")}".PadRight(33)}\u200B || Points: {x.Stats.Points}`").ToList(); break; } } var pages = userStrings.SplitList(20).Select(x => new PaginatedMessage.Page { Description = string.Join("\n", x) }); var pager = new PaginatedMessage { Title = "ELO Bot Leader board", Pages = pages, Color = Color.Blue }; return(PagedReplyAsync(pager, new ReactionList { Forward = true, Backward = true, Trash = true })); }