From 3ed87263e357bc1527393c7a7cefd04e92245603 Mon Sep 17 00:00:00 2001 From: Afalinko Date: Sat, 16 Sep 2017 23:04:47 -0400 Subject: [PATCH] Tradehubs distance extracted from bookmarks in clipboard --- .../java/com/eve/copilot/Application.java | 46 +++++++++++++++++-- src/main/java/com/eve/copilot/Tradehub.java | 14 ++++++ src/main/resources/trade_hubs.json | 5 ++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/eve/copilot/Tradehub.java create mode 100644 src/main/resources/trade_hubs.json diff --git a/src/main/java/com/eve/copilot/Application.java b/src/main/java/com/eve/copilot/Application.java index 37b760d..4850223 100644 --- a/src/main/java/com/eve/copilot/Application.java +++ b/src/main/java/com/eve/copilot/Application.java @@ -8,9 +8,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; +import java.util.*; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; public class Application { @@ -39,6 +40,16 @@ private static Map loadSigs() { return Arrays.stream(sigDb).collect(Collectors.toMap(o -> o.sig, o -> o.val)); } + private static Set loadHubs() { + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + InputStream is = classloader.getResourceAsStream("trade_hubs.json"); + + Gson g = new Gson(); + Tradehub[] hubDb = g.fromJson(new BufferedReader(new InputStreamReader(is)), Tradehub[].class); + System.out.println("Hubs loaded: " + hubDb.length); + return Arrays.stream(hubDb).map(Tradehub::getHubname).collect(Collectors.toSet()); + } + private static String expandSig(String scanLine, Map sigDB) { if (!scanLine.matches("[A-Z]{3}-C\\?-[A-Za-z][0-9]+")) { return null; @@ -56,6 +67,7 @@ private static String expandSig(String scanLine, Map sigDB) { public static void main(String args[]) throws InterruptedException { String data = ""; Map sigDB = loadSigs(); + Set hubDB = loadHubs(); while (true) { String clipboardContents = getClipboardContents(); if (!data.equalsIgnoreCase(clipboardContents)) { @@ -74,6 +86,11 @@ public static void main(String args[]) throws InterruptedException { Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); System.out.println("set: " + sigExpansion); } + } else if (data.contains("Jita")) { + final String hubExpansion = expandHub(data, hubDB); + StringSelection selection = new StringSelection(hubExpansion); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); + System.out.println("set: " + hubExpansion); } } @@ -81,6 +98,29 @@ public static void main(String args[]) throws InterruptedException { } } + private static String expandHub(String data, Set hubDB) { + if (!data.contains("Coordinate")) { + return null; + } + + final Map distance = new LinkedHashMap<>(); + final String[] data_parts = data.split("\n"); + for (String part : data_parts) { + for (String hub : hubDB) { + if (part.toLowerCase().contains(hub)){ + final Matcher hubMatcher = Pattern.compile("Coordinate\t([0-9]+)").matcher(part); + if (hubMatcher.find()){ + distance.put(hub, Integer.valueOf(hubMatcher.group(1))); + } + } + } + } + return distance.entrySet().stream() + .sorted(Comparator.comparing(Map.Entry::getValue)) + .map(o -> o.getKey()+": "+o.getValue()+"\n") + .collect(Collectors.joining()); + } + private static String getClipboardContents() { String result = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); diff --git a/src/main/java/com/eve/copilot/Tradehub.java b/src/main/java/com/eve/copilot/Tradehub.java new file mode 100644 index 0000000..43ab37b --- /dev/null +++ b/src/main/java/com/eve/copilot/Tradehub.java @@ -0,0 +1,14 @@ +package com.eve.copilot; + +public class Tradehub { + + private String hubname; + + public String getHubname() { + return hubname; + } + + public void setHubname(String hubname) { + this.hubname = hubname; + } +} diff --git a/src/main/resources/trade_hubs.json b/src/main/resources/trade_hubs.json new file mode 100644 index 0000000..6b333cd --- /dev/null +++ b/src/main/resources/trade_hubs.json @@ -0,0 +1,5 @@ +[{"hubname":"jita"}, + {"hubname":"amarr"}, + {"hubname":"rens"}, + {"hubname":"dodixie"}, + {"hubname":"hek"}]