Search in sources :

Example 1 with JamSubmission

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

the class JamSubmitSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    if (!activeJam.submissionsAllowed()) {
        return Responses.warning(event, "Submissions Not Permitted", "The Jam is not currently accepting submissions.");
    }
    OptionMapping sourceLinkOption = event.getOption("link");
    OptionMapping descriptionOption = event.getOption("description");
    if (sourceLinkOption == null || descriptionOption == null) {
        return Responses.warning(event, "Missing required arguments.");
    }
    String link = sourceLinkOption.getAsString();
    if (!this.validateLink(link)) {
        return Responses.warning(event, "Invalid Source", "The source link you provide must lead to a valid web page.");
    }
    JamSubmission submission = new JamSubmission();
    submission.setUserId(event.getUser().getIdLong());
    submission.setJam(activeJam);
    submission.setThemeName(this.getThemeName(con, activeJam, event));
    submission.setSourceLink(link);
    submission.setDescription(descriptionOption.getAsString());
    new JamSubmissionRepository(con).saveSubmission(submission);
    return Responses.success(event, "Submission Received", "Thank you for your submission to the Jam.");
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission)

Example 2 with JamSubmission

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

the class ListSubmissionsSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    OptionMapping pageOption = event.getOption("page");
    OptionMapping userOption = event.getOption("user");
    int page = 1;
    if (pageOption != null) {
        page = (int) pageOption.getAsLong();
    }
    Long userId = null;
    if (userOption != null) {
        userId = userOption.getAsUser().getIdLong();
    }
    List<JamSubmission> submissions = new JamSubmissionRepository(con).getSubmissions(activeJam, page, userId);
    EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Submissions").setColor(config.getJamEmbedColor());
    for (JamSubmission sub : submissions) {
        User user = event.getJDA().getUserById(sub.getUserId());
        String userName = user == null ? "Unknown user" : user.getAsTag();
        String timestamp = sub.getCreatedAt().format(DateTimeFormatter.ofPattern("dd MMMM yyyy, HH:mm:ss 'UTC'"));
        embedBuilder.addField(String.format("`%d` %s at %s", sub.getId(), userName, timestamp), "Link: *" + sub.getSourceLink() + "*\n> " + sub.getDescription(), false);
    }
    embedBuilder.setFooter("Page " + page + ", up to 10 items per page");
    return event.replyEmbeds(embedBuilder.build()).setEphemeral(true);
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission)

Example 3 with JamSubmission

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

the class ToCompletionTransition method getSubmissionMessageIds.

/**
 * Retrieves the submission's message ids.
 *
 * @param submissions       A {@link List} with all Submissions.
 * @param messageRepository The {@link JamMessageRepository}.
 * @return A Map contains the {@link JamSubmission} and its message id.
 * @throws SQLException If an error occurs.
 */
public Map<JamSubmission, Long> getSubmissionMessageIds(List<JamSubmission> submissions, JamMessageRepository messageRepository) throws SQLException {
    Map<JamSubmission, Long> submissionMessages = new HashMap<>();
    for (JamSubmission submission : submissions) {
        Long id = messageRepository.getMessageId(submission.getJam(), "submission-" + submission.getId());
        if (id == null) {
            throw new IllegalStateException("Could not find message id for submission " + submission.getId());
        }
        submissionMessages.put(submission, id);
    }
    return submissionMessages;
}
Also used : JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) HashMap(java.util.HashMap)

Example 4 with JamSubmission

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

the class ToCompletionTransition method transition.

@Override
public void transition(Jam jam, SlashCommandInteractionEvent event, JamChannelManager channelManager, Connection con) throws SQLException {
    JamMessageRepository messageRepository = new JamMessageRepository(con);
    List<JamSubmission> submissions = new JamSubmissionRepository(con).getSubmissions(jam);
    var votes = channelManager.getSubmissionVotes(this.getSubmissionMessageIds(submissions, messageRepository));
    var voteCounts = this.recordAndCountVotes(votes, con, messageRepository);
    var winningSubmissions = this.determineWinners(voteCounts);
    if (winningSubmissions.isEmpty()) {
        channelManager.sendNoWinnersMessage();
    } else if (winningSubmissions.size() == 1) {
        channelManager.sendSingleWinnerMessage(winningSubmissions.get(0), voteCounts, event);
    } else {
        channelManager.sendMultipleWinnersMessage(winningSubmissions, voteCounts, event);
    }
    new JamRepository(con).completeJam(jam);
}
Also used : JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) JamMessageRepository(net.javadiscord.javabot.systems.jam.dao.JamMessageRepository) JamRepository(net.javadiscord.javabot.systems.jam.dao.JamRepository)

Example 5 with JamSubmission

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

the class ToCompletionTransition method recordAndCountVotes.

/**
 * Counts JavaJam submission votes.
 *
 * @param submissionVotes   A Map containing the {@link JamSubmission} and a {@link List} that contains all message id's.
 * @param con               The datasource's connection.
 * @param messageRepository The {@link JamMessageRepository}.
 * @return A Map containing the {@link JamSubmission} and the vote count as an Integer.
 * @throws SQLException If an error occurs.
 */
public Map<JamSubmission, Integer> recordAndCountVotes(Map<JamSubmission, List<Long>> submissionVotes, Connection con, JamMessageRepository messageRepository) throws SQLException {
    try (PreparedStatement submissionVoteStmt = con.prepareStatement("INSERT INTO jam_submission_vote (submission_id, user_id) VALUES (?, ?)")) {
        Map<JamSubmission, Integer> voteCounts = new HashMap<>();
        for (var entry : submissionVotes.entrySet()) {
            submissionVoteStmt.setLong(1, entry.getKey().getId());
            for (var userId : entry.getValue()) {
                submissionVoteStmt.setLong(2, userId);
                submissionVoteStmt.executeUpdate();
            }
            voteCounts.put(entry.getKey(), entry.getValue().size());
            messageRepository.removeMessageId(entry.getKey().getJam(), "submission-" + entry.getKey().getId());
        }
        return voteCounts;
    }
}
Also used : JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement)

Aggregations

JamSubmission (net.javadiscord.javabot.systems.jam.model.JamSubmission)12 PreparedStatement (java.sql.PreparedStatement)4 HashMap (java.util.HashMap)4 JamSubmissionRepository (net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)4 ResultSet (java.sql.ResultSet)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 ArrayList (java.util.ArrayList)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 OffsetDateTime (java.time.OffsetDateTime)1 List (java.util.List)1 Map (java.util.Map)1 JDA (net.dv8tion.jda.api.JDA)1 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)1 User (net.dv8tion.jda.api.entities.User)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 JamConfig (net.javadiscord.javabot.data.config.guild.JamConfig)1 Jam (net.javadiscord.javabot.systems.jam.model.Jam)1 JamTheme (net.javadiscord.javabot.systems.jam.model.JamTheme)1