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