From 21abe2f9ea26f17f6d332ca7bf3f4b0f044aa77a Mon Sep 17 00:00:00 2001 From: syeyoung Date: Thu, 24 Dec 2020 16:41:27 +0900 Subject: TOOLTIPS --- .../kr/syeyoung/dungeonsguide/utils/AhUtils.java | 131 +++++++++++++++++++++ .../kr/syeyoung/dungeonsguide/utils/TextUtils.java | 29 +++++ 2 files changed, 160 insertions(+) create mode 100644 src/main/java/kr/syeyoung/dungeonsguide/utils/AhUtils.java (limited to 'src/main/java/kr/syeyoung/dungeonsguide/utils') diff --git a/src/main/java/kr/syeyoung/dungeonsguide/utils/AhUtils.java b/src/main/java/kr/syeyoung/dungeonsguide/utils/AhUtils.java new file mode 100644 index 00000000..1f7644bc --- /dev/null +++ b/src/main/java/kr/syeyoung/dungeonsguide/utils/AhUtils.java @@ -0,0 +1,131 @@ +package kr.syeyoung.dungeonsguide.utils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.Timer; +import java.util.TimerTask; +import java.util.TreeSet; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; + +public class AhUtils { + public static volatile Map auctions = new HashMap(); + + private static Map semi_auctions = new HashMap(); + + public static Timer timer = new Timer(); + + public static int totalAuctions = 0; + + public static void registerTimer() { + timer.schedule(new TimerTask() { + public void run() { + AhUtils.loadAuctions(); + } + }, 0L, 1800000L); + } + + public static void loadAuctions() { + try { + int i = 0; + do { + + } while (loadPage(i++)); + loadBazaar(); + auctions = semi_auctions; + semi_auctions = new HashMap(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void loadBazaar() throws IOException { + System.out.println("Fetching bazaar data"); + URL url = new URL("https://api.hypixel.net/skyblock/bazaar"); + InputStreamReader reader = new InputStreamReader(url.openStream()); + JsonObject object = (JsonObject)(new JsonParser()).parse(reader); + boolean success = object.get("success").getAsBoolean(); + if (!success) + return; + JsonObject element = object.getAsJsonObject("products"); + for (Map.Entry product : (Iterable>)element.entrySet()) { + String id = product.getKey(); + AuctionData auctionData = semi_auctions.get(id); + boolean notexisted = (auctionData == null); + if (notexisted) + auctionData = new AuctionData(id); + auctionData.sellPrice = ((JsonElement)product.getValue()).getAsJsonObject().getAsJsonObject("quick_status").get("sellPrice").getAsInt(); + auctionData.buyPrice = ((JsonElement)product.getValue()).getAsJsonObject().getAsJsonObject("quick_status").get("buyPrice").getAsInt(); + if (notexisted) + semi_auctions.put(id, auctionData); + } + } + + public static boolean loadPage(int page) throws IOException { + System.out.println("Fetching page " + page + " of auctions"); + URL url = new URL("https://api.hypixel.net/skyblock/auctions?page=" + page); + InputStreamReader reader = new InputStreamReader(url.openStream()); + JsonObject object = (JsonObject)(new JsonParser()).parse(reader); + boolean success = object.get("success").getAsBoolean(); + if (!success) + return false; + int maxPage = object.get("totalPages").getAsInt(); + int totalAuctions = object.get("totalAuctions").getAsInt(); + System.out.println("Fetched page " + page + "/" + maxPage + " of auctions! (" + totalAuctions + " total)"); + JsonArray array = object.get("auctions").getAsJsonArray(); + for (JsonElement element2 : array) { + JsonObject element = element2.getAsJsonObject(); + JsonElement isBin = element.get("bin"); + if (isBin == null || !isBin.getAsBoolean()) + continue; + byte[] itemData = Base64.decode(element.get("item_bytes").getAsString().replace("\\u003d", "=")); + NBTTagCompound nbtTagCompound = CompressedStreamTools.readCompressed(new ByteArrayInputStream(itemData)); + NBTTagCompound acutalItem = (NBTTagCompound)nbtTagCompound.getTagList("i", 10).get(0); + NBTTagCompound attributes = acutalItem.getCompoundTag("tag").getCompoundTag("ExtraAttributes"); + String id = attributes.getString("id"); + if (id.equals("ENCHANTED_BOOK")) { + NBTTagCompound enchants = attributes.getCompoundTag("enchantments"); + Set keys = enchants.getKeySet(); + if (keys.size() != 1) + continue; + String ench = keys.iterator().next(); + int lv = enchants.getInteger(ench); + id = id + "::" + ench + "-" + lv; + } + AuctionData auctionData = semi_auctions.get(id); + boolean notexisted = (auctionData == null); + if (notexisted) + auctionData = new AuctionData(id); + auctionData.prices.add(element.get("starting_bid").getAsInt()); + if (notexisted) + semi_auctions.put(id, auctionData); + } + return (page < maxPage); + } + + public static class AuctionData { + public String id; + + public SortedSet prices; + + public int sellPrice = -1; + + public int buyPrice = -1; + + public AuctionData(String id) { + this.id = id; + this.prices = new TreeSet(); + } + } +} \ No newline at end of file diff --git a/src/main/java/kr/syeyoung/dungeonsguide/utils/TextUtils.java b/src/main/java/kr/syeyoung/dungeonsguide/utils/TextUtils.java index d56584c3..300cf6ba 100755 --- a/src/main/java/kr/syeyoung/dungeonsguide/utils/TextUtils.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/utils/TextUtils.java @@ -2,6 +2,8 @@ package kr.syeyoung.dungeonsguide.utils; import java.text.DecimalFormat; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.regex.Pattern; public class TextUtils { @@ -33,4 +35,31 @@ public class TextUtils { return stringBuilder.toString(); } + + + private static final TreeMap suffixes = new TreeMap(); + + static { + suffixes.put(1000L, "k"); + suffixes.put(1000000L, "m"); + suffixes.put(1000000000L, "b"); + } + + public static String format(long value) { +// return String.valueOf(value); + + if (value == Long.MIN_VALUE) + return format(-9223372036854775807L); + if (value < 0L) + return "-" + format(-value); + if (value < 1000L) + return Long.toString(value); + Map.Entry e = suffixes.floorEntry(value); + Long divideBy = e.getKey(); + String suffix = e.getValue(); + long truncated = value * 10 / divideBy ; + boolean hasDecimal = (truncated < 100L && (truncated / 10.0D) != (truncated / 10L)); + return hasDecimal ? ((truncated / 10.0D) + suffix) : ((truncated / 10L) + suffix); + } + } \ No newline at end of file -- cgit