use of com.sx4.bot.entities.games.GameState in project Sx4 by sx4-discord-bot.
the class GameCommand method leaderboard.
@Command(value = "leaderboard", aliases = { "lb" }, description = "View the users who have played/won/lost/drawn the most games")
@CommandId(457)
@Redirects({ "lb games", "leaderboard games", "lb game", "leaderboard game" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void leaderboard(Sx4CommandEvent event, @Argument(value = "games") @Endless(minArguments = 0) GameType[] gameTypes, @Option(value = "server", aliases = { "guild" }, description = "Filters by only users in the current server") boolean guild, @Option(value = "state", description = "Filters whether the leaderboard should be for wins, played, losses or draws") GameState state) {
List<Bson> filters = new ArrayList<>();
if (gameTypes.length > 0) {
filters.add(Filters.in("type", Arrays.stream(gameTypes).map(GameType::getId).collect(Collectors.toList())));
}
if (state != null) {
filters.add(Filters.eq("state", state.getId()));
}
List<Bson> pipeline = List.of(Aggregates.project(Projections.include("state", "type", "userId")), Aggregates.match(filters.isEmpty() ? Filters.empty() : Filters.and(filters)), Aggregates.group("$userId", Accumulators.sum("count", 1L)), Aggregates.sort(Sorts.descending("count")));
event.getMongo().aggregateGames(pipeline).whenComplete((games, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Map.Entry<User, Long>> users = new ArrayList<>();
AtomicInteger userIndex = new AtomicInteger(-1);
int i = 0;
for (Document game : games) {
User user = event.getShardManager().getUserById(game.getLong("_id"));
if (user == null) {
continue;
}
if (!event.getGuild().isMember(user) && guild) {
continue;
}
i++;
users.add(Map.entry(user, game.getLong("count")));
if (user.getIdLong() == event.getAuthor().getIdLong()) {
userIndex.set(i);
}
}
if (users.isEmpty()) {
event.replyFailure("There are no users which fit into this leaderboard").queue();
return;
}
PagedResult<Map.Entry<User, Long>> paged = new PagedResult<>(event.getBot(), users).setPerPage(10).setCustomFunction(page -> {
int rank = userIndex.get();
EmbedBuilder embed = new EmbedBuilder().setTitle("Games Leaderboard").setFooter(event.getAuthor().getName() + "'s Rank: " + (rank == -1 ? "N/A" : NumberUtility.getSuffixed(rank)) + " | Page " + page.getPage() + "/" + page.getMaxPage(), event.getAuthor().getEffectiveAvatarUrl());
page.forEach((entry, index) -> embed.appendDescription(String.format("%d. `%s` - %,d game%s\n", index + 1, MarkdownSanitizer.escape(entry.getKey().getAsTag()), entry.getValue(), entry.getValue() == 1 ? "" : "s")));
return new MessageBuilder().setEmbeds(embed.build());
});
paged.execute(event);
});
}
use of com.sx4.bot.entities.games.GameState in project Sx4 by sx4-discord-bot.
the class RPSCommand method stats.
@Command(value = "stats", description = "View some stats about your personal rps record")
@CommandId(296)
@Examples({ "rps stats", "rps stats @Shea#6653" })
public void stats(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
User user = member == null ? event.getAuthor() : member.getUser();
Bson filter = Filters.and(Filters.eq("userId", user.getIdLong()), Filters.eq("type", GameType.ROCK_PAPER_SCISSORS.getId()));
List<Document> games = event.getMongo().getGames(filter, Projections.include("state")).into(new ArrayList<>());
if (games.isEmpty()) {
event.replyFailure("That user has not played rock paper scissors yet").queue();
return;
}
int wins = 0, draws = 0, losses = 0, total = 0;
for (Document game : games) {
GameState state = GameState.fromId(game.getInteger("state"));
if (state == GameState.WIN) {
wins++;
} else if (state == GameState.DRAW) {
draws++;
} else if (state == GameState.LOSS) {
losses++;
}
total++;
}
EmbedBuilder embed = new EmbedBuilder().setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl()).setDescription(String.format("Wins: %,d\nDraws: %,d\nLosses: %,d\n\nWin Percentage: %s%%", wins, draws, losses, NumberUtility.DEFAULT_DECIMAL_FORMAT.format(((double) wins / total) * 100)));
event.reply(embed.build()).queue();
}
Aggregations