Skip to content

Commit

Permalink
Allow setting api url in slaytherelics_config.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
MaT1g3R committed Oct 7, 2023
1 parent ea6726c commit ef02519
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
49 changes: 21 additions & 28 deletions src/main/java/str_exporter/BackendBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
31 changes: 20 additions & 11 deletions src/main/java/str_exporter/SlayTheRelicsExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import java.util.Properties;

@SpireInitializer
public class SlayTheRelicsExporter implements
RelicGetSubscriber,
public class SlayTheRelicsExporter implements RelicGetSubscriber,
PotionGetSubscriber,
StartGameSubscriber,
PostCreateStartingRelicsSubscriber,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<String> 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();
}
Expand All @@ -133,6 +130,20 @@ public static void initialize() {
instance = new SlayTheRelicsExporter();
}

private static void setApiUrl(List<String> 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;
Expand All @@ -159,15 +170,13 @@ 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);
tipsJsonBuilder = new TipsJSONBuilder(login, secret, version);
deckJsonBuilder = new DeckJSONBuilder(login, secret, version);
okayJsonBuilder = new JSONMessageBuilder(login, secret, version, 5);


ModPanel settingsPanel = new ModPanel();

ModLabel
Expand All @@ -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
Expand All @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/ModTheSpire.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit ef02519

Please sign in to comment.