Search in sources :

Example 11 with RaidGroup

use of pokeraidbot.infrastructure.jpa.raid.RaidGroup in project pokeraidbot by magnusmickelsson.

the class NewRaidGroupCommand method createRaidGroup.

public static void createRaidGroup(MessageChannel channel, Guild guild, Config config, User user, Locale locale, LocalTime startAtTime, String raidId, LocaleService localeService, RaidRepository raidRepository, BotService botService, ServerConfigRepository serverConfigRepository, PokemonRepository pokemonRepository, GymRepository gymRepository, ClockService clockService, ExecutorService executorService, PokemonRaidStrategyService pokemonRaidStrategyService) {
    assertAllParametersNotNullOrEmpty(channel, config, user, locale, startAtTime, raidId, localeService, raidRepository, botService, serverConfigRepository, pokemonRepository, gymRepository, clockService, executorService);
    Raid raid = raidRepository.getById(raidId);
    final LocalDate raidDate = raid.getEndOfRaid().toLocalDate();
    final LocalDateTime raidStart = Utils.getStartOfRaid(raid.getEndOfRaid(), raid.isExRaid());
    LocalDateTime startAt = LocalDateTime.of(raidDate, startAtTime);
    checkValidInput(user, locale, startAtTime, localeService, raidRepository, raid, raidStart, startAt);
    final EmoticonSignUpMessageListener emoticonSignUpMessageListener = new EmoticonSignUpMessageListener(botService, localeService, serverConfigRepository, raidRepository, pokemonRepository, gymRepository, raid.getId(), startAt, user);
    TimeUnit delayTimeUnit = raid.isExRaid() ? TimeUnit.MINUTES : TimeUnit.SECONDS;
    int delay = raid.isExRaid() ? 1 : 15;
    final MessageEmbed messageEmbed = getRaidGroupMessageEmbed(startAt, raid.getId(), localeService, clockService, locale, delayTimeUnit, delay, raidRepository);
    channel.sendMessage(messageEmbed).queue(embed -> {
        final String messageId = embed.getId();
        emoticonSignUpMessageListener.setInfoMessageId(messageId);
        emoticonSignUpMessageListener.setEmoteMessageId(messageId);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Thread: " + Thread.currentThread().getId() + " - Adding event listener and emotes for emote message with ID: " + messageId);
        }
        final MessageChannel embedChannel = embed.getChannel();
        RaidGroup group = new RaidGroup(config.getServer(), embedChannel.getName(), messageId, messageId, user.getId(), startAt);
        group = raidRepository.newGroupForRaid(user, group, raid, guild, config);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Created group in channel " + channel.getName() + " for emote message with ID: " + messageId + " - " + group);
        }
        // Add number icons for signups
        embedChannel.addReactionById(messageId, Emotes.ONE).queue();
        embedChannel.addReactionById(messageId, Emotes.TWO).queue();
        embedChannel.addReactionById(messageId, Emotes.THREE).queue();
        embedChannel.addReactionById(messageId, Emotes.FOUR).queue();
        embedChannel.addReactionById(messageId, Emotes.FIVE).queue();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Eventlistener and emotes added for emote message with ID: " + messageId);
        }
        if (config.isPinGroups()) {
            embedChannel.pinMessageById(embed.getId()).queueAfter(50, TimeUnit.MILLISECONDS);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Pinning info message for raid group. ID is: " + embed.getId());
            }
        }
        final Callable<Boolean> refreshEditThreadTask = getMessageRefreshingTaskToSchedule(channel, raid, emoticonSignUpMessageListener, messageId, locale, raidRepository, localeService, clockService, executorService, botService, delayTimeUnit, delay, group.getId());
        executorService.submit(refreshEditThreadTask);
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) TimeUnit(java.util.concurrent.TimeUnit) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Raid(pokeraidbot.domain.raid.Raid) LocalDate(java.time.LocalDate)

Example 12 with RaidGroup

use of pokeraidbot.infrastructure.jpa.raid.RaidGroup in project pokeraidbot by magnusmickelsson.

the class RaidListCommand method executeWithConfig.

