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;
}
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");
}
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;
}
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);
}
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);
}
Aggregations