Skip to content

Instantly share code, notes, and snippets.

@exception
Created November 21, 2017 19:35
Show Gist options
  • Save exception/9afc522ace23328905e94928268dbfd2 to your computer and use it in GitHub Desktop.
Save exception/9afc522ace23328905e94928268dbfd2 to your computer and use it in GitHub Desktop.
package io.erosemberg.conquest.store;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @author Erik Rosemberg
* @since 21/11/2017
*/
public class BuycraftProcessor {
private Map<Integer, PackageExecutor> internalExecutors = new LinkedHashMap<>();
private static final String COMMANDS_ACTION = "commands";
private static final String DELETE_ACTION = "delete";
private final String API_KEY;
private final long timeOut;
private final JsonParser parser;
private BuycraftProcessor(String API_KEY, long timeOut) {
this.API_KEY = API_KEY;
this.timeOut = timeOut;
this.parser = new JsonParser();
System.out.println("Starting BuyCraft processor, will fetch data every " + (timeOut / 1000) + " seconds.");
new Thread(() -> {
while (!Thread.interrupted()) {
System.out.println("Fetching BuyCraft data...");
processCommands(fetchCommands());
try {
Thread.sleep(timeOut);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "Buycraft Thread").start();
}
public static BuycraftProcessor grab(String apiKey, TimeUnit unit, long value) {
return new BuycraftProcessor(apiKey, unit.toMillis(value));
}
public BuycraftProcessor withExecutor(int packageId, PackageExecutor executor) {
internalExecutors.putIfAbsent(packageId, executor);
return this;
}
private JsonObject fetchCommands() {
try {
HttpResponse<JsonNode> response = Unirest.get(getUrl(COMMANDS_ACTION)).asJson();
InputStream stream = response.getRawBody();
InputStreamReader reader = new InputStreamReader(stream);
JsonElement element = parser.parse(reader);
return element.getAsJsonObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
private void processCommands(JsonObject object) {
int code = object.get("code").getAsInt();
if (code != 0) {
throw new RuntimeException("code was " + code);
}
JsonObject payload = object.get("payload").getAsJsonObject();
JsonArray elements = payload.get("commands").getAsJsonArray();
if (elements.size() > 0) {
System.out.println("Found " + elements.size() + " due commands. Executing now.");
elements.forEach(element -> {
JsonObject command = element.getAsJsonObject();
int id = command.get("id").getAsInt();
UUID uuid = fromNoDash(command.get("uuid").getAsString());
String[] c = command.get("command").getAsString().split(" ");
int packageId = Integer.valueOf(c[c.length - 1]);
PackageExecutor executor = internalExecutors.getOrDefault(packageId, null);
if (executor == null) {
throw new RuntimeException("no package executor for " + packageId);
}
try {
executor.handle(uuid, command);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Unirest.get(getUrl(DELETE_ACTION) + "[" + id + "]").asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
}
});
} else {
System.out.println("No due commands found...");
}
}
private UUID fromNoDash(String noDash) {
BigInteger bi1 = new BigInteger(noDash.substring(0, 16), 16);
BigInteger bi2 = new BigInteger(noDash.substring(16, 32), 16);
return new UUID(bi1.longValue(), bi2.longValue());
}
private String getUrl(String command) {
switch (command.toLowerCase()) {
case "commands":
return "http://api.buycraft.net/v4?playersOnline=0&offlineCommands=true&action=commands&offlineCommandLimit=150&do=lookup&secret=" + API_KEY + "&users=[]";
case "delete":
return "http://api.buycraft.net/v4?playersOnline=0&action=commands&secret=" + API_KEY + "&do=removeId&commands=";
default:
return null;
}
}
}
package io.erosemberg.conquest.store;
import com.google.gson.JsonObject;
import java.util.UUID;
/**
* @author Erik Rosemberg
* @since 21/11/2017
*/
public interface PackageExecutor {
void handle(UUID uuid, JsonObject payload);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment