From ee8f34fc9b40c46b76592f7ffe5987fc011e606e Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:54:15 -0500 Subject: [PATCH 1/4] First WIP on API-V2 --- .../botdetector/http/BotDetectorClient.java | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index ed00e175..fb3b556d 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -85,22 +85,24 @@ public class BotDetectorClient private static final String API_VERSION_FALLBACK_WORD = "latest"; private static final HttpUrl BASE_HTTP_URL = HttpUrl.parse( System.getProperty("BotDetectorAPIPath", "https://api.prd.osrsbotdetector.com")); + private static final HttpUrl BASE_HTTP_URL_V2 = HttpUrl.parse( + System.getProperty("BotDetectorAPIPathV2", "https://api-v2.prd.osrsbotdetector.com")); private static final Supplier CURRENT_EPOCH_SUPPLIER = () -> String.valueOf(Instant.now().getEpochSecond()); @Getter @AllArgsConstructor private enum ApiPath { - DETECTION("v1/report"), - PLAYER_STATS_PASSIVE("v1/report/count"), - PLAYER_STATS_MANUAL("v1/report/manual/count"), - PLAYER_STATS_FEEDBACK("v1/feedback/count"), - PREDICTION("v1/prediction"), - FEEDBACK("v1/feedback/"), - VERIFY_DISCORD("site/discord_user/") + DETECTION("v2/report", true), + PLAYER_STATS_REPORTS("v2/player/report/score", true), + PLAYER_STATS_FEEDBACK("v2/player/feedback/score", true), + PREDICTION("v2/player/prediction", true), + FEEDBACK("v1/feedback/", false), + VERIFY_DISCORD("site/discord_user/", false) ; final String path; + final boolean v2; } public OkHttpClient okHttpClient; @@ -123,7 +125,7 @@ private enum ApiPath */ private HttpUrl getUrl(ApiPath path, boolean addVersion) { - HttpUrl.Builder builder = BASE_HTTP_URL.newBuilder(); + HttpUrl.Builder builder = (path.isV2() ? BASE_HTTP_URL_V2 : BASE_HTTP_URL).newBuilder(); if (addVersion) { @@ -398,7 +400,15 @@ public void onResponse(Call call, Response response) { try { - future.complete(processResponse(gson, response, Prediction.class)); + Collection preds = processResponse(gson, response, new TypeToken>() + { + }.getType()); + if (preds != null) + { + future.complete(preds.stream().findFirst().orElse(null)); + return; + } + future.complete(null); } catch (IOException e) { @@ -422,18 +432,8 @@ public void onResponse(Call call, Response response) */ public CompletableFuture> requestPlayerStats(String playerName) { - Gson bdGson = gson.newBuilder() - .registerTypeAdapter(boolean.class, new BooleanToZeroOneConverter()) - .create(); - - Request requestP = new Request.Builder() - .url(getUrl(ApiPath.PLAYER_STATS_PASSIVE).newBuilder() - .addQueryParameter("name", playerName) - .build()) - .build(); - - Request requestM = new Request.Builder() - .url(getUrl(ApiPath.PLAYER_STATS_MANUAL).newBuilder() + Request requestR = new Request.Builder() + .url(getUrl(ApiPath.PLAYER_STATS_REPORTS).newBuilder() .addQueryParameter("name", playerName) .build()) .build(); @@ -444,18 +444,16 @@ public CompletableFuture> requestPlayerStats(S .build()) .build(); - CompletableFuture> passiveFuture = new CompletableFuture<>(); - CompletableFuture> manualFuture = new CompletableFuture<>(); + CompletableFuture> reportsFuture = new CompletableFuture<>(); CompletableFuture> feedbackFuture = new CompletableFuture<>(); - okHttpClient.newCall(requestP).enqueue(new PlayerStatsCallback(passiveFuture, bdGson)); - okHttpClient.newCall(requestM).enqueue(new PlayerStatsCallback(manualFuture, bdGson)); - okHttpClient.newCall(requestF).enqueue(new PlayerStatsCallback(feedbackFuture, bdGson)); + okHttpClient.newCall(requestR).enqueue(new PlayerStatsCallback(reportsFuture, gson)); + okHttpClient.newCall(requestF).enqueue(new PlayerStatsCallback(feedbackFuture, gson)); CompletableFuture> finalFuture = new CompletableFuture<>(); - // Doing this so we log only the first future failing, not all 3 within the callback. - CompletableFuture.allOf(passiveFuture, manualFuture, feedbackFuture).whenComplete((v, e) -> + // Doing this so we log only the first future failing, not all 2 within the callback. + CompletableFuture.allOf(reportsFuture, feedbackFuture).whenComplete((v, e) -> { if (e != null) { @@ -465,8 +463,7 @@ public CompletableFuture> requestPlayerStats(S } else { - finalFuture.complete(processPlayerStats( - passiveFuture.join(), manualFuture.join(), feedbackFuture.join())); + finalFuture.complete(processPlayerStats(reportsFuture.join(), feedbackFuture.join())); } }); @@ -592,20 +589,21 @@ private IOException getIOException(Response response) /** * Collects the given {@link PlayerStatsAPIItem} into a combined map that the plugin expects. - * @param passive The passive usage stats from the API. - * @param manual The manual flagging stats from the API. + * @param reports The reports usage stats from the API. * @param feedback The feedback stats from the API. * @return The combined processed map expected by the plugin. */ - private Map processPlayerStats(Collection passive, Collection manual, Collection feedback) + private Map processPlayerStats(Collection reports, Collection feedback) { - if (passive == null || manual == null || feedback == null) + if (reports == null || feedback == null) { return null; } - PlayerStats passiveStats = countStats(passive, false); - PlayerStats manualStats = countStats(manual, true); + PlayerStats passiveStats = countStats(reports.stream().filter( + r -> r.getManual() != null && !r.getManual()).collect(Collectors.toList()), false); + PlayerStats manualStats = countStats(reports.stream().filter( + r -> r.getManual() != null && r.getManual()).collect(Collectors.toList()), true); PlayerStats feedbackStats = countStats(feedback, false); PlayerStats totalStats = PlayerStats.builder() @@ -623,7 +621,7 @@ private Map processPlayerStats(Collection Date: Thu, 1 Feb 2024 19:40:10 -0500 Subject: [PATCH 2/4] Lowercase placeholder labels --- src/main/java/com/botdetector/ui/BotDetectorPanel.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/botdetector/ui/BotDetectorPanel.java b/src/main/java/com/botdetector/ui/BotDetectorPanel.java index e7cb4278..7b808328 100644 --- a/src/main/java/com/botdetector/ui/BotDetectorPanel.java +++ b/src/main/java/com/botdetector/ui/BotDetectorPanel.java @@ -157,9 +157,9 @@ public enum WarningLabel private static final int MAX_FEEDBACK_TEXT_CHARS = 250; private static final Dimension FEEDBACK_TEXTBOX_PREFERRED_SIZE = new Dimension(0, 75); - private static final FeedbackPredictionLabel UNSURE_PREDICTION_LABEL = new FeedbackPredictionLabel("Unsure", null, FeedbackValue.NEUTRAL); - private static final FeedbackPredictionLabel SOMETHING_ELSE_PREDICTION_LABEL = new FeedbackPredictionLabel("Something_else", null, FeedbackValue.NEGATIVE); - private static final FeedbackPredictionLabel CORRECT_FALLBACK_PREDICTION_LABEL = new FeedbackPredictionLabel("Correct", null, FeedbackValue.POSITIVE); + private static final FeedbackPredictionLabel UNSURE_PREDICTION_LABEL = new FeedbackPredictionLabel("unsure", null, FeedbackValue.NEUTRAL); + private static final FeedbackPredictionLabel SOMETHING_ELSE_PREDICTION_LABEL = new FeedbackPredictionLabel("something_else", null, FeedbackValue.NEGATIVE); + private static final FeedbackPredictionLabel CORRECT_FALLBACK_PREDICTION_LABEL = new FeedbackPredictionLabel("correct", null, FeedbackValue.POSITIVE); private static final PlayerStatsType[] PLAYER_STAT_TYPES = { PlayerStatsType.TOTAL, PlayerStatsType.PASSIVE, PlayerStatsType.MANUAL From ce1af6fd4b9e10919fb16b9c7bfa712e132f74c2 Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Thu, 1 Feb 2024 19:40:40 -0500 Subject: [PATCH 3/4] Use v2/feeback, remove v2 api switch --- .../com/botdetector/http/BotDetectorClient.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index fb3b556d..a40bceea 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -85,24 +85,21 @@ public class BotDetectorClient private static final String API_VERSION_FALLBACK_WORD = "latest"; private static final HttpUrl BASE_HTTP_URL = HttpUrl.parse( System.getProperty("BotDetectorAPIPath", "https://api.prd.osrsbotdetector.com")); - private static final HttpUrl BASE_HTTP_URL_V2 = HttpUrl.parse( - System.getProperty("BotDetectorAPIPathV2", "https://api-v2.prd.osrsbotdetector.com")); private static final Supplier CURRENT_EPOCH_SUPPLIER = () -> String.valueOf(Instant.now().getEpochSecond()); @Getter @AllArgsConstructor private enum ApiPath { - DETECTION("v2/report", true), - PLAYER_STATS_REPORTS("v2/player/report/score", true), - PLAYER_STATS_FEEDBACK("v2/player/feedback/score", true), - PREDICTION("v2/player/prediction", true), - FEEDBACK("v1/feedback/", false), - VERIFY_DISCORD("site/discord_user/", false) + DETECTION("v2/report"), + PLAYER_STATS_REPORTS("v2/player/report/score"), + PLAYER_STATS_FEEDBACK("v2/player/feedback/score"), + PREDICTION("v2/player/prediction"), + FEEDBACK("v2/feedback"), + VERIFY_DISCORD("site/discord_user") ; final String path; - final boolean v2; } public OkHttpClient okHttpClient; @@ -125,7 +122,7 @@ private enum ApiPath */ private HttpUrl getUrl(ApiPath path, boolean addVersion) { - HttpUrl.Builder builder = (path.isV2() ? BASE_HTTP_URL_V2 : BASE_HTTP_URL).newBuilder(); + HttpUrl.Builder builder = BASE_HTTP_URL.newBuilder(); if (addVersion) { From b5078722ea0af724e708428e45f1ebddd9766785 Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:15:02 -0500 Subject: [PATCH 4/4] Revert to v1/feedback for now --- src/main/java/com/botdetector/http/BotDetectorClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index a40bceea..c775e989 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -95,7 +95,7 @@ private enum ApiPath PLAYER_STATS_REPORTS("v2/player/report/score"), PLAYER_STATS_FEEDBACK("v2/player/feedback/score"), PREDICTION("v2/player/prediction"), - FEEDBACK("v2/feedback"), + FEEDBACK("v1/feedback/"), // Remove last forward slash when using v2! VERIFY_DISCORD("site/discord_user") ;