Search in sources :

Example 1 with GuildMessageReceivedEvent

use of net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.

the class GuessTheNumber method call.

@Override
public void call(GameLobby lobby, List<String> players) {
    // This class is not using Game<T>#callDefault due to it being custom/way too different from the default ones (aka give hints/etc)
    InteractiveOperations.create(lobby.getChannel(), Long.parseLong(lobby.getPlayers().get(0)), 60, new InteractiveOperation() {

        @Override
        public int run(GuildMessageReceivedEvent e) {
            final var channel = lobby.getChannel();
            if (!e.getChannel().getId().equals(channel.getId())) {
                return Operation.IGNORED;
            }
            final var contentRaw = e.getMessage().getContentRaw();
            final var languageContext = lobby.getLanguageContext();
            for (var s : MantaroData.config().get().getPrefix()) {
                if (contentRaw.startsWith(s)) {
                    return Operation.IGNORED;
                }
            }
            if (players.contains(e.getAuthor().getId())) {
                if (contentRaw.equalsIgnoreCase("end")) {
                    channel.sendMessageFormat(languageContext.get("commands.game.number.ended_game"), EmoteReference.CORRECT, number).queue();
                    lobby.startNextGame(true);
                    return Operation.COMPLETED;
                }
                if (contentRaw.equalsIgnoreCase("endlobby")) {
                    channel.sendMessageFormat(languageContext.get("commands.game.lobby.ended_lobby"), EmoteReference.CORRECT).queue();
                    lobby.getGamesToPlay().clear();
                    lobby.startNextGame(true);
                    return Operation.COMPLETED;
                }
                int parsedAnswer;
                try {
                    parsedAnswer = Integer.parseInt(contentRaw);
                } catch (NumberFormatException ex) {
                    channel.sendMessageFormat(languageContext.get("commands.game.number.nan"), EmoteReference.ERROR).queue();
                    attempts = attempts + 1;
                    return Operation.IGNORED;
                }
                if (contentRaw.equals(String.valueOf(number))) {
                    var unifiedPlayer = UnifiedPlayer.of(e.getAuthor(), config.getCurrentSeason());
                    var player = unifiedPlayer.getPlayer();
                    var seasonalPlayer = unifiedPlayer.getSeasonalPlayer();
                    var gains = 140;
                    unifiedPlayer.addMoney(gains);
                    player.getData().setGamesWon(player.getData().getGamesWon() + 1);
                    seasonalPlayer.getData().setGamesWon(seasonalPlayer.getData().getGamesWon() + 1);
                    if (player.getData().getGamesWon() == 100) {
                        player.getData().addBadgeIfAbsent(Badge.GAMER);
                    }
                    if (player.getData().getGamesWon() == 1000) {
                        player.getData().addBadgeIfAbsent(Badge.ADDICTED_GAMER);
                    }
                    if (number > 90) {
                        player.getData().addBadgeIfAbsent(Badge.APPROACHING_DESTINY);
                    }
                    unifiedPlayer.saveUpdating();
                    TextChannelGround.of(e).dropItemWithChance(ItemReference.FLOPPY_DISK, 3);
                    channel.sendMessageFormat(languageContext.get("commands.game.lobby.won_game"), EmoteReference.MEGA, e.getMember().getEffectiveName(), gains).queue();
                    lobby.startNextGame(true);
                    return Operation.COMPLETED;
                }
                if (attempts >= maxAttempts) {
                    channel.sendMessageFormat(languageContext.get("commands.game.number.all_attempts_used"), EmoteReference.ERROR, number).queue();
                    // This should take care of removing the lobby, actually.
                    lobby.startNextGame(true);
                    return Operation.COMPLETED;
                }
                channel.sendMessageFormat(languageContext.get("commands.game.lobby.incorrect_answer") + "\n" + String.format(languageContext.get("commands.game.number.hint"), (parsedAnswer < number ? languageContext.get("commands.game.number.higher") : languageContext.get("commands.game.number.lower"))), EmoteReference.ERROR, (maxAttempts - attempts)).queue();
                attempts = attempts + 1;
                return Operation.IGNORED;
            }
            return Operation.IGNORED;
        }

        @Override
        public void onExpire() {
            final var channel = lobby.getChannel();
            if (channel == null) {
                GameLobby.LOBBYS.remove(Long.parseLong(lobby.getChannelId()));
                return;
            }
            channel.sendMessageFormat(lobby.getLanguageContext().get("commands.game.lobby_timed_out"), EmoteReference.ERROR, number).queue();
            GameLobby.LOBBYS.remove(channel.getIdLong());
        }

        @Override
        public void onCancel() {
            GameLobby.LOBBYS.remove(lobby.getChannel().getIdLong());
        }
    });
}
Also used : InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent)

Aggregations

GuildMessageReceivedEvent (net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent)1 InteractiveOperation (net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation)1