Search in sources :

Example 1 with TriviaGame

use of tk.ardentbot.utils.rpg.TriviaGame in project Ardent by adamint.

the class TriviaChecker method check.

static void check(MessageReceivedEvent event) throws Exception {
    if (event.isFromType(ChannelType.TEXT)) {
        User user = event.getAuthor();
        Guild guild = event.getGuild();
        Shard shard = GuildUtils.getShard(guild);
        TextChannel channel = event.getTextChannel();
        for (Iterator<TriviaGame> iterator = Trivia.gamesInSession.iterator(); iterator.hasNext(); ) {
            TriviaGame triviaGame = iterator.next();
            if (triviaGame.getGuildId().equalsIgnoreCase(guild.getId()) && triviaGame.getTextChannelId().equalsIgnoreCase(channel.getId())) {
                if (!triviaGame.isAnsweredCurrentQuestion()) {
                    if (triviaGame.isSolo() && !triviaGame.getCreator().equalsIgnoreCase(user.getId()))
                        return;
                    String content = event.getMessage().getContent();
                    if (triviaGame.getCurrentTriviaQuestion() != null) {
                        final boolean[] correct = { false };
                        triviaGame.getCurrentTriviaQuestion().getAnswers().forEach(a -> {
                            if (a.equalsIgnoreCase(content))
                                correct[0] = true;
                        });
                        if (correct[0]) {
                            triviaGame.addPoint(user);
                            shard.help.sendEditedTranslation("{0} got it right!", user, channel, user.getAsMention());
                            if (triviaGame.getRound() != triviaGame.getTotalRounds()) {
                                Trivia.dispatchRound(guild, channel, guild.getMemberById(triviaGame.getCreator()).getUser(), triviaGame, triviaGame.getEx());
                            } else
                                triviaGame.finish(shard, shard.help);
                        }
                    }
                }
            }
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) User(net.dv8tion.jda.core.entities.User) TriviaGame(tk.ardentbot.utils.rpg.TriviaGame) Guild(net.dv8tion.jda.core.entities.Guild) Shard(tk.ardentbot.main.Shard)

Example 2 with TriviaGame

use of tk.ardentbot.utils.rpg.TriviaGame in project Ardent by adamint.

the class Trivia method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Start a new trivia game", "start", "start") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            for (TriviaGame triviaGame : gamesInSession) {
                if (triviaGame.getGuildId().equalsIgnoreCase(guild.getId())) {
                    sendTranslatedMessage("There's already a game in session in this server!", channel, user);
                    return;
                }
            }
            if (gamesSettingUp.contains(guild.getId())) {
                sendTranslatedMessage("There's already a game in session in this server!", channel, user);
                return;
            }
            gamesSettingUp.add(guild.getId());
            sendTranslatedMessage("Do you want to play this solo? Type `yes` if so, or `no` if not", channel, user);
            interactiveOperation(channel, message, (soloMessage) -> {
                String content = soloMessage.getContent();
                boolean solo;
                solo = content.equalsIgnoreCase("yes");
                TriviaGame currentGame = new TriviaGame(user, solo, (TextChannel) channel, 15);
                gamesInSession.add(currentGame);
                sendTranslatedMessage("The game is starting! Type your answers in this channel. You have **15** seconds to answer " + "each question.", channel, user);
                commenceRounds(guild, (TextChannel) channel, user, currentGame);
                gamesSettingUp.remove(guild.getId());
            });
        }
    });
    subcommands.add(new Subcommand("Stop a currently active trivia game", "stop", "stop") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                if (gamesInSession.stream().filter(game -> game.getGuildId().equals(guild.getId())).count() > 0 || gamesSettingUp.contains(guild.getId())) {
                    gamesSettingUp.remove(guild.getId());
                    gamesInSession.removeIf(g -> g.getGuildId().equals(guild.getId()));
                    sendTranslatedMessage("Stopped the trivia game in session.", channel, user);
                } else {
                    sendTranslatedMessage("There isn't a trivia game running!", channel, user);
                }
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
}
Also used : Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Subcommand(tk.ardentbot.core.executor.Subcommand) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) TriviaQuestion(tk.ardentbot.utils.models.TriviaQuestion) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) TimeUnit(java.util.concurrent.TimeUnit) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) Permission(net.dv8tion.jda.core.Permission) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TriviaGame(tk.ardentbot.utils.rpg.TriviaGame) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Subcommand(tk.ardentbot.core.executor.Subcommand) TriviaGame(tk.ardentbot.utils.rpg.TriviaGame) BotException(tk.ardentbot.core.misc.logging.BotException)

Aggregations

Shard (tk.ardentbot.main.Shard)2 TriviaGame (tk.ardentbot.utils.rpg.TriviaGame)2 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Permission (net.dv8tion.jda.core.Permission)1 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)1 Guild (net.dv8tion.jda.core.entities.Guild)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 User (net.dv8tion.jda.core.entities.User)1 Command (tk.ardentbot.core.executor.Command)1 Subcommand (tk.ardentbot.core.executor.Subcommand)1 BotException (tk.ardentbot.core.misc.logging.BotException)1 GuildUtils (tk.ardentbot.utils.discord.GuildUtils)1 TriviaQuestion (tk.ardentbot.utils.models.TriviaQuestion)1