Skip to content

Commit

Permalink
Speedrun.com DR API
Browse files Browse the repository at this point in the history
  • Loading branch information
roccodev committed Oct 31, 2018
1 parent e3b1144 commit 7d0bd63
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import pw.roccodev.beezig.hiveapi.wrapper.monthly.maxthat.dr.DrMonthlyProfile;
import pw.roccodev.beezig.hiveapi.wrapper.player.PvPStats;
import pw.roccodev.beezig.hiveapi.wrapper.player.Titleable;
import pw.roccodev.beezig.hiveapi.wrapper.speedrun.WorldRecord;
import pw.roccodev.beezig.hiveapi.wrapper.utils.download.UrlBuilder;
import pw.roccodev.beezig.hiveapi.wrapper.utils.json.LazyObject;

import java.util.Map;

public class DrStats extends PvPStats implements MonthliesReady, Titleable {

private LazyObject source;
Expand All @@ -22,6 +25,10 @@ public DrStats(String username, boolean convertToUUID) {
source = getSource();
}

public static WorldRecord getWorldRecord(String mapSpeedrunId) {
return new WorldRecord(new LazyObject(null, new UrlBuilder().speedrun().level(mapSpeedrunId).build()));
}

public long getTrapsActivated() {
return source.getLong("trapsactivated");
}
Expand Down Expand Up @@ -50,6 +57,26 @@ public Visibility getSelectedVisibility() {
return Visibility.valueOf(source.getString("visibility"));
}

public Map<String, Long> getMapRecords() {
return source.getJSONObject("maprecords");
}

public Map<String, Long> getMapKills() {
return source.getJSONObject("mapkills");
}

public Map<String, Long> getMapDeaths() {
return source.getJSONObject("mapdeaths");
}

public Map<String, Long> getTrapClassKills() {
return source.getJSONObject("trapclasskills");
}

public Map<String, Long> getTrapClassDeaths() {
return source.getJSONObject("trapclassdeaths");
}

@Override
public DrMonthlyProfile getMonthlyProfile() {
return getMonthlyProfile(getUUID());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pw.roccodev.beezig.hiveapi.wrapper.speedrun;

import pw.roccodev.beezig.hiveapi.wrapper.utils.download.UrlBuilder;
import pw.roccodev.beezig.hiveapi.wrapper.utils.json.JObject;
import pw.roccodev.beezig.hiveapi.wrapper.utils.json.LazyObject;

public class WorldRecord {

private LazyObject wrData;
private JObject run;

private LazyObject holderData;

public WorldRecord(LazyObject wrData) {
this.wrData = wrData;
run = wrData.getJObject("data").getJArray("runs").getJObject(0).getJObject("run");
}

public double getTime() {
return run.getJObject("times").getDouble("primary_t");
}

public String getHolderId() {
return run.getJArray("players").getJObject(0).getString("id");
}

public String getHolderName() {
if(holderData == null) {
holderData = new LazyObject(null, new UrlBuilder().speedrun().user(getHolderId()).build());
}
return holderData.getJObject("data").getJObject("names").getString("international");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pw.roccodev.beezig.hiveapi.wrapper.utils.download;

public class SpeedrunUrlBuilder extends UrlBuilder {

SpeedrunUrlBuilder(UrlBuilder parent) {
builder = parent.builder.append(URLs.SPEEDRUN_BASE);
}

public SpeedrunUrlBuilder level(String id) {
builder.append("leaderboards/369ep8dl/level/").append(id).append("/824xzvmd?top=1");
return this;
}

public SpeedrunUrlBuilder user(String id) {
builder.append("users/").append(id);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class URLs {

static final String HIVE_BASE = "api.hivemc.com/v1/";
static final String SPEEDRUN_BASE = "www.speedrun.com/api/v1/";

static final String PLAYER_ENDPOINT = "player/";
static final String GAME_ENDPOINT = "game/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public UrlBuilder mojang(String username) {
return this;
}

public SpeedrunUrlBuilder speedrun() {
return new SpeedrunUrlBuilder(this);
}

public URL build() {
try {
return new URL(builder.toString().trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public boolean getBoolean(int index) {
return (boolean) jsonInput.get(index);
}

public double getDouble(int index) {
return (double) jsonInput.get(index);
}

public JSONArray getInput() {
return jsonInput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public boolean getBoolean(String key) {
return (boolean) jsonInput.get(key);
}

public double getDouble(String key) {
return (double) jsonInput.get(key);
}

public JSONObject getInput() {
return jsonInput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public boolean getBoolean(int index) {
return super.getBoolean(index);
}

@Override
public double getDouble(int index) {
checkIfSourceExists();
return super.getDouble(index);
}

public void fetch() {
checkIfSourceExists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public boolean getBoolean(String key) {
return super.getBoolean(key);
}

@Override
public double getDouble(String key) {
checkIfSourceExists();
return super.getDouble(key);
}

public void fetch() {
checkIfSourceExists();
}
Expand Down

0 comments on commit 7d0bd63

Please sign in to comment.