From ef0251908ffbf3425f7006af1e4eb652d0e38b7a Mon Sep 17 00:00:00 2001 From: Peijun Ma Date: Sat, 7 Oct 2023 18:08:23 -0400 Subject: [PATCH] Allow setting api url in slaytherelics_config.txt --- .../java/str_exporter/BackendBroadcaster.java | 49 ++++++++----------- .../str_exporter/SlayTheRelicsExporter.java | 31 +++++++----- src/main/resources/ModTheSpire.json | 2 +- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/main/java/str_exporter/BackendBroadcaster.java b/src/main/java/str_exporter/BackendBroadcaster.java index 70c12b3..fc87d0b 100644 --- a/src/main/java/str_exporter/BackendBroadcaster.java +++ b/src/main/java/str_exporter/BackendBroadcaster.java @@ -4,6 +4,7 @@ import org.apache.logging.log4j.Logger; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; @@ -15,12 +16,6 @@ public class BackendBroadcaster { public static final Logger logger = LogManager.getLogger(BackendBroadcaster.class.getName()); -// private static final String EBS_URL = "https://localhost:8081"; - private static final String EBS_URL = "https://slaytherelics.xyz:8080"; - -// private static final long CHECK_QUEUE_PERIOD_MILLIS = 100; -// private static BackendBroadcaster instance = new BackendBroadcaster(); - public static final String DELAY_PLACEHOLDER = "&&&DELAY&&&"; private String message; @@ -71,7 +66,7 @@ private void readQueue() { long ts = 0; queueLock.lock(); try { - if (message != null && (sendDuplicates || !message.equals(lastMessage))) { + if (message != null && (sendDuplicates || !message.equals(lastMessage))) { lastMessage = message; msg = message; ts = messageTimestamp; @@ -94,34 +89,32 @@ private static String injectDelayToMessage(String msg, long delay) { } private void broadcastMessage(String msg) { - - try { - URL url = new URL(EBS_URL); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("Content-Type", "application/json"); //; utf-8 - con.setRequestProperty("Accept", "application/json"); - con.setDoOutput(true); - - OutputStream os = con.getOutputStream(); - byte[] input = msg.getBytes(StandardCharsets.UTF_8); - os.write(input, 0, input.length); - - BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); + try (BufferedReader br = makeRequest(msg)) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } - - if (!response.toString().equals("Success")) - logger.info("message not broadcasted succesfully, response: " + response.toString()); -// logger.info("broadcasted message, response: " + response.toString()); - + if (!response.toString().equals("Success")) { + logger.info("message not broadcast successfully, response: " + response); + } } catch (Exception e) { e.printStackTrace(); - } finally { -// logger.info(SlayTheRelicsExporter.removeSecret(msg)); } } + + private static BufferedReader makeRequest(String msg) throws IOException { + URL url = new URL(SlayTheRelicsExporter.getApiUrl()); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); //; utf-8 + con.setRequestProperty("Accept", "application/json"); + con.setDoOutput(true); + + OutputStream os = con.getOutputStream(); + byte[] input = msg.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + + return new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); + } } diff --git a/src/main/java/str_exporter/SlayTheRelicsExporter.java b/src/main/java/str_exporter/SlayTheRelicsExporter.java index df7f9c7..68ef9c5 100644 --- a/src/main/java/str_exporter/SlayTheRelicsExporter.java +++ b/src/main/java/str_exporter/SlayTheRelicsExporter.java @@ -28,8 +28,7 @@ import java.util.Properties; @SpireInitializer -public class SlayTheRelicsExporter implements - RelicGetSubscriber, +public class SlayTheRelicsExporter implements RelicGetSubscriber, PotionGetSubscriber, StartGameSubscriber, PostCreateStartingRelicsSubscriber, @@ -70,6 +69,7 @@ public class SlayTheRelicsExporter implements public static Properties strDefaultSettings = new Properties(); public static final String DELAY_SETTINGS = "delay"; public static long delay = 0; // The boolean we'll be setting on/off (true/false) + private static String apiUrl = ""; private static long lastOkayBroadcast = 0; public SlayTheRelicsExporter() { @@ -108,20 +108,17 @@ public static void initialize() { logger.info("initialize() called!"); version = getVersion(); try { - Path path = Paths.get("slaytherelics_config.txt"); - if (!Files.exists(path)) - path = Paths.get("slaytherelics_config.txt.txt"); + if (!Files.exists(path)) path = Paths.get("slaytherelics_config.txt.txt"); String data = new String(Files.readAllBytes(path)); List lines = Files.readAllLines(path); login = lines.get(0).split(":")[1].toLowerCase().trim(); secret = lines.get(1).split(":")[1].trim(); + setApiUrl(lines); logger.info("slaytherelics_config.txt was succesfully loaded"); - -// logger.info("loaded login " + login + " and secret " + secret); } catch (Exception e) { e.printStackTrace(); } @@ -133,6 +130,20 @@ public static void initialize() { instance = new SlayTheRelicsExporter(); } + private static void setApiUrl(List lines) { + SlayTheRelicsExporter.apiUrl = "https://str.otonokizaka.moe"; + for (String line : lines) { + if (line.startsWith("api_url:")) { + SlayTheRelicsExporter.apiUrl = line.replaceFirst("api_url:", "").trim(); + break; + } + } + } + + public static String getApiUrl() { + return apiUrl; + } + private void queue_check() { checkTipsNextUpdate = true; checkDeckNextUpdate = true; @@ -159,7 +170,6 @@ private void broadcastDeck() { @Override public void receivePostInitialize() { - tipsBroadcaster = new BackendBroadcaster(BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); deckBroadcaster = new BackendBroadcaster(BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, false); okayBroadcaster = new BackendBroadcaster(BROADCAST_CHECK_QUEUE_PERIOD_MILLIS, true); @@ -167,7 +177,6 @@ public void receivePostInitialize() { deckJsonBuilder = new DeckJSONBuilder(login, secret, version); okayJsonBuilder = new JSONMessageBuilder(login, secret, version, 5); - ModPanel settingsPanel = new ModPanel(); ModLabel @@ -190,6 +199,7 @@ public void receivePostInitialize() { logger.info("slider value: " + me.value); delay = (long) (me.value * me.multiplier); }); + ModLabelButton btn = new ModLabelButton("Save", 400f, 480f, settingsPanel, (me) -> { try { SpireConfig @@ -209,8 +219,7 @@ public void receivePostInitialize() { slider.setValue(delay * 1.0f / slider.multiplier); - BaseMod.registerModBadge(ImageMaster.loadImage( - "SlayTheRelicsExporterResources/img/ink_bottle.png"), + BaseMod.registerModBadge(ImageMaster.loadImage("SlayTheRelicsExporterResources/img/ink_bottle.png"), "Slay the Relics Exporter", "LordAddy, vmService", "This mod exports data to Slay the Relics Twitch extension. See the extension config on Twitch for setup instructions.", diff --git a/src/main/resources/ModTheSpire.json b/src/main/resources/ModTheSpire.json index 2575afd..f1ea480 100644 --- a/src/main/resources/ModTheSpire.json +++ b/src/main/resources/ModTheSpire.json @@ -3,7 +3,7 @@ "name": "Slay the Relics Exporter", "author_list": ["LordAddy", "vmService"], "description": "This mod exports data to Slay the Relics Twitch extension. \n\nThis mod in combination with the extension displays deck view and tooltips for viewers on stream for relics, potions, player/monster powers, orbs, even some custom tooltips from some mods. \nThe viewers just need to hover over the respective item just as if they were in the game themselves.\n\nSee the extension config on Twitch for setup instructions (search 'Slay the Relics').", - "version": "1.2.4", + "version": "1.2.5", "sts_version": "07-30-2020", "mts_version": "3.15.0", "dependencies": ["basemod"]