|
| 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 | +} |
0 commit comments