@Override
protected void executeWithConfig(CommandEvent commandEvent, Config config) {
    final User user = commandEvent.getAuthor();
    final String args = commandEvent.getArgs();
    final Locale locale = localeService.getLocaleForUser(user);
    Set<Raid> raids;
    if (args != null && args.length() > 0) {
        raids = raidRepository.getRaidsInRegionForPokemon(config.getRegion(), pokemonRepository.search(args, user));
    } else {
        raids = raidRepository.getAllRaidsForRegion(config.getRegion());
    }
    if (raids.size() == 0) {
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle(null);
        embedBuilder.setAuthor(null, null, null);
        embedBuilder.setDescription(localeService.getMessageFor(LocaleService.LIST_NO_RAIDS, locale));
        commandEvent.reply(embedBuilder.build());
    } else {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder exRaids = new StringBuilder();
        stringBuilder.append("**").append(localeService.getMessageFor(LocaleService.CURRENT_RAIDS, locale));
        if (args != null && args.length() > 0) {
            stringBuilder.append(" (").append(args).append(")");
        }
        stringBuilder.append(":**");
        stringBuilder.append("\n").append(localeService.getMessageFor(LocaleService.RAID_DETAILS, localeService.getLocaleForUser(user))).append("\n");
        Pokemon currentPokemon = null;
        for (Raid raid : raids) {
            final Pokemon raidBoss = raid.getPokemon();
            if (!raid.isExRaid() && (currentPokemon == null || (!currentPokemon.equals(raidBoss)))) {
                currentPokemon = raid.getPokemon();
                stringBuilder.append("\n**").append(currentPokemon.getName()).append("**\n");
            }
            final int numberOfPeople = raid.getNumberOfPeopleSignedUp();
            final Gym raidGym = raid.getGym();
            final Set<RaidGroup> groups = raidRepository.getGroups(raid);
            if (!raid.isExRaid()) {
                if (raidGym.isExGym()) {
                    stringBuilder.append("**").append(raidGym.getName()).append(Emotes.STAR + "**");
                } else {
                    stringBuilder.append("*").append(raidGym.getName()).append("*");
                }
                stringBuilder.append(" ").append(printTimeIfSameDay(getStartOfRaid(raid.getEndOfRaid(), false))).append("-").append(printTime(raid.getEndOfRaid().toLocalTime()));
                if (groups.size() < 1) {
                    stringBuilder.append(" (**").append(numberOfPeople).append("**)");
                } else {
                    stringBuilder.append(raidRepository.listGroupsForRaid(raid, groups));
                }
                stringBuilder.append("\n");
            } else {
                exRaids.append("\n*").append(raidGym.getName());
                exRaids.append("* ").append(localeService.getMessageFor(LocaleService.RAID_BETWEEN, locale, printTimeIfSameDay(getStartOfRaid(raid.getEndOfRaid(), true)), printTime(raid.getEndOfRaid().toLocalTime())));
                if (groups.size() < 1) {
                    exRaids.append(" (**").append(numberOfPeople).append("**)");
                } else {
                    exRaids.append(raidRepository.listGroupsForRaid(raid, groups));
                }
            }
        }
        final String exRaidList = exRaids.toString();
        if (exRaidList.length() > 1) {
            stringBuilder.append("\n**Raid-EX:**").append(exRaidList);
        }
        replyBasedOnConfig(config, commandEvent, stringBuilder.toString());
    }
}
Also used : Locale(java.util.Locale) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) User(net.dv8tion.jda.core.entities.User) Gym(pokeraidbot.domain.gym.Gym) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Raid(pokeraidbot.domain.raid.Raid) Pokemon(pokeraidbot.domain.pokemon.Pokemon)

Example 13 with RaidGroup

use of pokeraidbot.infrastructure.jpa.raid.RaidGroup in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method deleteRaidAndGroups.

private void deleteRaidAndGroups(CommandEvent commandEvent, Config config, User user, String userName, String[] args) {
    StringBuilder gymNameBuilder;
    String gymName;
    Gym gym;
    gymNameBuilder = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
        gymNameBuilder.append(args[i]).append(" ");
    }
    gymName = gymNameBuilder.toString().trim();
    gym = gymRepository.search(user, gymName, config.getRegion());
    Raid deleteRaid = raidRepository.getActiveRaidOrFallbackToExRaid(gym, config.getRegion(), user);
    verifyPermission(localeService, commandEvent, user, deleteRaid, config);
    final boolean userIsNotAdministrator = !isUserAdministrator(commandEvent);
    final boolean userIsNotMod = !isUserServerMod(commandEvent, config);
    if ((userIsNotAdministrator && userIsNotMod) && deleteRaid.getSignUps().size() > 0) {
        throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.ONLY_ADMINS_REMOVE_RAID, localeService.getLocaleForUser(user)));
    }
    final Set<RaidGroup> groups = raidRepository.getGroups(deleteRaid);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Deleting " + groups.size() + " groups associated with raid " + deleteRaid + "... ");
    }
    final int numberOfGroups = groups.size();
    for (RaidGroup group : groups) {
        final EmoticonSignUpMessageListener listener = getListenerForGroup(deleteRaid, group);
        if (listener != null) {
            final MessageChannel channel = getChannel(commandEvent.getGuild(), group.getChannel());
            if (channel != null) {
                NewRaidGroupCommand.cleanUpRaidGroupAndDeleteSignUpsIfPossible(channel, group.getStartsAt(), deleteRaid.getId(), listener, raidRepository, botService, group.getId());
            } else {
                LOGGER.debug("Could not find channel " + group.getChannel() + " in guild " + commandEvent.getGuild().getName());
            }
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Groups deleted for raid " + deleteRaid);
    }
    if (raidRepository.delete(deleteRaid)) {
        commandEvent.reactSuccess();
        removeOriginMessageIfConfigSaysSo(config, commandEvent);
        LOGGER.info("Deleted raid (and " + numberOfGroups + " related groups): " + deleteRaid);
    } else {
        throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.RAID_NOT_EXISTS, localeService.getLocaleForUser(user)));
    }
}
Also used : MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) Gym(pokeraidbot.domain.gym.Gym) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Raid(pokeraidbot.domain.raid.Raid)

