From 1d1799692e3e32eaede679b0c45a1ebaa34ecf3d Mon Sep 17 00:00:00 2001 From: jalpp <92553013+jalpp@users.noreply.github.com> Date: Wed, 8 May 2024 09:19:18 -0400 Subject: [PATCH] refactor(/Fide) remove unsupported FIDE API integration with cache Fide top10 as top10 does not change often --- .../HelperModules/ToolContextModule.java | 14 +- src/main/java/Fide/FideClient.java | 164 ------------------ 2 files changed, 12 insertions(+), 166 deletions(-) delete mode 100644 src/main/java/Fide/FideClient.java diff --git a/src/main/java/Discord/HelperModules/ToolContextModule.java b/src/main/java/Discord/HelperModules/ToolContextModule.java index bfd62d8..1b08c47 100644 --- a/src/main/java/Discord/HelperModules/ToolContextModule.java +++ b/src/main/java/Discord/HelperModules/ToolContextModule.java @@ -3,7 +3,6 @@ import Chesscom.DailyCommandCC; import Chesscom.puzzle; import Discord.MainHandler.AntiSpam; -import Fide.FideClient; import Lichess.*; import chariot.Client; import net.dv8tion.jda.api.EmbedBuilder; @@ -170,7 +169,18 @@ public void sendTop10FideEmbed(SlashCommandInteractionEvent slashEvent){ EmbedBuilder builder = new EmbedBuilder(); builder.setTitle("FIDE Top10 Players"); builder.setThumbnail("https://upload.wikimedia.org/wikipedia/en/thumb/5/5b/Fidelogo.svg/1200px-Fidelogo.svg.png"); - builder.setDescription(FideClient.getTopNInString("standard", 10)); + builder.setDescription(""" + 1) GM Carlsen, Magnus, standard: (2830) + 2) GM Kasparov, Garry, standard: (2812) + 3) GM Caruana, Fabiano, standard: (2805) + 4) GM Nakamura, Hikaru, standard: (2794) + 5) GM Nepomniachtchi, Ian, standard: (2770) + 6) GM Abdusattorov, Nodirbek, standard: (2765) + 7) GM Gukesh D, standard: (2763) + 8) GM Ding, Liren, standard: (2762) + 9) GM Erigaisi Arjun, standard: (2761) + 10) GM So, Wesley, standard: (2757) + """); builder.setColor(Color.WHITE); slashEvent.replyEmbeds(builder.build()).queue(); } diff --git a/src/main/java/Fide/FideClient.java b/src/main/java/Fide/FideClient.java deleted file mode 100644 index 10eb643..0000000 --- a/src/main/java/Fide/FideClient.java +++ /dev/null @@ -1,164 +0,0 @@ -package Fide; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - - -// represents the FIDE API client for Java -// Feel free to use the client in your codebase -// Full documentation for the API at http://fide.thanh.se/docs/index.html - -public class FideClient { - - - private static final String API_ENDPOINT = "https://fide.thanh.se/api/players"; - - - public FideClient(){ - - } - - - public static void main(String[] args) { - String res = getQueryByTag("name", "magnus"); - System.out.println(res); - } - - /** - * get the Top n (size) players by rating band of (bandName) - * size: the size of page - * bandName: the rating band - */ - - public static String getTopNPlayerForNRatingBand(int size, String bandName){ - try { - - - URL url = new URL(API_ENDPOINT + "?sort_by=" + bandName + "&order=desc&page_size=" + size); - - - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - - int responseCode = connection.getResponseCode(); - - - if (responseCode == HttpURLConnection.HTTP_OK) { - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - String line; - StringBuilder response = new StringBuilder(); - - while ((line = reader.readLine()) != null) { - response.append(line); - } - - reader.close(); - - return response.toString(); - } else { - System.out.println("Failed to make API request. Response Code: " + responseCode); - } - - // Close the connection - connection.disconnect(); - - } catch (Exception e) { - e.printStackTrace(); - } - - return "Error!"; - - } - - /** - * run a query on the fide api by providing query and searchTag - * query: query fields in FIDE API - * searchTag: the value to be searched for - */ - - - public static String getQueryByTag(String query, String searchTag){ - try { - - URL url = new URL(API_ENDPOINT + "?" + query+ "=" + searchTag); - - - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - - int responseCode = connection.getResponseCode(); - - - if (responseCode == HttpURLConnection.HTTP_OK) { - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - String line; - StringBuilder response = new StringBuilder(); - - while ((line = reader.readLine()) != null) { - response.append(line); - } - - reader.close(); - - return response.toString(); - } else { - System.out.println("Failed to make API request. Response Code: " + responseCode); - } - - // Close the connection - connection.disconnect(); - - } catch (Exception e) { - e.printStackTrace(); - } - - return "Error!"; - - } - - /** - * creates a Top10 leaderboard String builder String, used for Discord app, can also be used for debugging - * responseData: A JSON FIDE data in string format - */ - - - public static String StringfyResponse(String responseData) { - try { - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode rootNode = objectMapper.readTree(responseData); - StringBuilder formattedData = new StringBuilder(); - JsonNode itemsNode = rootNode.get("items"); - int counter = 0; - for (JsonNode item : itemsNode) { - counter++; - String name = item.get("name").asText(); - String title = item.get("title").asText(); - int standardRating = item.get("standard").asInt(); - formattedData.append(counter).append(". ").append(title).append(" ").append(name).append(", standard: (").append(standardRating).append(") \n"); - } - return formattedData.toString(); - } catch (IOException e) { - e.printStackTrace(); - return "Error parsing response"; - } - } - - // helper method used in the app and command /fide - - public static String getTopNInString(String ratingBand, int size){ - String res = getTopNPlayerForNRatingBand(size, ratingBand); - return StringfyResponse(res); - } - - - - - - -}