Skip to content

Commit

Permalink
Adds /api/v1/readonly/tracks/:trackname/withusernames
Browse files Browse the repository at this point in the history
For api consumers that need player usernames

Closes #11
  • Loading branch information
JustBru00 committed Jun 22, 2023
1 parent d02eef5 commit de2e6b3
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/com/frosthex/timingsystem/restapi/network/SparkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,62 @@ public static void initSpark() {
return responseObject.toString();
});

// /api/v1/readonly/tracks/:trackname/withusernames
get("/api/v1/readonly/tracks/:trackname/withusernames", (request, response) -> {
String trackInternalName = request.params("trackname");

if (trackInternalName == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. The track name provided is null.\"}");
}

Optional<Track> optionalTrack = TimingSystemAPI.getTrack(trackInternalName);
if (optionalTrack.isEmpty()) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. Could find a track with that name.\"}");
}

Track track = optionalTrack.get();

JsonObject responseObject = new JsonObject();
responseObject.addProperty("command_name", track.getCommandName());
responseObject.addProperty("display_name", track.getDisplayName());
responseObject.addProperty("mode", track.getModeAsString());
responseObject.addProperty("type", track.getTypeAsString());
responseObject.addProperty("open", track.isOpen());
responseObject.addProperty("date_created", track.getDateCreated());
responseObject.addProperty("id", track.getId());
responseObject.addProperty("total_attempts", track.getTotalAttempts());
responseObject.addProperty("total_finishes", track.getTotalFinishes());
responseObject.addProperty("total_time_spent", track.getTotalTimeSpent());
responseObject.addProperty("weight", track.getWeight());
responseObject.addProperty("gui_item", track.getGuiItem().toString());
JsonArray optionsArray = new JsonArray();
for (char c : track.getOptions()) {
optionsArray.add(c);
}
responseObject.add("options", optionsArray);
responseObject.addProperty("owner", track.getOwner().getUniqueId().toString());
responseObject.add("spawn_location", serializeLocation(track.getSpawnLocation()));
JsonArray tagsArray = new JsonArray();
for (TrackTag trackTag : track.getTags()) {
tagsArray.add(trackTag.getValue());
}
responseObject.add("tags", tagsArray);
JsonArray topListArray = new JsonArray();
for (TimeTrialFinish finish : track.getTopList()) {
JsonObject timeTrialFinishObject = new JsonObject();
timeTrialFinishObject.addProperty("date", finish.getDate());
timeTrialFinishObject.addProperty("id", finish.getId());
timeTrialFinishObject.addProperty("time", finish.getTime());
timeTrialFinishObject.addProperty("player_uuid", finish.getPlayer().getUniqueId().toString());
timeTrialFinishObject.addProperty("username", finish.getPlayer().getName());
topListArray.add(timeTrialFinishObject);
}
responseObject.add("top_list", topListArray);

response.status(200);
return responseObject.toString();
});

// /api/v1/readonly/players/:uuid OR /api/v1/readonly/players/:username
get("/api/v1/readonly/players/:uuidorusername", (request, response) -> {
String uuidOrUsernameString = request.params("uuidorusername");
Expand Down

0 comments on commit de2e6b3

Please sign in to comment.