Search in sources :

Example 1 with GameState

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);
    });
}
Also used : User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Document(org.bson.Document) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PagedResult(com.sx4.bot.paged.PagedResult) Redirects(com.sx4.bot.annotations.command.Redirects) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId)

Example 2 with GameState

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();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) GameState(com.sx4.bot.entities.games.GameState) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

Command (com.jockie.bot.core.command.Command)2 CommandId (com.sx4.bot.annotations.command.CommandId)2 Sx4Command (com.sx4.bot.core.Sx4Command)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 User (net.dv8tion.jda.api.entities.User)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)1 Examples (com.sx4.bot.annotations.command.Examples)1 Redirects (com.sx4.bot.annotations.command.Redirects)1 GameState (com.sx4.bot.entities.games.GameState)1 PagedResult (com.sx4.bot.paged.PagedResult)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)1