Search in sources :

Example 16 with Config

use of pokeraidbot.infrastructure.jpa.config.Config in project pokeraidbot by magnusmickelsson.

the class RaidRepositoryTest method moveTimeForGroupWorks.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void moveTimeForGroupWorks() throws Exception {
    // We're not allowed to create signups at night, so mocking time
    clockService.setMockTime(LocalTime.of(10, 0));
    final LocalDateTime now = clockService.getCurrentDateTime();
    final LocalTime nowTime = now.toLocalTime();
    LocalDateTime endOfRaid = now.plusMinutes(45);
    final Gym gym = gymRepository.findByName("Blenda", uppsalaRegion);
    Raid enteiRaid = new Raid(pokemonRepository.search("Entei", null), endOfRaid, gym, localeService, uppsalaRegion);
    String raidCreatorName = "testUser1";
    User user = mock(User.class);
    when(user.getName()).thenReturn(raidCreatorName);
    Guild guild = mock(Guild.class);
    Config config = mock(Config.class);
    try {
        repo.newRaid(user, enteiRaid, guild, config, "test");
    } catch (RuntimeException e) {
        System.err.println(e.getMessage());
        fail("Could not save raid: " + e.getMessage());
    }
    List<User> raiders = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        final User raider = mock(User.class);
        when(raider.getName()).thenReturn("User" + i);
        raiders.add(raider);
    }
    Raid raid = repo.getActiveRaidOrFallbackToExRaid(gym, uppsalaRegion, user);
    final LocalDateTime raidGroupTime = endOfRaid.minusMinutes(10);
    for (User raider : raiders) {
        repo.addSignUp(raider, raid, new SignUp(raider.getName(), 2, raidGroupTime.toLocalTime()));
    }
    final LocalDateTime newRaidGroupTime = raidGroupTime.plusMinutes(2);
    RaidEntity entity = raidEntityRepository.findOne(raid.getId());
    repo.moveAllSignUpsForTimeToNewTime(raid.getId(), raidGroupTime, newRaidGroupTime, user);
    entity = raidEntityRepository.findOne(raid.getId());
    raid = repo.getActiveRaidOrFallbackToExRaid(gym, uppsalaRegion, user);
    entity = raidEntityRepository.findOne(raid.getId());
    assertThat(raid.getSignUps().size(), is(5));
    assertThat(raid.getNumberOfPeopleSignedUp(), is(10));
    assertThat(raid.getSignUpsAt(newRaidGroupTime.toLocalTime()).size(), is(5));
    assertThat(raid.getSignUpsAt(raidGroupTime.toLocalTime()).size(), is(0));
}
Also used : LocalDateTime(java.time.LocalDateTime) SignUp(pokeraidbot.domain.raid.signup.SignUp) User(net.dv8tion.jda.core.entities.User) LocalTime(java.time.LocalTime) Config(pokeraidbot.infrastructure.jpa.config.Config) ArrayList(java.util.ArrayList) Guild(net.dv8tion.jda.core.entities.Guild) RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Gym(pokeraidbot.domain.gym.Gym) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with Config

use of pokeraidbot.infrastructure.jpa.config.Config in project pokeraidbot by magnusmickelsson.

the class RaidRepositoryTest method changeEndOfRaidWorks.

@Test
public void changeEndOfRaidWorks() throws Exception {
    // We're not allowed to create signups at night, so mocking time
    clockService.setMockTime(LocalTime.of(10, 0));
    final LocalDateTime now = clockService.getCurrentDateTime();
    final LocalTime nowTime = now.toLocalTime();
    LocalDateTime endOfRaid = now.plusMinutes(45);
    final Gym gym = gymRepository.findByName("Blenda", uppsalaRegion);
    Raid enteiRaid = new Raid(pokemonRepository.search("Entei", null), endOfRaid, gym, localeService, uppsalaRegion);
    String raidCreatorName = "testUser1";
    User user = mock(User.class);
    when(user.getName()).thenReturn(raidCreatorName);
    Guild guild = mock(Guild.class);
    Config config = mock(Config.class);
    try {
        repo.newRaid(user, enteiRaid, guild, config, "test");
    } catch (RuntimeException e) {
        System.err.println(e.getMessage());
        fail("Could not save raid: " + e.getMessage());
    }
    Raid raid = repo.getActiveRaidOrFallbackToExRaid(gym, uppsalaRegion, user);
    Raid changedRaid = repo.changeEndOfRaid(raid.getId(), endOfRaid.plusMinutes(5), guild, config, user, "test");
    assertThat(raid.getEndOfRaid(), not(changedRaid.getEndOfRaid()));
    assertThat(changedRaid.getEndOfRaid(), is(raid.getEndOfRaid().plusMinutes(5)));
    assertThat(raid.getGym(), is(changedRaid.getGym()));
    assertThat(raid.getSignUps(), is(changedRaid.getSignUps()));
    assertThat(raid.getRegion(), is(changedRaid.getRegion()));
    assertThat(raid.getPokemon().getName(), is(changedRaid.getPokemon().getName()));
}
Also used : LocalDateTime(java.time.LocalDateTime) User(net.dv8tion.jda.core.entities.User) LocalTime(java.time.LocalTime) Config(pokeraidbot.infrastructure.jpa.config.Config) Gym(pokeraidbot.domain.gym.Gym) Guild(net.dv8tion.jda.core.entities.Guild) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with Config

