Repository: pajlads/DinkPlugin
Pull request #387 (2023-12-11 04:51:19)

refactor: minor changes to leagues code
just the simple parts from #368


iProdigy: tested in-game :+1: 
![image](https://github.com/pajlads/DinkPlugin/assets/8106344/d37eb61c-658a-4331-9db1-3b1d0f50dfbf)


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c74e7b6..f5c42a7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
 - Bugfix: Update set of items that are never kept on dangerous deaths. (#364)
 - Bugfix: Don't report inaccurate completed collections count, when character summary tab was not selected. (#374)
 - Bugfix: Avoid undercounting diary completions for notifications that occur shortly after a teleport. (#373)
+- Dev: Small refactor of leagues notifier. (#387)
 - Dev: Execute screenshot rescaling on background thread to minimize FPS impact. (#378)
 
 ## 1.7.2
diff --git a/README.md b/README.md
index 4500329..1d80269 100644
--- a/README.md
+++ b/README.md
@@ -873,6 +873,7 @@ For all players, Karamja is the _zeroth_ region selected (and there is no notifi
     "taskPoints": 10,
     "totalPoints": 30,
     "tasksCompleted": 3,
+    "tasksUntilNextArea": 57,
     "pointsUntilNextRelic": 470,
     "pointsUntilNextTrophy": 2470
   }
diff --git a/src/main/java/dinkplugin/notifiers/LeaguesNotifier.java b/src/main/java/dinkplugin/notifiers/LeaguesNotifier.java
index 81e3443..be111a7 100644
--- a/src/main/java/dinkplugin/notifiers/LeaguesNotifier.java
+++ b/src/main/java/dinkplugin/notifiers/LeaguesNotifier.java
@@ -47,6 +47,14 @@ public class LeaguesNotifier extends BaseNotifier {
     @VisibleForTesting
     static final @Varbit int FIVE_AREAS = 10666, FOUR_AREAS = 10665, THREE_AREAS = 10664, TWO_AREAS = 10663;
 
+    /**
+     * @see <a href="https://github.com/Joshua-F/cs2-scripts/blob/fa31b06ec5a9f6636bf9b9d5cbffbb71df022d06/scripts/[proc%2Cscript2451].cs2#L3-L6">CS2 Reference</a>
+     * @see <a href="https://abextm.github.io/cache2/#/viewer/enum/2670">Enum Reference</a>
+     * @see <a href="https://abextm.github.io/cache2/#/viewer/struct/4699">Struct Reference</a>
+     */
+    @VisibleForTesting
+    static final @Varbit int LEAGUES_VERSION = 10032; // 4 for Leagues IV
+
     /**
      * Trophy name by the required points, in a binary search tree.
      *
@@ -72,6 +80,7 @@ public class LeaguesNotifier extends BaseNotifier {
     @Override
     public boolean isEnabled() {
         return config.notifyLeagues() &&
+            client.getVarbitValue(LEAGUES_VERSION) == 4 &&
             client.getWorldType().contains(WorldType.SEASONAL) &&
             settingsManager.isNamePermitted(client.getLocalPlayer().getName());
     }
@@ -144,19 +153,17 @@ public class LeaguesNotifier extends BaseNotifier {
         if (relicTier == null) {
             // shouldn't happen, but just to be safe
             log.warn("Unknown relic encountered: {}", relic);
-            if (points >= 0) {
-                relicTier = LeagueRelicTier.TIER_BY_POINTS.floorEntry(points).getValue();
-            }
+            relicTier = LeagueRelicTier.TIER_BY_POINTS.floorEntry(Math.max(points, 0)).getValue();
         }
-        Integer tier = relicTier != null ? relicTier.ordinal() + 1 : null;
-        Integer requiredPoints = relicTier != null ? relicTier.getPoints() : null;
+        int tier = relicTier.ordinal() + 1;
+        int requiredPoints = relicTier.getPoints();
 
         String playerName = Utils.getPlayerName(client);
         Template text = Template.builder()
             .template("%USERNAME% unlocked a Tier %TIER% Relic: %RELIC%.")
             .replacementBoundary("%")
             .replacement("%USERNAME%", Replacements.ofText(playerName))
-            .replacement("%TIER%", Replacements.ofText(tier != null ? tier.toString() : "?"))
+            .replacement("%TIER%", Replacements.ofText(String.valueOf(tier)))
             .replacement("%RELIC%", Replacements.ofWiki(relic))
             .build();
         createMessage(config.leaguesSendImage(), NotificationBody.builder()
diff --git a/src/main/java/dinkplugin/notifiers/data/LeaguesRelicNotificationData.java b/src/main/java/dinkplugin/notifiers/data/LeaguesRelicNotificationData.java
index a89c74e..ff4725d 100644
--- a/src/main/java/dinkplugin/notifiers/data/LeaguesRelicNotificationData.java
+++ b/src/main/java/dinkplugin/notifiers/data/LeaguesRelicNotificationData.java
@@ -15,8 +15,8 @@ public class LeaguesRelicNotificationData extends NotificationData {
 
     @NotNull
     String relic;
-    Integer tier;
-    Integer requiredPoints;
+    int tier;
+    int requiredPoints;
     int totalPoints;
 
     @Nullable // if relics for all 8 tiers have now been unlocked
diff --git a/src/main/java/dinkplugin/notifiers/data/LeaguesTaskNotificationData.java b/src/main/java/dinkplugin/notifiers/data/LeaguesTaskNotificationData.java
index f03b18b..cee6c43 100644
--- a/src/main/java/dinkplugin/notifiers/data/LeaguesTaskNotificationData.java
+++ b/src/main/java/dinkplugin/notifiers/data/LeaguesTaskNotificationData.java
@@ -41,7 +41,9 @@ public class LeaguesTaskNotificationData extends NotificationData {
         List<Field> fields = new ArrayList<>(3);
         fields.add(new Field("Total Tasks", Field.formatBlock("", String.valueOf(tasksCompleted))));
         fields.add(new Field("Total Points", Field.formatBlock("", String.valueOf(totalPoints))));
-        if (earnedTrophy == null && pointsUntilNextRelic != null) {
+        if (earnedTrophy == null && tasksUntilNextArea != null) {
+            fields.add(new Field("Tasks until next Area", Field.formatBlock("", String.valueOf(tasksUntilNextArea))));
+        } else if (earnedTrophy == null && pointsUntilNextRelic != null) {
             fields.add(new Field("Points until next Relic", Field.formatBlock("", String.valueOf(pointsUntilNextRelic))));
         } else if (pointsUntilNextTrophy != null) {
             fields.add(new Field("Points until next Trophy", Field.formatBlock("", String.valueOf(pointsUntilNextTrophy))));


Test patch:
diff --git a/src/test/java/dinkplugin/notifiers/LeaguesNotifierTest.java b/src/test/java/dinkplugin/notifiers/LeaguesNotifierTest.java
index 4a8a88a..a684883 100644
--- a/src/test/java/dinkplugin/notifiers/LeaguesNotifierTest.java
+++ b/src/test/java/dinkplugin/notifiers/LeaguesNotifierTest.java
@@ -39,6 +39,7 @@ public class LeaguesNotifierTest extends MockedNotifierTest {
         // client mocks
         when(client.getWorldType()).thenReturn(EnumSet.of(WorldType.SEASONAL));
         when(client.getVarbitValue(Varbits.ACCOUNT_TYPE)).thenReturn(AccountType.IRONMAN.ordinal());
+        when(client.getVarbitValue(LeaguesNotifier.LEAGUES_VERSION)).thenReturn(4);
 
         // config mocks
         when(config.notifyLeagues()).thenReturn(true);