aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide
diff options
context:
space:
mode:
authorsyeyoung <cyong06@naver.com>2021-07-18 18:02:44 +0900
committersyeyoung <cyong06@naver.com>2021-07-18 18:02:44 +0900
commita9e35133d52e271b70a06d77747cfbe867e065e3 (patch)
tree9543c11362a3da9e35274a79e1120c24e966176f /src/main/java/kr/syeyoung/dungeonsguide
parentbceb0858a8215dad1f70342035e26ac45b9d970d (diff)
downloadSkyblock-Dungeons-Guide-a9e35133d52e271b70a06d77747cfbe867e065e3.tar.gz
Skyblock-Dungeons-Guide-a9e35133d52e271b70a06d77747cfbe867e065e3.tar.bz2
Skyblock-Dungeons-Guide-a9e35133d52e271b70a06d77747cfbe867e065e3.zip
Move trivia answers to DG Backend StaticResource API (TM)
Also Move Dungeon Bonus Score to that
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide')
-rwxr-xr-xsrc/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java24
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/commands/CommandDungeonsGuide.java11
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/features/impl/dungeon/FeatureDungeonScore.java90
-rwxr-xr-xsrc/main/java/kr/syeyoung/dungeonsguide/roomprocessor/RoomProcessorTrivia.java87
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResource.java30
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResourceCache.java81
6 files changed, 197 insertions, 126 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java
index a169eeda..f2bdad43 100755
--- a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java
+++ b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java
@@ -35,6 +35,7 @@ import kr.syeyoung.dungeonsguide.stomp.CloseListener;
import kr.syeyoung.dungeonsguide.stomp.StompClient;
import kr.syeyoung.dungeonsguide.stomp.StompInterface;
import kr.syeyoung.dungeonsguide.utils.AhUtils;
+import kr.syeyoung.dungeonsguide.wsresource.StaticResourceCache;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResourcePack;
@@ -125,6 +126,7 @@ public class DungeonsGuide implements DGInterface, CloseListener {
RichPresenceManager.INSTANCE.setup();
MinecraftForge.EVENT_BUS.register(RichPresenceManager.INSTANCE);
MinecraftForge.EVENT_BUS.register(PartyManager.INSTANCE);
+ MinecraftForge.EVENT_BUS.register(StaticResourceCache.INSTANCE);
MinecraftForge.EVENT_BUS.register(PartyInviteViewer.INSTANCE);
AhUtils.registerTimer();
@@ -178,13 +180,6 @@ public class DungeonsGuide implements DGInterface, CloseListener {
t.printStackTrace();
}
}
- private void copy(InputStream inputStream, File f) throws IOException {
- FileOutputStream fos = new FileOutputStream(f);
- IOUtils.copy(inputStream, fos);
- fos.flush();
- fos.close();
- inputStream.close();
- }
@Getter
private File configDir;
@@ -205,15 +200,12 @@ public class DungeonsGuide implements DGInterface, CloseListener {
}
public void connectStomp() {
- ex.schedule(new Runnable() {
- @Override
- public void run() {
- try {
- stompConnection = new StompClient(new URI(stompURL), authenticator.getToken(), DungeonsGuide.this);
- MinecraftForge.EVENT_BUS.post(new StompConnectedEvent(stompConnection));
- } catch (Exception e) {
- e.printStackTrace();
- }
+ ex.schedule(() -> {
+ try {
+ stompConnection = new StompClient(new URI(stompURL), authenticator.getToken(), DungeonsGuide.this);
+ MinecraftForge.EVENT_BUS.post(new StompConnectedEvent(stompConnection));
+ } catch (Exception e) {
+ e.printStackTrace();
}
}, 5L, TimeUnit.SECONDS);
}
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/commands/CommandDungeonsGuide.java b/src/main/java/kr/syeyoung/dungeonsguide/commands/CommandDungeonsGuide.java
index b24b5194..8299020c 100644
--- a/src/main/java/kr/syeyoung/dungeonsguide/commands/CommandDungeonsGuide.java
+++ b/src/main/java/kr/syeyoung/dungeonsguide/commands/CommandDungeonsGuide.java
@@ -34,11 +34,13 @@ import kr.syeyoung.dungeonsguide.events.DungeonLeftEvent;
import kr.syeyoung.dungeonsguide.features.FeatureRegistry;
import kr.syeyoung.dungeonsguide.features.impl.party.playerpreview.FeatureViewPlayerOnJoin;
import kr.syeyoung.dungeonsguide.features.impl.party.api.ApiFetchur;
+import kr.syeyoung.dungeonsguide.party.PartyInviteViewer;
import kr.syeyoung.dungeonsguide.party.PartyManager;
import kr.syeyoung.dungeonsguide.roomprocessor.GeneralRoomProcessor;
import kr.syeyoung.dungeonsguide.stomp.*;
import kr.syeyoung.dungeonsguide.utils.AhUtils;
import kr.syeyoung.dungeonsguide.utils.MapUtils;
+import kr.syeyoung.dungeonsguide.wsresource.StaticResourceCache;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.network.NetworkPlayerInfo;
@@ -365,6 +367,8 @@ public class CommandDungeonsGuide extends CommandBase {
cosmeticsManager.requestPerms();
cosmeticsManager.requestCosmeticsList();
cosmeticsManager.requestActiveCosmetics();
+ StaticResourceCache.INSTANCE.purgeCache();
+
sender.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §fSuccessfully purged API Cache!"));
} else if (args[0].equals("pbroadcast")) {
try {
@@ -378,7 +382,12 @@ public class CommandDungeonsGuide extends CommandBase {
} catch (Exception e) {
e.printStackTrace();
}
- } else {
+ } else if (args[0].equals("requeststaticresource")) {
+ UUID uid = UUID.fromString(args[1]);
+ StaticResourceCache.INSTANCE.getResource(uid).thenAccept(a -> {
+ sender.addChatMessage(new ChatComponentText(a.getResourceID()+": "+a.getValue()+": "+a.isExists()));
+ });
+ } else{
sender.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §e/dg §7-§fOpens configuration gui"));
sender.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §e/dg gui §7-§fOpens configuration gui"));
sender.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §e/dg help §7-§fShows command help"));
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/features/impl/dungeon/FeatureDungeonScore.java b/src/main/java/kr/syeyoung/dungeonsguide/features/impl/dungeon/FeatureDungeonScore.java
index 1205dea4..a332a370 100644
--- a/src/main/java/kr/syeyoung/dungeonsguide/features/impl/dungeon/FeatureDungeonScore.java
+++ b/src/main/java/kr/syeyoung/dungeonsguide/features/impl/dungeon/FeatureDungeonScore.java
@@ -35,6 +35,8 @@ import kr.syeyoung.dungeonsguide.stomp.StompMessageHandler;
import kr.syeyoung.dungeonsguide.stomp.StompPayload;
import kr.syeyoung.dungeonsguide.stomp.StompSubscription;
import kr.syeyoung.dungeonsguide.utils.TextUtils;
+import kr.syeyoung.dungeonsguide.wsresource.StaticResource;
+import kr.syeyoung.dungeonsguide.wsresource.StaticResourceCache;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.minecraft.client.Minecraft;
@@ -45,8 +47,11 @@ import net.minecraft.util.MathHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
-public class FeatureDungeonScore extends TextHUDFeature implements StompConnectedListener, StompMessageHandler {
+public class FeatureDungeonScore extends TextHUDFeature {
public FeatureDungeonScore() {
super("Dungeon", "Display Current Score", "Calculate and Display current score\nThis data is from pure calculation and can be different from actual score.", "dungeon.stats.score", false, 200, getFontRenderer().FONT_HEIGHT * 4);
this.setEnabled(false);
@@ -131,61 +136,41 @@ public class FeatureDungeonScore extends TextHUDFeature implements StompConnecte
if (score == null) return new ArrayList<StyledText>();
int sum = score.time + score.skill + score.explorer + score.bonus;
if (this.<Boolean>getParameter("verbose").getValue()) {
- actualBit.add(new StyledText("Skill","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(score.skill+" ","score"));
- actualBit.add(new StyledText("(","brackets"));
- actualBit.add(new StyledText(score.deaths+" Deaths","etc"));
- actualBit.add(new StyledText(")\n","brackets"));
- actualBit.add(new StyledText("Explorer","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(score.explorer+" ","score"));
- actualBit.add(new StyledText("(","brackets"));
- actualBit.add(new StyledText("Rooms "+(score.fullyCleared ? "O":"X")+ " Secrets "+score.secrets+"/"+score.totalSecrets+(score.totalSecretsKnown ? "": "?"),"etc"));
- actualBit.add(new StyledText(")\n","brackets"));
- actualBit.add(new StyledText("Time","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(score.time+" ","score"));
- actualBit.add(new StyledText("Bonus","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(score.bonus+" ","score"));
- actualBit.add(new StyledText("Total","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(sum+"\n","score"));
+ actualBit.add(new StyledText("Skill", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(score.skill + " ", "score"));
+ actualBit.add(new StyledText("(", "brackets"));
+ actualBit.add(new StyledText(score.deaths + " Deaths", "etc"));
+ actualBit.add(new StyledText(")\n", "brackets"));
+ actualBit.add(new StyledText("Explorer", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(score.explorer + " ", "score"));
+ actualBit.add(new StyledText("(", "brackets"));
+ actualBit.add(new StyledText("Rooms " + (score.fullyCleared ? "O" : "X") + " Secrets " + score.secrets + "/" + score.totalSecrets + (score.totalSecretsKnown ? "" : "?"), "etc"));
+ actualBit.add(new StyledText(")\n", "brackets"));
+ actualBit.add(new StyledText("Time", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(score.time + " ", "score"));
+ actualBit.add(new StyledText("Bonus", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(score.bonus + " ", "score"));
+ actualBit.add(new StyledText("Total", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(sum + "\n", "score"));
actualBit.addAll(buildRequirement(score));
} else {
String letter = getLetter(sum);
- actualBit.add(new StyledText("Score","scorename"));
- actualBit.add(new StyledText(": ","separator"));
- actualBit.add(new StyledText(sum+" ","score"));
- actualBit.add(new StyledText("(","brackets"));
- actualBit.add(new StyledText(letter,"currentScore"));
- actualBit.add(new StyledText(")","brackets"));
+ actualBit.add(new StyledText("Score", "scorename"));
+ actualBit.add(new StyledText(": ", "separator"));
+ actualBit.add(new StyledText(sum + " ", "score"));
+ actualBit.add(new StyledText("(", "brackets"));
+ actualBit.add(new StyledText(letter, "currentScore"));
+ actualBit.add(new StyledText(")", "brackets"));
}
return actualBit;
}
- @Override
- public void onStompConnected(StompConnectedEvent event) {
- event.getStompInterface().subscribe(StompSubscription.builder()
- .stompMessageHandler(this).ackMode(StompSubscription.AckMode.AUTO).destination("/topic/dungeon.bonusscore").build());
- event.getStompInterface().subscribe(StompSubscription.builder()
- .stompMessageHandler(this).ackMode(StompSubscription.AckMode.AUTO).destination("/user/queue/dungeon.bonusscore").build());
-
- event.getStompInterface().send(new StompPayload().header("destination", "/app/dungeon.bonusscore.req"));
- }
-
- private int mayorScore = 0;
- @Override
- public void handle(StompInterface stompInterface, StompPayload stompPayload) {
- try {
- mayorScore = Integer.parseInt(stompPayload.payload().trim());
- } catch (Exception e) {
- }
- }
-
-
@Data
@AllArgsConstructor
public static class ScoreCalculation {
@@ -307,7 +292,14 @@ public class FeatureDungeonScore extends TextHUDFeature implements StompConnecte
{
bonus += tombs = MathHelper.clamp_int(FeatureRegistry.DUNGEON_TOMBS.getTombsFound(), 0, 5);
if (context.isGotMimic()) bonus += 2;
- bonus += mayorScore;
+ CompletableFuture<StaticResource> staticResourceCompletableFuture = StaticResourceCache.INSTANCE.getResource(StaticResourceCache.BONUS_SCORE);
+ if (staticResourceCompletableFuture.isDone()) {
+ try {
+ bonus += Integer.parseInt(staticResourceCompletableFuture.get().getValue().trim());
+ } catch (InterruptedException | ExecutionException e) {
+ e.printStackTrace();
+ }
+ }
}
// amazing thing
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/roomprocessor/RoomProcessorTrivia.java b/src/main/java/kr/syeyoung/dungeonsguide/roomprocessor/RoomProcessorTrivia.java
index 47c7e4ac..148dfbb9 100755
--- a/src/main/java/kr/syeyoung/dungeonsguide/roomprocessor/RoomProcessorTrivia.java
+++ b/src/main/java/kr/syeyoung/dungeonsguide/roomprocessor/RoomProcessorTrivia.java
@@ -25,10 +25,12 @@ import kr.syeyoung.dungeonsguide.features.FeatureRegistry;
import kr.syeyoung.dungeonsguide.utils.RenderUtils;
import kr.syeyoung.dungeonsguide.utils.SkyblockUtils;
import kr.syeyoung.dungeonsguide.utils.TextUtils;
+import kr.syeyoung.dungeonsguide.wsresource.StaticResourceCache;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import org.apache.commons.lang3.math.NumberUtils;
+import org.json.JSONObject;
import java.awt.*;
import java.io.IOException;
@@ -41,54 +43,12 @@ public class RoomProcessorTrivia extends GeneralRoomProcessor {
public RoomProcessorTrivia(DungeonRoom dungeonRoom) {
super(dungeonRoom);
-// for (Map.Entry<String, String[]> answer : answers.entrySet()) {
-// StringBuilder sb = new StringBuilder();
-// for (String s : answer.getValue()) {
-// sb.append(s).append(',');
-// }
-// dungeonRoom.getDungeonRoomInfo().getProperties().put(answer.getKey(), sb.toString());
-// }
}
private final List<String> questionDialog = new ArrayList<String>();
private boolean questionDialogStart = false;
-// private static final Map<String, String[]> answers = new HashMap<String,String[]>() {{
-// put("what is the status of the watcher?", new String[]{"stalker"});
-// put("what is the status of bonzo?", new String[]{"new necromancer"});
-// put("what is the status of scarf?", new String[]{"apprentice necromancer"});
-// put("what is the status of the professor?", new String[]{"professor"});
-// put("what is the status of thorn?", new String[]{"shaman necromancer"});
-// put("what is the status of livid?", new String[]{"master necromancer"});
-// put("what is the status of sadan?", new String[]{"necromancer lord"});
-// put("what is the status of maxor?", new String[]{"young wither"});
-// put("what is the status of goldor?", new String[]{"wither soldier"});
-// put("what is the status of storm?", new String[]{"elementalist"});
-// put("what is the status of necron?", new String[]{"wither lord"});
-// put("how many total fairy souls are there?", new String[]{"209 fairy souls"});
-// put("how many fairy souls are there in spider's den?", new String[]{"17"});
-// put("how many fairy souls are there in the end?", new String[]{"12"});
-// put("how many fairy souls are there in the barn?", new String[]{"7"});
-// put("how many fairy souls are there in mushroom desert?", new String[]{"8"});
-// put("how many fairy souls are there in blazing fortress?", new String[]{"19"});
-// put("how many fairy souls are there in the park?", new String[]{"11"});
-// put("how many fairy souls are there in jerry's workshop?", new String[]{"5"});
-// put("how many fairy souls are there in the hub?", new String[]{"79"});
-// put("how many fairy souls are there in deep caverns?", new String[]{"21"});
-// put("how many fairy souls are there in gold mine?", new String[]{"12"});
-// put("how many fairy souls are there in dungeon hub?", new String[]{"7"});
-// put("which brother is on the spider's den?", new String[]{"rick"});
-// put("what is the name of rick's brother?", new String[]{"pat"});
-// put("what is the name of the painter in the hub?", new String[]{"marco"});
-// put("what is the name of the person that upgrades pets?", new String[]{"kat"});
-// put("what is the name of the lady of the nether?", new String[]{"elle"});
-// put("which villager in the village gives you a rogue sword?", new String[]{"jamie"});
-// put("how many unique minions are there?", new String[]{"52"});
-// put("which of these enemies does not spawn in the spider's den?", new String[]{"zombie spider","cave spider","broodfather"});
-// put("which of these monsters only spawns at night?", new String[]{"zombie villager","ghast"});
-// put("which of these is not a dragon in the end?", new String[]{"zoomer dragon","weak dragon","stonk dragon","holy dragon","boomer dragon","stable dragon"});
-// }};
@Override
public void chatReceived(IChatComponent chat) {
super.chatReceived(chat);
@@ -113,15 +73,8 @@ public class RoomProcessorTrivia extends GeneralRoomProcessor {
String answerA = getAnswer(questionDialog.get(2));
String answerB = getAnswer(questionDialog.get(3));
String answerC = getAnswer(questionDialog.get(4));
- String theRealAnswer = match(question, answerA, answerB, answerC);
-
- if (theRealAnswer == null)
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: §cCouldn't determine the answer! (no question found)"));
- else if (theRealAnswer.length() >1)
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: §cCouldn't determine the answer! ("+theRealAnswer+")"));
- else
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: "+theRealAnswer+"§f is the correct answer!"));
- correctAnswer = theRealAnswer;
+ match(question, answerA, answerB, answerC);
+
}
String correctAnswer;
@@ -131,15 +84,29 @@ public class RoomProcessorTrivia extends GeneralRoomProcessor {
if (!matcher.matches()) return "";
return matcher.group(1);
}
- private String match(String question, String a, String b, String c) {
- String semi_answers = (String) getDungeonRoom().getDungeonRoomInfo().getProperties().get(question.toLowerCase().trim());
- if (semi_answers == null) return null;
- semi_answers = takeCareOfPlaceHolders(semi_answers);
- String[] answers = semi_answers.split(",");
- if (match(answers, a)) return "A";
- if (match(answers, b)) return "B";
- if (match(answers, c)) return "C";
- return semi_answers;
+ private void match(String question, String a, String b, String c) {
+ StaticResourceCache.INSTANCE.getResource(StaticResourceCache.TRIVIA_ANSWERS).thenAccept(value -> {
+ JSONObject answersJSON = new JSONObject(value.getValue());
+
+ String semi_answers = answersJSON.getString(question.toLowerCase().trim());
+ String theRealAnswer;
+ if (semi_answers == null) theRealAnswer = null;
+ else {
+ semi_answers = takeCareOfPlaceHolders(semi_answers);
+ String[] answers = semi_answers.split(",");
+ if (match(answers, a)) theRealAnswer = "A";
+ else if (match(answers, b)) theRealAnswer = "B";
+ else if (match(answers, c)) theRealAnswer = "C";
+ else theRealAnswer = semi_answers;
+ }
+ if (theRealAnswer == null)
+ Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: §cCouldn't determine the answer! (no question found)"));
+ else if (theRealAnswer.length() >1)
+ Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: §cCouldn't determine the answer! ("+theRealAnswer+")"));
+ else
+ Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §eTrivia §7:: "+theRealAnswer+"§f is the correct answer!"));
+ correctAnswer = theRealAnswer;
+ });
}
private String takeCareOfPlaceHolders(String input) {
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResource.java b/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResource.java
new file mode 100644
index 00000000..814fb8c8
--- /dev/null
+++ b/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResource.java
@@ -0,0 +1,30 @@
+/*
+ * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
+ * Copyright (C) 2021 cyoung06
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package kr.syeyoung.dungeonsguide.wsresource;
+
+import lombok.Data;
+
+import java.util.UUID;
+
+@Data
+public class StaticResource {
+ private UUID resourceID;
+ private String value;
+ private boolean exists;
+}
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResourceCache.java b/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResourceCache.java
new file mode 100644
index 00000000..b134b7c3
--- /dev/null
+++ b/src/main/java/kr/syeyoung/dungeonsguide/wsresource/StaticResourceCache.java
@@ -0,0 +1,81 @@
+/*
+ * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
+ * Copyright (C) 2021 cyoung06
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package kr.syeyoung.dungeonsguide.wsresource;
+
+import kr.syeyoung.dungeonsguide.DungeonsGuide;
+import kr.syeyoung.dungeonsguide.events.StompConnectedEvent;
+import kr.syeyoung.dungeonsguide.stomp.StompInterface;
+import kr.syeyoung.dungeonsguide.stomp.StompMessageHandler;
+import kr.syeyoung.dungeonsguide.stomp.StompPayload;
+import kr.syeyoung.dungeonsguide.stomp.StompSubscription;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
+import org.json.JSONObject;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+
+public class StaticResourceCache implements StompMessageHandler {
+ public static final StaticResourceCache INSTANCE = new StaticResourceCache();
+
+ private Map<UUID, StaticResource> staticResourceMap = new HashMap<>();
+
+ private Map<UUID, CompletableFuture<StaticResource>> staticResourceRequest = new HashMap<>();
+
+ public void purgeCache() {
+ staticResourceRequest.clear();
+ staticResourceMap.clear();
+ }
+
+ public CompletableFuture<StaticResource> getResource(UUID resourceID) {
+ if (staticResourceMap.containsKey(resourceID)) return CompletableFuture.completedFuture(staticResourceMap.get(resourceID));
+ if (staticResourceRequest.containsKey(resourceID)) return staticResourceRequest.get(resourceID);
+
+ DungeonsGuide.getDungeonsGuide().getStompConnection().send(new StompPayload().header("destination", "/app/staticresource.get").payload(resourceID.toString()));
+ CompletableFuture<StaticResource> comp = new CompletableFuture<>();
+ staticResourceRequest.put(resourceID, comp);
+ return comp;
+ }
+
+ @Override
+ public void handle(StompInterface stompInterface, StompPayload stompPayload) {
+ JSONObject object = new JSONObject(stompPayload.payload());
+ StaticResource staticResource = new StaticResource();
+ staticResource.setResourceID(UUID.fromString(object.getString("resourceID")));
+ staticResource.setExists(object.getBoolean("exists"));
+ staticResource.setValue((!object.has("value") || object.isNull("value")) ? null : object.getString("value"));
+
+ staticResourceMap.put(staticResource.getResourceID(), staticResource);
+ CompletableFuture<StaticResource> completed = staticResourceRequest.remove(staticResource.getResourceID());
+ if (completed != null) completed.complete(staticResource);
+ }
+
+ @SubscribeEvent
+ public void stompConnect(StompConnectedEvent stompConnectedEvent) {
+ stompConnectedEvent.getStompInterface().subscribe(StompSubscription.builder()
+ .stompMessageHandler(this).ackMode(StompSubscription.AckMode.AUTO).destination("/user/queue/staticresource.get").build());
+
+ getResource(BONUS_SCORE);
+ getResource(TRIVIA_ANSWERS);
+ }
+
+ public static final UUID BONUS_SCORE = UUID.fromString("13f10001-66b5-46e5-94f9-1a5161a23429");
+ public static final UUID TRIVIA_ANSWERS = UUID.fromString("5657f2cc-2bb8-4fcd-b55c-ffc0a35b9349");
+}