Search in sources :

Example 6 with Config

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

the class InstallCommand method execute.

@Override
protected void execute(CommandEvent event) {
    String args = event.getArgs();
    if (StringUtils.isEmpty(args)) {
        // Answer with how-to
        event.replyInDM("Re-run the command !raid install, but with the following syntax:");
        event.replyInDM("!raid install server=[server name];region=[region dataset reference];" + "replyInDm=[true or false];locale=[2 char language code];mods=[group for mods (optional)]" + ";feedback=[feedback strategy (optional)];groupCreation=[group creation strategy (optional)];" + "groupChannel=[if using named channel strategy, channel name (optional)];botIntegration=[true or false " + "(optional)];pinGroups=[true or false (optional)]");
        event.replyInDM("Example: !raid install server=My test server;region=stockholm;" + "replyInDm=false;locale=sv;mods=mods;feedback=REMOVE_ALL_EXCEPT_MAP;groupCreation=NAMED_CHANNEL;" + "groupChannel=raidgroups;botIntegration=true;pinGroups=true");
        event.reactSuccess();
        return;
    } else {
        Map<String, String> settingsToSet = new HashMap<>();
        final String[] arguments = args.split(";");
        if (arguments.length < 4 || arguments.length > 10) {
            event.replyInDM("Wrong syntax of install command. Do this again, and this time " + "follow instructions, please: !raid install");
            event.reactError();
            return;
        }
        for (String argument : arguments) {
            final String[] keyValue = argument.split("=");
            if (keyValue.length != 2 || StringUtils.isEmpty(keyValue[0]) || StringUtils.isEmpty(keyValue[1])) {
                event.replyInDM("Wrong syntax of install command. Do this again, and this " + "time follow instructions, please: !raid install");
                event.reactError();
                return;
            }
            settingsToSet.put(keyValue[0].trim().toLowerCase(), keyValue[1].trim().toLowerCase());
        }
        final String server = settingsToSet.get("server");
        try {
            Config config = serverConfigRepository.getConfigForServer(server);
            final Locale locale = new Locale(settingsToSet.get("locale"));
            final String region = settingsToSet.get("region");
            final String modGroup = settingsToSet.get("mods");
            final String replyindmValue = settingsToSet.get("replyindm");
            final Boolean replyInDmWhenPossible = replyindmValue == null ? false : Boolean.valueOf(replyindmValue);
            final String feedbackStrategyValue = settingsToSet.get("feedback");
            final String groupCreationStrategyValue = settingsToSet.get("groupcreation");
            final String groupChannel = settingsToSet.get("groupchannel");
            final String botIntegrationValue = settingsToSet.get("botintegration");
            final Boolean botIntegration = botIntegrationValue == null ? false : Boolean.valueOf(botIntegrationValue);
            final String pinGroupsValue = settingsToSet.get("pinGroups");
            final Boolean pinGroups = pinGroupsValue == null ? false : Boolean.valueOf(pinGroupsValue);
            if (config == null) {
                config = new Config(region, replyInDmWhenPossible, locale, server);
            } else {
                config.setLocale(locale);
                config.setRegion(region);
                config.setReplyInDmWhenPossible(replyInDmWhenPossible);
            }
            config.setModPermissionGroup(modGroup);
            Config.RaidGroupCreationStrategy groupCreationStrategy = getGroupCreationStrategyIfPossible(event, groupCreationStrategyValue);
            if (groupCreationStrategy != null) {
                config.setGroupCreationStrategy(groupCreationStrategy);
            }
            Config.FeedbackStrategy feedbackStrategy = getFeedbackStrategyIfPossible(event, feedbackStrategyValue);
            if (feedbackStrategy != null) {
                config.setFeedbackStrategy(feedbackStrategy);
            }
            config.setGroupCreationChannel(groupChannel);
            config.setUseBotIntegration(botIntegration);
            config.setPinGroups(pinGroups);
            event.replyInDM("Configuration complete. Saved configuration: " + serverConfigRepository.save(config));
            event.replyInDM("Now, run \"!raid install-emotes\" in your server's text chat to install the custom " + "emotes the bot needs.");
            event.reactSuccess();
            gymRepository.reloadGymData();
        } catch (Throwable t) {
            event.replyInDM("There was an error: " + t.getMessage());
            event.replyInDM("Make sure you have followed the instructions correctly. " + "If you have, contact magnus.mickelsson@gmail.com for support " + "and ensure you include the error message you got above ...");
            event.reactError();
        }
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) Config(pokeraidbot.infrastructure.jpa.config.Config)

Example 7 with Config

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

the class StartUpEventListener method onEvent.

@Override
public void onEvent(Event event) {
    if (event instanceof ReadyEvent) {
        final List<Guild> guilds = event.getJDA().getGuilds();
        for (Guild guild : guilds) {
            Config config = serverConfigRepository.getConfigForServer(guild.getName().trim().toLowerCase());
            if (config != null) {
                final String messageId = config.getOverviewMessageId();
                if (!StringUtils.isEmpty(messageId)) {
                    for (MessageChannel channel : guild.getTextChannels()) {
                        if (attachToOverviewMessageIfExists(guild, config, messageId, channel, pokemonRaidStrategyService)) {
                            break;
                        } else {
                            if (LOGGER.isDebugEnabled()) {
                                LOGGER.debug("Didn't find overview in channel " + channel.getName());
                            }
                        }
                    }
                }
                final List<RaidGroup> groupsForServer = raidRepository.getGroupsForServer(config.getServer());
                for (RaidGroup group : groupsForServer) {
                    attachToGroupMessage(guild, config, group);
                }
            }
        }
    }
}
Also used : ReadyEvent(net.dv8tion.jda.core.events.ReadyEvent) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Config(pokeraidbot.infrastructure.jpa.config.Config) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Guild(net.dv8tion.jda.core.entities.Guild)

Example 8 with Config

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

the class TestServerMain method getGymRepositoryForConfig.

public static GymRepository getGymRepositoryForConfig(LocaleService localeService, ServerConfigRepository serverConfigRepository) {
    Map<String, Set<Gym>> gymsPerRegion = new HashMap<>();
    final Map<String, Config> configMap = serverConfigRepository.getAllConfig();
    System.out.println("Config has following servers: " + configMap.keySet());
    for (String server : configMap.keySet()) {
        final Config config = serverConfigRepository.getConfigForServer(server);
        final String region = config.getRegion();
        if (!gymsPerRegion.containsKey(region)) {
            final Set<Gym> gymsInRegion = new CSVGymDataReader("/gyms_" + region + ".csv").readAll();
            gymsPerRegion.put(region, gymsInRegion);
            System.out.println("Loaded " + gymsInRegion.size() + " gyms for server " + server + ".");
        }
    }
    return new GymRepository(gymsPerRegion, localeService);
}
Also used : Set(java.util.Set) GymRepository(pokeraidbot.domain.gym.GymRepository) HashMap(java.util.HashMap) Config(pokeraidbot.infrastructure.jpa.config.Config) Gym(pokeraidbot.domain.gym.Gym) CSVGymDataReader(pokeraidbot.infrastructure.CSVGymDataReader)

Example 9 with Config

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

the class GymRepositoryTest method setUp.

@Before
public void setUp() throws Exception {
    UserConfigRepository userConfigRepository = mock(UserConfigRepository.class);
    when(userConfigRepository.findOne(any(String.class))).thenReturn(null);
    localeService = new LocaleService("sv", userConfigRepository);
    final Config dalarnaConfig = new Config("dalarna", "dalarna");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("dalarna")).thenReturn(dalarnaConfig);
    final Config uppsalaConfig = new Config("uppsala", "uppsala");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("uppsala")).thenReturn(uppsalaConfig);
    final Config angelholmConfig = new Config("ängelholm", "ängelholm");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("ängelholm")).thenReturn(angelholmConfig);
    final Config luleConfig = new Config("luleå", "luleå");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("luleå")).thenReturn(luleConfig);
    final Config umeConfig = new Config("umeå", "umeå");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("umeå")).thenReturn(umeConfig);
    final Config vannasConfig = new Config("vännäs", "vännäs");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("vännäs")).thenReturn(vannasConfig);
    final Config lyckseleConfig = new Config("lycksele", "lycksele");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("lycksele")).thenReturn(lyckseleConfig);
    final Config norrkopingConfig = new Config("norrköping", "norrköping");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("norrköping")).thenReturn(norrkopingConfig);
    final Config trollhattanConfig = new Config("trollhättan", "trollhättan");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("trollhättan")).thenReturn(trollhattanConfig);
    final Config helsingborgConfig = new Config("helsingborg", "helsingborg");
    when(SERVER_CONFIG_REPOSITORY.getConfigForServer("helsingborg")).thenReturn(helsingborgConfig);
    configMap = new HashMap<>();
    configMap.put("dalarna", dalarnaConfig);
    configMap.put("uppsala", uppsalaConfig);
    configMap.put("ängelholm", angelholmConfig);
    configMap.put("luleå", luleConfig);
    configMap.put("umeå", umeConfig);
    configMap.put("vännäs", vannasConfig);
    configMap.put("norrköping", norrkopingConfig);
    configMap.put("trollhättan", trollhattanConfig);
    configMap.put("helsingborg", helsingborgConfig);
    configMap.put("lycksele", lyckseleConfig);
    when(SERVER_CONFIG_REPOSITORY.getAllConfig()).thenReturn(configMap);
    repo = TestServerMain.getGymRepositoryForConfig(localeService, SERVER_CONFIG_REPOSITORY);
}
Also used : UserConfigRepository(pokeraidbot.infrastructure.jpa.config.UserConfigRepository) Config(pokeraidbot.infrastructure.jpa.config.Config) LocaleService(pokeraidbot.domain.config.LocaleService) Before(org.junit.Before)

Example 10 with Config

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

the class RaidRepositoryTest method changePokemonWorks.

@Test
public void changePokemonWorks() 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);
    Guild guild = mock(Guild.class);
    Config config = mock(Config.class);
    when(user.getName()).thenReturn(raidCreatorName);
    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.changePokemon(raid, pokemonRepository.search("Mewtwo", user), guild, config, user, "test");
    assertThat(raid.getEndOfRaid(), is(changedRaid.getEndOfRaid()));
    assertThat(raid.getGym(), is(changedRaid.getGym()));
    assertThat(raid.getSignUps(), is(changedRaid.getSignUps()));
    assertThat(raid.getRegion(), is(changedRaid.getRegion()));
    assertThat(raid.getPokemon().getName(), is("Entei"));
    assertThat(changedRaid.getPokemon().getName(), is("Mewtwo"));
}
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)

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