Skip to content

Commit

Permalink
OfficialAPI foundation & implement Nation.getCapital()
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 27, 2023
1 parent ec77158 commit 98dca2f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/emcw/entities/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;

/**
* <p>An generic base class that all other entities inherit from. Holds a name and a reference to its parent.</p>
* A generic base class that all other entities inherit from. Holds a name and a reference to its parent.
* @param <T> Determines the class type of the {@link #parent} reference.
*/
public abstract class BaseEntity<T> {
Expand Down
60 changes: 40 additions & 20 deletions src/main/java/io/github/emcw/entities/Nation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.github.emcw.exceptions.MissingEntryException;
import io.github.emcw.interfaces.IPlayerCollective;
import io.github.emcw.interfaces.ISerializable;
import io.github.emcw.map.Towns;
import io.github.emcw.utils.Funcs;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;

import static io.github.emcw.utils.Funcs.collectEntities;
import static io.github.emcw.utils.GsonUtil.*;

@SuppressWarnings("unused")
public class Nation extends BaseEntity<Nation> implements IPlayerCollective, ISerializable {
@Getter Capital capital;
Capital capital;
@Getter List<String> towns;
@Getter List<Resident> residents;
@Getter String leader;
@Getter Integer area;

// Not exposed to serialization.
private transient List<String> residentNames;
private final transient String mapName;

/**
* Creates a new Nation by parsing raw data.<br>
* <font color="#e38c1b">Should <b>NOT</b> be called explicitly unless you know what you are doing!</font>
* @param obj The unparsed data required to build this object.
*/
public Nation(JsonObject obj) {
public Nation(JsonObject obj, String mapName) {
super();

this.mapName = mapName;
init(obj);
}

Expand All @@ -46,25 +54,37 @@ private void init(JsonObject obj) {
residents = Resident.fromArr(residentArr, "name");
}

public Town getCapital() {
Towns towns = Funcs.mapByName(mapName).Towns;
try {
return towns.single(capital.getName());
} catch (MissingEntryException e) {
return new Town(capital);
}
}

// TODO: Finish invitableTowns
// public Map<String, Town> invitableTowns(String mapName) {
// Towns towns = Funcs.mapByName(mapName).Towns;
//
// return collectAsMap(streamEntries(towns.all()).map(entry -> {
// Town town = entry.getValue();
//
// Location townLoc = town.getLocation();
// Location capitalLoc = getCapital().getLocation();
//
// // In range, return the town
// if (Funcs.manhattan(capitalLoc, townLoc) < 2500) {
//
// }
//
// // Otherwise null
// return null;
// }));
// }
public Map<String, Town> invitableTowns(String mapName) {
Towns towns = Funcs.mapByName(mapName).Towns;

Stream<Entry<String, Town>> townsStream = streamEntries(towns.all());
Stream<Town> townsMap = townsStream.map(entry -> {
Town town = entry.getValue();

Location townLoc = town.getLocation();
Location capitalLoc = getCapital().getLocation();

// In range, return the town
if (Funcs.manhattan(capitalLoc, townLoc) < 2500) {
return town;
}

// Otherwise null
return null;
});

return collectEntities(townsMap);
}

/**
* Helper method to reduce mapping over {@link #residents} for names.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/github/emcw/entities/Town.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public Town(JsonObject obj) {
init(obj);
}

public Town(Capital capital) {
super();

setInfo(this, capital.getName());
location = capital.getLocation();
}

void init(JsonObject obj) {
setInfo(this, keyAsStr(obj, "name"));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/emcw/map/Nations.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void updateCache(Boolean force) {

// Parse map data into usable Nation objects.
DataParser.parseMapData(parent.getMapName(), true, true, false);
Cache<String, Nation> nations = DataParser.parsedNations();
Cache<String, Nation> nations = DataParser.parsedNations(parent.getMapName());

if (!nations.asMap().isEmpty())
setCache(nations);
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/io/github/emcw/utils/DataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ public static void parseMapData(String map, Boolean parseTowns, Boolean parseNat
processMapData(mapData, parseTowns, parseNations, parseResidents);
}

private static void processMapData(@NotNull JsonObject mapData,
Boolean parseTowns, Boolean parseNations, Boolean parseResidents) {
private static void processMapData(
@NotNull JsonObject mapData,
Boolean parseTowns,
Boolean parseNations,
Boolean parseResidents
) {
streamValues(mapData.asMap()).forEach(town -> {
JsonObject cur = town.getAsJsonObject();
//ProcessedTown processed = new ProcessedTown(cur);
Expand Down Expand Up @@ -129,9 +133,11 @@ private static void processMapData(@NotNull JsonObject mapData,
});
}

private static void parseTowns(String name, String nation, String mayor, String wiki,
JsonArray residents, int[] x, int[] z, int area, Boolean capital,
List<String> info, String fill, String outline) {
private static void parseTowns(
String name, String nation, String mayor, String wiki,
JsonArray residents, int[] x, int[] z, int area, Boolean capital,
List<String> info, String fill, String outline
) {
rawTowns.asMap().computeIfAbsent(name, k -> {
JsonObject obj = new JsonObject();

Expand Down Expand Up @@ -161,8 +167,10 @@ private static void parseTowns(String name, String nation, String mayor, String
});
}

private static void parseNations(String nation, String town, JsonArray residents,
String mayor, int area, int[] x, int[] z, boolean capital) {
private static void parseNations(
String nation, String town, JsonArray residents,
String mayor, int area, int[] x, int[] z, boolean capital
) {
// Not present, create a new Nation.
rawNations.asMap().computeIfAbsent(nation, k -> {
JsonObject obj = new JsonObject();
Expand Down Expand Up @@ -244,17 +252,26 @@ public static void parsePlayerData(String map) {
}

public static Cache<String, Town> parsedTowns() {
streamEntries(rawTowns.asMap()).forEach(entry -> towns.put(entry.getKey(), new Town(valueAsObj(entry))));
streamEntries(rawTowns.asMap()).forEach(entry ->
towns.put(entry.getKey(), new Town(valueAsObj(entry)))
);

return towns;
}

public static Cache<String, Nation> parsedNations() {
streamEntries(rawNations.asMap()).forEach(entry -> nations.put(entry.getKey(), new Nation(valueAsObj(entry))));
public static Cache<String, Nation> parsedNations(String map) {
streamEntries(rawNations.asMap()).forEach(entry ->
nations.put(entry.getKey(), new Nation(valueAsObj(entry), map))
);

return nations;
}

public static Cache<String, Resident> parsedResidents() {
streamEntries(rawResidents.asMap()).forEach(entry -> residents.put(entry.getKey(), new Resident(valueAsObj(entry))));
streamEntries(rawResidents.asMap()).forEach(entry ->
residents.put(entry.getKey(), new Resident(valueAsObj(entry)))
);

return residents;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/emcw/utils/Funcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static <T> Stream<T> streamList(@NotNull List<T> list) {
}

public static EMCMap mapByName(@NotNull String name) {
var wrapper = EMCWrapper.instance();
EMCWrapper wrapper = EMCWrapper.instance();
return name.equals("nova") ? wrapper.getNova() : wrapper.getAurora();
}

Expand Down
47 changes: 46 additions & 1 deletion src/main/java/io/github/emcw/utils/http/OfficialAPI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
package io.github.emcw.utils.http;

import com.google.gson.JsonObject;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

public class OfficialAPI {
public OfficialAPI() { }

static String Domain = "https://api.earthmc.net/v2/aurora/";
static String OAPI_DOMAIN = "https://api.earthmc.net/v2/aurora/";

@Contract("_, -> new")
private static @NotNull CompletableFuture<JsonObject> get(String url) {
return CompletableFuture.supplyAsync(() -> {
try {
return Request.send(url);
}
catch (Exception e) {
System.out.println("Exception occurred!\n" + e.getMessage());
return null;
}
});
}

public JsonObject townyData() {
return townyData("");
}

public JsonObject townyData(@NotNull String endpoint) {
if (endpoint.startsWith("/")) {
endpoint = endpoint.substring(1);
}

// Get as JSON
String fullUrl = OAPI_DOMAIN + endpoint;
CompletableFuture<JsonObject> data = get(fullUrl);

try {
return data.join().getAsJsonObject();
}
catch (Exception e) {
System.out.println(
"Error fetching OAPI data from " + fullUrl +
"\nReceived response may be incorrectly formatted."
);
}

return new JsonObject();
}
}

0 comments on commit 98dca2f

Please sign in to comment.