private static async Task <Message> GetLeaderboardMessageAsync(DatabaseContext dbContext, IConfigurationRoot configuration) { var users = await SharedLeaderboardManager.GetTopUsersAndTeams(dbContext, 20, configuration.GetValue <bool>("DynamicScoring")); var eb = new CustomEmbedBuilder(); eb.WithTitle("Leaderboard"); for (int i = 0; i < users.Count; i++) { if (users[i].IsTeam) { eb.Description += $"\n**{i + 1}. {users[i].TeamName}** (team) - {users[i].Score} points"; } else { eb.Description += $"\n**{i + 1}. {users[i].WebsiteUser?.UserName ?? users[i].DiscordUsername}** - {users[i].Score} points"; } } var builder = new ComponentBuilder().WithButton("Full Leaderboard", style: ButtonStyle.Link, url: "https://imaginaryctf.org/leaderboard"); return(new Message { Embed = eb.Build(), MessageComponent = builder.Build(), Ephemeral = true }); }
public async Task <IActionResult> CtftimeAsync() { var top = await SharedLeaderboardManager.GetTopUsersAndTeams(_context, int.MaxValue); var teams = new List <dynamic>(); for (int i = 0; i < top.Count; i++) { teams.Add(new { Pos = i + 1, Team = top[i].IsTeam ? top[i].TeamName : (top[i].WebsiteUser?.UserName ?? top[i].DiscordUsername), Score = top[i].Score }); } return(Json(new { standings = teams })); }
public static async Task UpdateLeaderboard(DiscordSocketClient client, DatabaseContext context, bool dynamicScoring = false) { var config = await context.Configuration.FirstOrDefaultAsync(); if (config == null || config.GuildId == 0 || config.LeaderboardChannelId == 0) { return; } var channel = client.GetGuild(config.GuildId).GetTextChannel(config.LeaderboardChannelId); var users = await SharedLeaderboardManager.GetTopUsersAndTeams(context, 20, dynamicScoring); var embedBuilder = new CustomEmbedBuilder(); embedBuilder.WithTitle("Leaderboard"); for (int i = 0; i < users.Count; i++) { if (users[i].IsTeam) { embedBuilder.Description += $"\n**{i + 1}. {users[i].TeamName}** (team) - {users[i].Score} points"; } else { embedBuilder.Description += $"\n**{i + 1}. {users[i].WebsiteUser?.UserName ?? users[i].DiscordUsername}** - {users[i].Score} points"; } } var message = (await channel.GetMessagesAsync(1).FlattenAsync()).FirstOrDefault(); if (message != null && message.Author.Id == client.CurrentUser.Id) { await((IUserMessage)message).ModifyAsync(x => x.Embed = embedBuilder.Build()); } else { var builder = new ComponentBuilder().WithButton("Full Leaderboard", style: ButtonStyle.Link, url: "https://imaginaryctf.org/leaderboard"); await channel.SendMessageAsync(embed : embedBuilder.Build(), component : builder.Build()); } }
public static async Task UpdateRoles(DiscordSocketClient client, DatabaseContext context) { Config config = await context.Configuration.FirstOrDefaultAsync(); if (config == null || config.GuildId == 0 || config.FirstPlaceRoleId == 0 || config.SecondPlaceRoleId == 0 || config.ThirdPlaceRoleId == 0) { return; } var firstRole = client.GetGuild(config.GuildId).GetRole(config.FirstPlaceRoleId); var secondRole = client.GetGuild(config.GuildId).GetRole(config.SecondPlaceRoleId); var thirdRole = client.GetGuild(config.GuildId).GetRole(config.ThirdPlaceRoleId); if (firstRole == null || secondRole == null || thirdRole == null) { return; } var rolesToRemove = new List <SocketRole> { firstRole, secondRole, thirdRole }; var users = await SharedLeaderboardManager.GetTopUsersAndTeams(context, 5); var top = users.FirstOrDefault(); if (top != null && top.DiscordId != 0) { var topUser = client.GetGuild(config.GuildId).GetUser(top.DiscordId); if (topUser != null && !topUser.Roles.Any(x => x.Id == firstRole.Id)) { await topUser.SendMessageAsync("Congrats for reaching the top of the leaderboard :partying_face:"); } } for (int i = 0; i < users.Count; i++) { var user = users[i]; if (user.DiscordId == 0) { continue; } var dUser = client.GetGuild(config.GuildId).GetUser(user.DiscordId); if (dUser == null) { continue; } switch (i) { case 0: if (!dUser.Roles.Any(x => x.Id == firstRole.Id)) { await dUser.RemoveRolesAsync(rolesToRemove); await dUser.AddRoleAsync(firstRole); } break; case 1: if (!dUser.Roles.Any(x => x.Id == secondRole.Id)) { await dUser.RemoveRolesAsync(rolesToRemove); await dUser.AddRoleAsync(secondRole); } break; case 2: if (!dUser.Roles.Any(x => x.Id == thirdRole.Id)) { await dUser.RemoveRolesAsync(rolesToRemove); await dUser.AddRoleAsync(thirdRole); } break; default: await dUser.RemoveRolesAsync(rolesToRemove); break; } } }
public async Task OnGetAsync() { Items = await SharedLeaderboardManager.GetTopUsersAndTeams(_context, int.MaxValue, _configuration.GetValue <bool>("DynamicScoring")); }