use of pokeraidbot.domain.errors.UserMessedUpException in project pokeraidbot by magnusmickelsson.
the class Utils method assertTimeInRaidTimespan.
public static void assertTimeInRaidTimespan(User user, LocalDateTime dateTimeToCheck, Raid raid, LocaleService localeService) {
final LocalDateTime startOfRaid = getStartOfRaid(raid.getEndOfRaid(), raid.isExRaid());
final boolean timeIsSameOrBeforeEnd = raid.getEndOfRaid().isAfter(dateTimeToCheck) || raid.getEndOfRaid().equals(dateTimeToCheck);
final boolean timeIsSameOrAfterStart = startOfRaid.isBefore(dateTimeToCheck) || startOfRaid.equals(dateTimeToCheck);
if (!(timeIsSameOrBeforeEnd && timeIsSameOrAfterStart)) {
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.TIME_NOT_IN_RAID_TIMESPAN, localeService.getLocaleForUser(user), printDateTime(dateTimeToCheck), printDateTime(startOfRaid), printTimeIfSameDay(raid.getEndOfRaid())));
}
}
use of pokeraidbot.domain.errors.UserMessedUpException in project pokeraidbot by magnusmickelsson.
the class AlterRaidCommand method changePokemon.
// todo: move to service
public static void changePokemon(GymRepository gymRepository, LocaleService localeService, PokemonRepository pokemonRepository, RaidRepository raidRepository, CommandEvent commandEvent, Config config, User user, String userName, String newPokemonName, String... gymArguments) {
String whatToChangeTo;
StringBuilder gymNameBuilder;
String gymName;
Gym gym;
Raid raid;
whatToChangeTo = newPokemonName;
gymNameBuilder = new StringBuilder();
for (String arg : gymArguments) {
gymNameBuilder.append(arg).append(" ");
}
gymName = gymNameBuilder.toString().trim();
gym = gymRepository.search(user, gymName, config.getRegion());
Raid pokemonRaid = raidRepository.getActiveRaidOrFallbackToExRaid(gym, config.getRegion(), user);
final Pokemon pokemon = pokemonRepository.search(whatToChangeTo, user);
if (pokemonRaid.isExRaid()) {
throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.EX_NO_CHANGE_POKEMON, localeService.getLocaleForUser(user)));
}
// Anybody should be able to report hatched eggs
if (!pokemonRaid.getPokemon().isEgg()) {
verifyPermission(localeService, commandEvent, user, pokemonRaid, config);
}
if (Utils.isRaidExPokemon(whatToChangeTo)) {
throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.EX_CANT_CHANGE_RAID_TYPE, localeService.getLocaleForUser(user)));
}
raid = raidRepository.changePokemon(pokemonRaid, pokemon, commandEvent.getGuild(), config, user, "!raid change pokemon " + pokemon.getName() + " " + gymName);
commandEvent.reactSuccess();
removeOriginMessageIfConfigSaysSo(config, commandEvent);
LOGGER.info("Changed pokemon for raid: " + raid);
}
use of pokeraidbot.domain.errors.UserMessedUpException in project pokeraidbot by magnusmickelsson.
the class AlterRaidCommand method changeGroupTime.
private boolean changeGroupTime(CommandEvent commandEvent, Config config, User user, String userName, Raid raid, LocalDateTime newDateTime) {
boolean groupChanged = false;
final Set<RaidGroup> groups = raidRepository.getGroups(raid);
final Set<EmoticonSignUpMessageListener> listenersToCheck = getListenersToCheck(commandEvent, config, user, raid, groups);
for (EmoticonSignUpMessageListener listener : listenersToCheck) {
final String raidId = raid.getId();
final LocalDateTime currentStartAt = listener.getStartAt();
if (currentStartAt != null && currentStartAt.equals(newDateTime)) {
LOGGER.info("Group is already at input time.");
// This group is already at the time to change to
commandEvent.reactError();
} else if (currentStartAt != null) {
LOGGER.info("Changing group time from " + currentStartAt + " to " + newDateTime);
RaidGroup raidGroup = raidRepository.changeGroup(user, raidId, listener.getUserId(), currentStartAt, newDateTime);
raidRepository.moveAllSignUpsForTimeToNewTime(raidId, currentStartAt, newDateTime, user);
listener.setStartAt(newDateTime);
groupChanged = true;
replyBasedOnConfigAndRemoveAfter(config, commandEvent, localeService.getMessageFor(LocaleService.MOVED_GROUP, localeService.getLocaleForUser(user), printTimeIfSameDay(currentStartAt), printTimeIfSameDay(newDateTime), raid.getGym().getName()), BotServerMain.timeToRemoveFeedbackInSeconds);
LOGGER.info("Group time changed. Group: " + raidGroup);
commandEvent.reactSuccess();
} else {
LOGGER.info("Group is about to get cleaned up.");
commandEvent.reactError();
// This group is about to get cleaned up since its start time is null
replyBasedOnConfigAndRemoveAfter(config, commandEvent, localeService.getMessageFor(LocaleService.GROUP_CLEANING_UP, localeService.getLocaleForUser(user)), BotServerMain.timeToRemoveFeedbackInSeconds);
return true;
}
}
if (!groupChanged) {
throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.BAD_SYNTAX, localeService.getLocaleForUser(user), "!raid change group 10:00 solna platform"));
}
return false;
}
use of pokeraidbot.domain.errors.UserMessedUpException in project pokeraidbot by magnusmickelsson.
the class ConfigAwareCommand method execute.
@Override
protected void execute(CommandEvent commandEvent) {
Config configForServer = null;
try {
final Guild guild = commandEvent.getGuild();
if (guild != null) {
final String server = guild.getName().trim().toLowerCase();
configForServer = serverConfigRepository.getConfigForServer(server);
if (configForServer == null) {
final String noConfigText = localeService.getMessageFor(LocaleService.NO_CONFIG, localeService.getLocaleForUser(commandEvent.getAuthor()));
commandEvent.reply(noConfigText);
if (commandListener != null) {
commandListener.onCompletedCommand(commandEvent, this);
}
return;
}
}
executeWithConfig(commandEvent, configForServer);
if (commandListener != null) {
commandListener.onCompletedCommand(commandEvent, this);
}
} catch (Throwable t) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Exception thrown from command " + this.getClass().getSimpleName() + " with input message \"" + commandEvent.getMessage().getRawContent() + "\"" + (configForServer != null ? " for server " + configForServer.getServer() : "") + ":\n" + t.getMessage());
if (t.getMessage() == null) {
LOGGER.debug("Dumping stacktrace, since exception was null.", t);
}
}
try {
if (t instanceof IllegalArgumentException) {
getFeedbackStrategy(configForServer).replyError(configForServer, commandEvent, new UserMessedUpException(commandEvent.getAuthor().getName(), t.getMessage()), localeService);
} else {
getFeedbackStrategy(configForServer).replyError(configForServer, commandEvent, t, localeService);
}
if (commandListener != null) {
commandListener.onTerminatedCommand(commandEvent, this);
}
} catch (Throwable tt) {
LOGGER.warn("Exception when trying to give feedback about an error for server " + configForServer + ": " + tt.getMessage());
}
}
}
use of pokeraidbot.domain.errors.UserMessedUpException in project pokeraidbot by magnusmickelsson.
the class GymRepository method search.
public Gym search(User user, String query, String region) {
if (region == null || StringUtils.isEmpty(query)) {
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH, LocaleService.DEFAULT));
}
final Set<Gym> gyms = getAllGymsForRegion(region);
final Locale localeForUser = localeService.getLocaleForUser(user);
final Optional<Gym> gym = get(query, region);
if (gym.isPresent()) {
return gym.get();
} else {
// 70 seems like a reasonable cutoff here...
List<ExtractedResult> candidates = extractTop(query, gyms.stream().map(s -> s.getName()).collect(Collectors.toList()), 6, 70);
if (candidates.size() == 1) {
return findByName(candidates.iterator().next().getString(), region);
} else if (candidates.size() < 1) {
throw new GymNotFoundException(query, localeService, LocaleService.SWEDISH, region);
} else {
List<Gym> matchingPartial = getMatchingPartial(query, region, candidates);
if (matchingPartial.size() == 1) {
return matchingPartial.get(0);
}
if (candidates.size() <= 5) {
String possibleMatches = candidates.stream().map(s -> findByName(s.getString(), region).getName()).collect(Collectors.joining(", "));
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_OPTIONS, localeForUser, possibleMatches));
} else {
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_MANY_RESULTS, localeForUser));
}
}
}
}
Aggregations