Skip to content

Commit

Permalink
Tradehubs distance extracted from bookmarks in clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Afalinko committed Sep 17, 2017
1 parent eef4dba commit 3ed8726
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/main/java/com/eve/copilot/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -39,6 +40,16 @@ private static Map<String, String> loadSigs() {
return Arrays.stream(sigDb).collect(Collectors.toMap(o -> o.sig, o -> o.val));
}

private static Set<String> 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<String, String> sigDB) {
if (!scanLine.matches("[A-Z]{3}-C\\?-[A-Za-z][0-9]+")) {
return null;
Expand All @@ -56,6 +67,7 @@ private static String expandSig(String scanLine, Map<String, String> sigDB) {
public static void main(String args[]) throws InterruptedException {
String data = "";
Map<String, String> sigDB = loadSigs();
Set<String> hubDB = loadHubs();
while (true) {
String clipboardContents = getClipboardContents();
if (!data.equalsIgnoreCase(clipboardContents)) {
Expand All @@ -74,13 +86,41 @@ 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);
}

}
Thread.sleep(1000);
}
}

private static String expandHub(String data, Set<String> hubDB) {
if (!data.contains("Coordinate")) {
return null;
}

final Map<String, Integer> 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();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/eve/copilot/Tradehub.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/trade_hubs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{"hubname":"jita"},
{"hubname":"amarr"},
{"hubname":"rens"},
{"hubname":"dodixie"},
{"hubname":"hek"}]

0 comments on commit 3ed8726

Please sign in to comment.