Skip to content

Commit 26e3448

Browse files
committed
4. Add Pursuits endpoint
1 parent 796017d commit 26e3448

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package net.earthmc.emcapi.endpoint;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import io.javalin.http.BadRequestResponse;
6+
import io.javalin.http.NotFoundResponse;
7+
import net.earthmc.emcapi.EMCAPI;
8+
import net.earthmc.emcapi.integration.Integrations;
9+
import net.earthmc.emcapi.integration.PursuitsIntegration;
10+
import net.earthmc.emcapi.manager.KeyManager;
11+
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
12+
import net.earthmc.emcapi.util.CooldownUtil;
13+
import net.earthmc.emcapi.util.HttpExceptions;
14+
import net.earthmc.emcapi.util.JSONUtil;
15+
import net.earthmc.lynchpin.api.pursuits.Pursuit;
16+
import org.jspecify.annotations.Nullable;
17+
18+
import java.util.List;
19+
import java.util.UUID;
20+
import java.util.ArrayList;
21+
import java.util.Map;
22+
23+
public class PursuitsEndpoint extends PostEndpoint<PursuitsEndpoint.PursuitsLeaderboard> {
24+
private static final long COOLDOWN_SECONDS = 60;
25+
private final PursuitsIntegration integration;
26+
27+
public PursuitsEndpoint(EMCAPI plugin) {
28+
super(plugin);
29+
integration = Integrations.getIntegration("lynchpin-pursuits");
30+
}
31+
32+
@Override
33+
public PursuitsLeaderboard getObjectOrNull(JsonElement element, @Nullable String key) {
34+
String string = JSONUtil.getJsonElementAsStringOrNull(element);
35+
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");
36+
37+
UUID keyOwner = KeyManager.getKeyOwner(key);
38+
if (keyOwner == null) {
39+
throw HttpExceptions.MISSING_API_KEY;
40+
}
41+
42+
Pursuit.Type specifiedType;
43+
if (string.equalsIgnoreCase("all")) {
44+
specifiedType = null;
45+
} else {
46+
try {
47+
specifiedType = Pursuit.Type.valueOf(string.toUpperCase());
48+
} catch (IllegalArgumentException ignored) {
49+
throw new BadRequestResponse("Invalid Pursuit Type specified. ");
50+
}
51+
}
52+
53+
CooldownUtil.checkAndAddCooldownOrThrow("pursuits", keyOwner.toString(), COOLDOWN_SECONDS);
54+
if (specifiedType != null) {
55+
Pursuit pursuit = integration.getPursuit(specifiedType);
56+
if (pursuit == null) {
57+
throw new NotFoundResponse("No pursuit found for type " + specifiedType.name());
58+
}
59+
return new PursuitsLeaderboard(List.of(pursuit));
60+
}
61+
62+
List<Pursuit> pursuits = new ArrayList<>(integration.getPursuits().values());
63+
if (pursuits.isEmpty()) {
64+
throw new NotFoundResponse("No pursuits found");
65+
}
66+
return new PursuitsLeaderboard(pursuits);
67+
}
68+
69+
@Override
70+
public JsonElement getJsonElement(PursuitsLeaderboard object, @Nullable String key) {
71+
JsonObject json = new JsonObject();
72+
for (Pursuit pursuit : object.pursuits) {
73+
json.add(pursuit.type().name(), formatPursuit(pursuit));
74+
}
75+
76+
return json;
77+
}
78+
79+
private JsonObject formatPursuit(Pursuit pursuit) {
80+
JsonObject json = new JsonObject();
81+
82+
json.addProperty("name", pursuit.name());
83+
json.addProperty("isActive", pursuit.isActive());
84+
85+
json.add("top", formatTop(pursuit));
86+
return json;
87+
}
88+
89+
private JsonObject formatTop(Pursuit pursuit) {
90+
JsonObject top = new JsonObject();
91+
int index = 1;
92+
String typeName = pursuit.type().name().toLowerCase();
93+
for (Map.Entry<UUID, Double> entry : pursuit.top(10).entrySet()) {
94+
JsonObject element = new JsonObject();
95+
element.addProperty(typeName, entry.getKey().toString());
96+
element.addProperty("score", entry.getValue());
97+
98+
top.add(String.valueOf(index++), element);
99+
}
100+
return top;
101+
}
102+
103+
public record PursuitsLeaderboard(List<Pursuit> pursuits) {}
104+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.earthmc.emcapi.integration;
2+
3+
import net.earthmc.lynchpin.api.pursuits.Pursuit;
4+
import net.earthmc.lynchpin.api.pursuits.PursuitsProvider;
5+
6+
import java.util.Map;
7+
8+
public class PursuitsIntegration extends Integration {
9+
private PursuitsProvider pursuits;
10+
11+
public PursuitsIntegration() {
12+
super("Lynchpin");
13+
try {
14+
pursuits = PursuitsProvider.instance();
15+
} catch (Exception e) {
16+
plugin.getLogger().warning("Not loading pursuits integration due to the module not being present/enabled");
17+
}
18+
}
19+
20+
@Override
21+
public boolean isEnabled() {
22+
return super.isEnabled() && pursuits != null && pursuits.isEnabled();
23+
}
24+
25+
@Override
26+
public void register() {
27+
Integrations.addIntegration("lynchpin-pursuits", this);
28+
}
29+
30+
public Pursuit getPursuit(Pursuit.Type type) {
31+
return pursuits.getPursuit(type);
32+
}
33+
34+
public Map<Pursuit.Type, Pursuit> getPursuits() {
35+
return pursuits.getPursuits();
36+
}
37+
}

0 commit comments

Comments
 (0)