diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 751b1a5..91ed465 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 tw.kane osu4j - v1.1.5 + v1.2 diff --git a/pom.xml b/pom.xml index ba7a4ab..2ce3c34 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tw.kane osu4j - v1.1.5 + v1.2 diff --git a/src/main/java/tw/kane/osu4j/Cache.java b/src/main/java/tw/kane/osu4j/Cache.java new file mode 100644 index 0000000..8a86df6 --- /dev/null +++ b/src/main/java/tw/kane/osu4j/Cache.java @@ -0,0 +1,8 @@ +package tw.kane.osu4j; + +import java.util.HashMap; + +public class Cache { + public static HashMap userCache = new HashMap<>(); + public static HashMap beatmapCache = new HashMap<>(); +} diff --git a/src/main/java/tw/kane/osu4j/OsuClient.java b/src/main/java/tw/kane/osu4j/OsuClient.java index 2d50fd7..3169151 100644 --- a/src/main/java/tw/kane/osu4j/OsuClient.java +++ b/src/main/java/tw/kane/osu4j/OsuClient.java @@ -10,15 +10,17 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; public class OsuClient { private final String token; private final OkHttpClient httpClient = new OkHttpClient(); + static OsuClient client; + public OsuClient(String token) { this.token = token; + client = this; } /** @@ -28,7 +30,10 @@ public OsuClient(String token) { * @throws InvalidTokenException if token provided to OsuClient wrong * @throws NotFoundException if user not found */ - public User getUser(String id) throws IOException, InvalidTokenException, NotFoundException { + public User getUser(String id, boolean allowCached) throws IOException, InvalidTokenException, NotFoundException { + if(allowCached && Cache.userCache.containsKey(id)) + return Cache.userCache.get(id); + Request request = new Request.Builder() .url(String.format("https://osu.ppy.sh/api/get_user?k=%s&u=%s", token, @@ -43,10 +48,12 @@ public User getUser(String id) throws IOException, InvalidTokenException, NotFou throw new InvalidTokenException(result.getString("error")); } else { - JSONArray user = new JSONArray(responseString); - if(user.length() == 0) + JSONArray userArray = new JSONArray(responseString); + if(userArray.length() == 0) throw new NotFoundException(); - return new User((JSONObject) user.get(0)); + User user = new User(userArray.getJSONObject(0)); + Cache.userCache.put(id, user); + return user; } return null; } diff --git a/src/main/java/tw/kane/osu4j/Score.java b/src/main/java/tw/kane/osu4j/Score.java index 8b19178..d13d56d 100644 --- a/src/main/java/tw/kane/osu4j/Score.java +++ b/src/main/java/tw/kane/osu4j/Score.java @@ -124,7 +124,7 @@ public enum Rank { D("D"), F("F"); - public final String name; + private final String name; Rank(String name) { this.name = name; diff --git a/src/main/java/tw/kane/osu4j/User.java b/src/main/java/tw/kane/osu4j/User.java index b9001eb..845815e 100644 --- a/src/main/java/tw/kane/osu4j/User.java +++ b/src/main/java/tw/kane/osu4j/User.java @@ -1,7 +1,10 @@ package tw.kane.osu4j; import org.json.JSONObject; +import tw.kane.osu4j.Exception.InvalidTokenException; +import tw.kane.osu4j.Exception.NotFoundException; +import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -37,6 +40,18 @@ public User(JSONObject object) { pp = new PerformancePoint(); } + public Score[] getBest() throws NotFoundException, InvalidTokenException, IOException { + if(OsuClient.client == null) + throw new InvalidTokenException("client not init first"); + return OsuClient.client.getUserBest(id); + } + + public Score[] getRecent() throws NotFoundException, InvalidTokenException, IOException { + if(OsuClient.client == null) + throw new InvalidTokenException("client not init first"); + return OsuClient.client.getUserRecent(id); + } + private class Count { long _50, _100, _300, SSH, SS, SH, S, A, plays, secondPlayed;