sxcybot

Discord bot for OSRS based channels
git clone git://git.wimdupont.com/sxcybot.git
Log | Files | Refs | README | LICENSE

commit 3c004e84b0e3530657c07a1cf6279df686da12ed
parent 90a58295b65f16ce21d6848c9736f6b6c7d7ce29
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date:   Sun,  7 Nov 2021 09:29:19 +0100

Fix hiscore stats (construction)

Diffstat:
Msrc/main/java/com/sxcy/sxcybot/SxcyBotApplication.java | 25+++++++++++++------------
Msrc/main/java/com/sxcy/sxcybot/client/HiScoreClient.java | 10+++++-----
Msrc/test/java/com/sxcy/sxcybot/client/GrandExchangeClientTest.java | 2++
Msrc/test/java/com/sxcy/sxcybot/client/HiScoreClientTest.java | 40++++++++++++++++++++++++++++++----------
4 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/src/main/java/com/sxcy/sxcybot/SxcyBotApplication.java b/src/main/java/com/sxcy/sxcybot/SxcyBotApplication.java @@ -57,12 +57,12 @@ public class SxcyBotApplication implements CommandLineRunner { @Override public void run(String[] args) throws LoginException { JDA jda = JDABuilder.createLight(token, - GatewayIntent.GUILD_MESSAGES, - GatewayIntent.DIRECT_MESSAGES, - GatewayIntent.GUILD_MESSAGE_REACTIONS, - GatewayIntent.GUILD_MEMBERS, - GatewayIntent.GUILD_BANS - ) + GatewayIntent.GUILD_MESSAGES, + GatewayIntent.DIRECT_MESSAGES, + GatewayIntent.GUILD_MESSAGE_REACTIONS, + GatewayIntent.GUILD_MEMBERS, + GatewayIntent.GUILD_BANS + ) .addEventListeners(commandListener, adminCommandListener, eventWaiterUtil, guildMemberEventListener, pollReactionListener) .setActivity(Activity.listening(Commands.COMMAND_PREFIX + Command.HELP.name().toLowerCase())) .build(); @@ -75,11 +75,12 @@ public class SxcyBotApplication implements CommandLineRunner { } } - private void processArgs(JDA jda, String... args){ - if (args.length == 0 || Arrays.stream(args).noneMatch("noNotes"::equalsIgnoreCase)) - releaseNotesUtil.showReleaseNotes(jda); - if (args.length > 0 && Arrays.stream(args).anyMatch("snap"::equalsIgnoreCase)) - pvMRoleResolver.resolvePvMRoles(); + private void processArgs(JDA jda, String... args) { + if (args.length > 0) { + if (Arrays.stream(args).anyMatch("notes"::equalsIgnoreCase)) + releaseNotesUtil.showReleaseNotes(jda); + if (Arrays.stream(args).anyMatch("snap"::equalsIgnoreCase)) + pvMRoleResolver.resolvePvMRoles(); + } } - } diff --git a/src/main/java/com/sxcy/sxcybot/client/HiScoreClient.java b/src/main/java/com/sxcy/sxcybot/client/HiScoreClient.java @@ -30,9 +30,9 @@ public class HiScoreClient { private static final String URL = "https://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=%s"; private static final int STATS_START = 0; - private static final int STATS_END = 22; + private static final int STATS_END = 23; private static final int BOSS_START = 36; - private static final int BOSS_END = 83; + private static final int BOSS_END = 82; @NonNull private final OsrsHiscoreBossService osrsHiscoreBossService; @@ -43,7 +43,7 @@ public class HiScoreClient { List<OsrsHiscoreStat> hiscoreStats = osrsHiscoreStatService.findAll(); return getHiScores(playername, channel, (osrsStat, hiScoreStatValues, i) -> OsrsStat.builder() .name(hiscoreStats.stream().filter(f -> f.getOrderValue() == i).findFirst().orElseThrow(() - -> new EntityNotFoundException("OsrsStatName for order not found!")) + -> new EntityNotFoundException(String.format("OsrsStatName for order %s not found!", i))) .getName()) .rank(NumberFormatter.format(hiScoreStatValues.get(0))) .level(hiScoreStatValues.get(1)) @@ -55,7 +55,7 @@ public class HiScoreClient { List<OsrsHiscoreBoss> hiscoreBosses = osrsHiscoreBossService.findAll(); return getHiScores(playername, channel, (osrsKc, hiScoreKcValues, i) -> OsrsBossKc.builder() .name(hiscoreBosses.stream().filter(f -> f.getOrderValue() == i).findFirst().orElseThrow(() - -> new EntityNotFoundException("OsrsBoss for order not found!")) + -> new EntityNotFoundException(String.format("OsrsBoss for order %s not found!", i))) .getName()) .rank(hiScoreKcValues.get(0)) .kc(hiScoreKcValues.get(1)) @@ -72,7 +72,7 @@ public class HiScoreClient { int i = fromIndex; List<String> hiScores = Arrays.stream(result.split("\n")).collect(Collectors.toList()); try { - for (String osrsHiScoresStats : hiScores.subList(fromIndex, toIndex)) { + for (String osrsHiScoresStats : hiScores.subList(fromIndex, toIndex + 1)) { List<String> hiScoreStatValues = Arrays.stream(osrsHiScoresStats.split(",")).collect(Collectors.toList()); try { hiScoreList.add(hiScoreBuilder.accept(osrsHiScoresStats, hiScoreStatValues, i)); diff --git a/src/test/java/com/sxcy/sxcybot/client/GrandExchangeClientTest.java b/src/test/java/com/sxcy/sxcybot/client/GrandExchangeClientTest.java @@ -12,6 +12,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(SpringExtension.class) class GrandExchangeClientTest { @@ -33,6 +34,7 @@ class GrandExchangeClientTest { System.out.println(result); assertNotNull(result); + assertTrue(Integer.parseInt(result) > 0); } @Test diff --git a/src/test/java/com/sxcy/sxcybot/client/HiScoreClientTest.java b/src/test/java/com/sxcy/sxcybot/client/HiScoreClientTest.java @@ -46,15 +46,25 @@ class HiScoreClientTest { void getHiScoreStatsValid() { String playerName = "Zxxy"; List<OsrsHiscoreStat> osrsHiscoreStats = newArrayList(OsrsHiscoreStat.builder() - .name("Overall") - .orderValue(0) - .build()); + .name("Overall") + .orderValue(0) + .build(), + OsrsHiscoreStat.builder() + .name("Construction") + .orderValue(23) + .build()); given(osrsHiscoreStatService.findAll()).willReturn(osrsHiscoreStats); + Optional<List<OsrsStat>> result = hiScoreClient.getHiScoreStats(playerName, messageChannel); + assertFalse(result.isEmpty()); - assertEquals(1, result.get().size()); + assertEquals(2, result.get().size()); + assertEquals("Overall", result.get().get(0).getName()); - assertEquals("1939", result.get().get(0).getLevel()); + assertTrue(1939 <= Integer.parseInt(result.get().get(0).getLevel())); + + assertEquals("Construction", result.get().get(1).getName()); + assertTrue(90 <= Integer.parseInt(result.get().get(1).getLevel())); } @Test @@ -70,16 +80,26 @@ class HiScoreClientTest { void getHiscoreBossKcValidKbdKc() { String playerName = "Zxxy"; List<OsrsHiscoreBoss> hiscoreBosses = newArrayList(OsrsHiscoreBoss.builder() - .name("KBD") - .orderValue(58) - .build()); + .name("KBD") + .orderValue(58) + .build(), + OsrsHiscoreBoss.builder() + .name("Wintertodt") + .orderValue(80) + .build() + ); + given(osrsHiscoreBossService.findAll()).willReturn(hiscoreBosses); Optional<List<OsrsBossKc>> result = hiScoreClient.getHiScoreBossKc(playerName, messageChannel); assertFalse(result.isEmpty()); - assertEquals(1, result.get().size()); + assertEquals(2, result.get().size()); + assertEquals("KBD", result.get().get(0).getName()); - assertEquals("723", result.get().get(0).getKc()); + assertTrue(723 <= Integer.parseInt(result.get().get(0).getKc())); + + assertEquals("Wintertodt", result.get().get(1).getName()); + assertTrue(87 <= Integer.parseInt(result.get().get(1).getKc())); } @Test