Example 14 with RaidGroup

use of pokeraidbot.infrastructure.jpa.raid.RaidGroup in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method getGroupToDelete.

private RaidGroup getGroupToDelete(User user, LocalTime existingGroupTimeIfAvailable, Set<RaidGroup> groups, Raid raid) {
    RaidGroup theGroupToDelete = null;
    final Locale localeForUser = localeService.getLocaleForUser(user);
    if (groups.size() > 1 && (existingGroupTimeIfAvailable == null)) {
        throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.MANY_GROUPS_FOR_RAID, localeForUser, raid.toString(localeForUser)));
    } else if (groups.size() == 1) {
        theGroupToDelete = groups.iterator().next();
    } else {
        for (RaidGroup g : groups) {
            if (g.getStartsAt().toLocalTime().equals(existingGroupTimeIfAvailable)) {
                theGroupToDelete = g;
                break;
            }
        }
    }
    if (theGroupToDelete == null) {
        throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.NO_SUCH_GROUP, localeForUser));
    }
    return theGroupToDelete;
}
Also used : Locale(java.util.Locale) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup)

Example 15 with RaidGroup

use of pokeraidbot.infrastructure.jpa.raid.RaidGroup in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method changeOrDeleteGroup.

private void changeOrDeleteGroup(CommandEvent commandEvent, Config config, User user, String userName, String[] args) {
    String whatToChangeTo;
    String gymName;
    Gym gym;
    Raid raid;
    whatToChangeTo = args[1].trim().toLowerCase();
    String originalTime = preProcessTimeString(args[2].trim());
    LocalTime existingGroupTimeIfAvailable = null;
    try {
        existingGroupTimeIfAvailable = Utils.parseTime(user, originalTime, localeService);
    } catch (UserMessedUpException e) {
    // Input was not a time
    }
    gymName = getGymName(args, existingGroupTimeIfAvailable);
    gym = gymRepository.search(user, gymName, config.getRegion());
    raid = raidRepository.getActiveRaidOrFallbackToExRaid(gym, config.getRegion(), user);
    if (whatToChangeTo.equals("remove")) {
        final Set<RaidGroup> groups = raidRepository.getGroups(raid);
        final Set<EmoticonSignUpMessageListener> listenersToCheck = getListenersToCheck(commandEvent, config, user, raid, groups);
        EmoticonSignUpMessageListener listener = listenersToCheck.iterator().next();
        verifyIsModOrHasGroupForRaid(commandEvent, user, raid, config);
        RaidGroup theGroupToDelete = getGroupToDelete(user, existingGroupTimeIfAvailable, groups, raid);
        assertPermissionToManageThisGroup(user, theGroupToDelete, commandEvent, config);
        NewRaidGroupCommand.cleanUpGroupMessageAndEntity(commandEvent.getChannel(), raid.getId(), listener, raidRepository, botService, theGroupToDelete.getId(), raid.toString());
        final String message = localeService.getMessageFor(LocaleService.GROUP_DELETED, localeService.getLocaleForUser(user));
        replyBasedOnConfig(config, commandEvent, message);
    } else {
        LocalTime newTime = parseTime(user, whatToChangeTo, localeService);
        LocalDateTime newDateTime = LocalDateTime.of(raid.getEndOfRaid().toLocalDate(), newTime);
        checkIfInputIsValidAndUserHasRights(commandEvent, config, user, raid, newDateTime);
        if (changeGroupTime(commandEvent, config, user, userName, raid, newDateTime))
            return;
    }
    removeOriginMessageIfConfigSaysSo(config, commandEvent);
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) Gym(pokeraidbot.domain.gym.Gym) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Raid(pokeraidbot.domain.raid.Raid)

Aggregations

RaidGroup (pokeraidbot.infrastructure.jpa.raid.RaidGroup)15 Gym (pokeraidbot.domain.gym.Gym)6 Raid (pokeraidbot.domain.raid.Raid)6 LocalDateTime (java.time.LocalDateTime)5 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)5 RaidEntity (pokeraidbot.infrastructure.jpa.raid.RaidEntity)5 EmoticonSignUpMessageListener (pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener)4 LocalTime (java.time.LocalTime)3 MessageChannel (net.dv8tion.jda.core.entities.MessageChannel)3 Pokemon (pokeraidbot.domain.pokemon.Pokemon)3 Locale (java.util.Locale)2 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 Guild (net.dv8tion.jda.core.entities.Guild)2 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)2 User (net.dv8tion.jda.core.entities.User)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Config (pokeraidbot.infrastructure.jpa.config.Config)2 LocalDate (java.time.LocalDate)1 TimeUnit (java.util.concurrent.TimeUnit)1