Search in sources :

Example 1 with JamTheme

use of net.javadiscord.javabot.systems.jam.model.JamTheme in project JavaBot by Java-Discord.

the class ToSubmissionTransition method determineWinner.

private JamTheme determineWinner(Map<JamTheme, Integer> voteCounts) {
    JamTheme winningTheme = null;
    int winningThemeVotes = 0;
    for (var entry : voteCounts.entrySet()) {
        if (entry.getValue() > winningThemeVotes) {
            winningTheme = entry.getKey();
            winningThemeVotes = entry.getValue();
        }
    }
    return winningTheme;
}
Also used : JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme)

Example 2 with JamTheme

use of net.javadiscord.javabot.systems.jam.model.JamTheme in project JavaBot by Java-Discord.

the class ToSubmissionTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    JamMessageRepository messageRepository = new JamMessageRepository(con);
    List<JamTheme> themes = new JamThemeRepository(con).getThemes(jam);
    long themeVotingMessageId = messageRepository.getMessageId(jam, "theme_voting");
    var votes = channelManager.getThemeVotes(themeVotingMessageId, themes);
    var voteCounts = this.recordAndCountVotes(jam, votes, con);
    JamTheme winningTheme = this.determineWinner(voteCounts);
    if (winningTheme == null) {
        throw new IllegalStateException("No winning jam theme could be found.");
    }
    this.updateThemesAcceptedState(themes, winningTheme, con);
    channelManager.sendChosenThemeMessage(voteCounts, winningTheme);
    new JamRepository(con).updateJamPhase(jam, JamPhase.SUBMISSION);
    messageRepository.removeMessageId(jam, "theme_voting");
}
Also used : JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository) JamThemeRepository(net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)

Example 3 with JamTheme

use of net.javadiscord.javabot.systems.jam.model.JamTheme in project JavaBot by Java-Discord.

the class ToSubmissionTransition method recordAndCountVotes.

private Map<JamTheme, Integer> recordAndCountVotes(Jam jam, Map<JamTheme, List<Long>> votes, Connection con) throws SQLException {
    Map<JamTheme, Integer> voteCounts = new HashMap<>();
    PreparedStatement themeVoteStmt = con.prepareStatement("INSERT INTO jam_theme_vote (jam_id, theme_name, user_id) VALUES (?, ?, ?)");
    themeVoteStmt.setLong(1, jam.getId());
    for (var entry : votes.entrySet()) {
        JamTheme theme = entry.getKey();
        themeVoteStmt.setString(2, theme.getName());
        for (long userId : entry.getValue()) {
            themeVoteStmt.setLong(3, userId);
            themeVoteStmt.executeUpdate();
        }
        voteCounts.put(theme, entry.getValue().size());
    }
    return voteCounts;
}
Also used : HashMap(java.util.HashMap) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) PreparedStatement(java.sql.PreparedStatement)

Example 4 with JamTheme

use of net.javadiscord.javabot.systems.jam.model.JamTheme in project JavaBot by Java-Discord.

the class ToThemeVotingTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    List<JamTheme> themes = new JamThemeRepository(con).getThemes(jam);
    if (themes.isEmpty()) {
        throw new IllegalStateException("Cannot start theme voting until at least one theme is available.");
    }
    long votingMessageId = channelManager.sendThemeVotingMessages(jam, themes);
    new JamMessageRepository(con).saveMessageId(jam, votingMessageId, "theme_voting");
    new JamRepository(con).updateJamPhase(jam, JamPhase.THEME_VOTING);
}
Also used : JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository) JamThemeRepository(net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)

Example 5 with JamTheme

use of net.javadiscord.javabot.systems.jam.model.JamTheme in project JavaBot by Java-Discord.

the class ListThemesSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    List<JamTheme> themes = new JamThemeRepository(con).getThemes(activeJam);
    EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Themes for Jam " + activeJam.getId()).setColor(config.getJamEmbedColor());
    for (JamTheme theme : themes) {
        embedBuilder.addField(theme.getName(), theme.getDescription(), false);
    }
    return event.replyEmbeds(embedBuilder.build()).setEphemeral(true);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) JamTheme(net.javadiscord.javabot.systems.jam.model.JamTheme) JamThemeRepository(net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)

Aggregations

JamTheme (net.javadiscord.javabot.systems.jam.model.JamTheme)12 JamThemeRepository (net.javadiscord.javabot.systems.jam.dao.JamThemeRepository)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)2 JamMessageRepository (net.javadiscord.javabot.systems.jam.dao.JamMessageRepository)2 JamRepository (net.javadiscord.javabot.systems.jam.dao.JamRepository)2 Map (java.util.Map)1