use of pokeraidbot.infrastructure.jpa.config.Config in project pokeraidbot by magnusmickelsson.

the class GymHuntrRaidEventListener method onEvent.

@Override
public void onEvent(Event event) {
    if (event instanceof GuildMessageReceivedEvent) {
        GuildMessageReceivedEvent guildEvent = (GuildMessageReceivedEvent) event;
        final User messageAuthor = guildEvent.getAuthor();
        try {
            if (isUserGymhuntrBot(messageAuthor) || isUserPokeAlarmBot(messageAuthor)) {
                final String serverName = guildEvent.getGuild().getName().toLowerCase();
                final Config config = serverConfigRepository.getConfigForServer(serverName);
                if (config == null) {
                    LOGGER.warn("Server configuration is null for this guild: " + guildEvent.getGuild().getName());
                    return;
                }
                if (!config.useBotIntegration()) {
                    if (LOGGER.isTraceEnabled()) {
                        LOGGER.trace("Skipping trigger, since bot integration setting is false for server " + guildEvent.getGuild().getName());
                    }
                    return;
                }
                final List<MessageEmbed> embeds = guildEvent.getMessage().getEmbeds();
                if (embeds != null && embeds.size() > 0) {
                    for (MessageEmbed embed : embeds) {
                        final LocalDateTime currentDateTime = clockService.getCurrentDateTime();
                        final String description = embed.getDescription();
                        final String title = embed.getTitle();
                        List<String> newRaidArguments;
                        if (isUserGymhuntrBot(messageAuthor)) {
                            newRaidArguments = gymhuntrArgumentsToCreateRaid(title, description, clockService);
                        } else if (isUserPokeAlarmBot(messageAuthor)) {
                            newRaidArguments = pokeAlarmArgumentsToCreateRaid(title, description, clockService);
                        } else {
                            newRaidArguments = new ArrayList<>();
                        }
                        try {
                            if (newRaidArguments != null && newRaidArguments.size() > 0) {
                                final Iterator<String> iterator = newRaidArguments.iterator();
                                final String gym = iterator.next();
                                final String pokemon = iterator.next();
                                final String time = iterator.next();
                                final Pokemon raidBoss = pokemonRepository.getByName(pokemon);
                                final String region = config.getRegion();
                                final Gym raidGym = gymRepository.findByName(gym, region);
                                final LocalDate currentDate = currentDateTime.toLocalDate();
                                final LocalDateTime endOfRaid = LocalDateTime.of(currentDate, LocalTime.parse(time, Utils.timeParseFormatter));
                                final SelfUser botUser = botService.getBot().getSelfUser();
                                final PokemonRaidInfo raidInfo;
                                raidInfo = strategyService.getRaidInfo(raidBoss);
                                handleRaidFromIntegration(botUser, guildEvent, raidBoss, raidGym, endOfRaid, config, clockService, raidInfo, strategyService);
                            } else {
                                if (LOGGER.isDebugEnabled()) {
                                    LOGGER.debug("No arguments to create raid with for server " + config + ", skipping. Raw command: " + guildEvent.getMessage().getRawContent());
                                }
                            }
                        } catch (Throwable t) {
                            LOGGER.warn("Exception when trying to get arguments for raid creation: " + t.getMessage());
                        }
                    }
                }
            }
        } catch (Throwable t) {
            LOGGER.warn("Exception thrown for event listener: " + t.getMessage());
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Stacktrace: ", t);
            }
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) SelfUser(net.dv8tion.jda.core.entities.SelfUser) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Config(pokeraidbot.infrastructure.jpa.config.Config) PokemonRaidInfo(pokeraidbot.domain.pokemon.PokemonRaidInfo) LocalDate(java.time.LocalDate) Gym(pokeraidbot.domain.gym.Gym) Pokemon(pokeraidbot.domain.pokemon.Pokemon) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SelfUser(net.dv8tion.jda.core.entities.SelfUser)

Aggregations

Config (pokeraidbot.infrastructure.jpa.config.Config)18 User (net.dv8tion.jda.core.entities.User)9 Guild (net.dv8tion.jda.core.entities.Guild)8 LocalDateTime (java.time.LocalDateTime)7 Gym (pokeraidbot.domain.gym.Gym)7 LocalTime (java.time.LocalTime)5 Test (org.junit.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 Transactional (org.springframework.transaction.annotation.Transactional)3 HashMap (java.util.HashMap)2 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 CSVGymDataReader (pokeraidbot.infrastructure.CSVGymDataReader)2 RaidGroup (pokeraidbot.infrastructure.jpa.raid.RaidGroup)2 SocketTimeoutException (java.net.SocketTimeoutException)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1