diff --git a/README.md b/README.md index de96a5e..9cc7753 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # RuneAPI.java -Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape.com/) API. +Simple open source Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape.com/) API. ## Class Data @@ -14,14 +14,24 @@ Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape ### Get **Stats** - Retrieve the stats stored about the provided player. + +**#1** - Loop all skills and print their data. ```java -List stats = RuneAPI.getStats(username); +Map stats = RuneAPI.getStats(username); -for(Skill skill: stats) { +for(Skill skill: stats.values()) { System.out.printf("%s - %,d - %,d - %,d \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience()); } ``` +**#2** - Find specific skill and print its data. +```java +Map stats = RuneAPI.getStats(username); +Skill overall = stats.get("Overall"); + +System.out.printf("%s - %,d - %,d - %,d \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience()); +``` + ## Download [![](https://jitpack.io/v/BasketBandit/RuneAPI.java.svg)](https://jitpack.io/#BasketBandit/RuneAPI.java) diff --git a/src/main/java/com/baseketbandit/runeapi/RuneAPI.java b/src/main/java/com/baseketbandit/runeapi/RuneAPI.java index 479b022..dc16074 100644 --- a/src/main/java/com/baseketbandit/runeapi/RuneAPI.java +++ b/src/main/java/com/baseketbandit/runeapi/RuneAPI.java @@ -3,11 +3,11 @@ import com.baseketbandit.runeapi.entity.Skill; import com.baseketbandit.runeapi.io.RequestHandler; -import java.util.List; +import java.util.Map; public class RuneAPI { - public static List getStats(String username) { + public static Map getStats(String username) { return RequestHandler.doRequest(username.replace(" ", "%20")); } } diff --git a/src/main/java/com/baseketbandit/runeapi/io/Parser.java b/src/main/java/com/baseketbandit/runeapi/io/Parser.java index 5790aa9..38ba056 100644 --- a/src/main/java/com/baseketbandit/runeapi/io/Parser.java +++ b/src/main/java/com/baseketbandit/runeapi/io/Parser.java @@ -2,32 +2,32 @@ import com.baseketbandit.runeapi.entity.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; class Parser { /** - * Breaks down raw data from the API to a list of Skill objects. + * Breaks down raw data from the API to a hash map of Skill objects. * * @param data raw data from the RuneScape API - * @return List list of objects of type Skill + * @return Map HashMap of objects of type Skill */ - static List parseStats(String data) { + static Map parseStats(String data) { List skillNames = Arrays.asList(Skill.Name.values()); - ArrayList skills = new ArrayList<>(); + Map skills = new HashMap<>(); String[] rawSkill = data.split("\n"); + int i = 0; for(String raw: rawSkill) { if(i < Skill.Name.values().length) { String[] r = raw.split(","); - skills.add(new Skill(skillNames.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2]))); + skills.put(skillNames.get(i).name(), new Skill(skillNames.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2]))); i++; continue; } break; } + return skills; } } diff --git a/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java b/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java index 271550c..621b293 100644 --- a/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java +++ b/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java @@ -8,20 +8,20 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; public class RequestHandler { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); private static final OkHttpClient client = new OkHttpClient(); /** - * A OkHttp method used to connect to and return the string of data from the server. + * A OkHttp method used to connect to and return a HashMap of data from the server. * * @param username the username to do the request against - * @return List list of objects of type Skill or an `empty` list if no results are found. + * @return Map HashMap of objects of type Skill or an `empty` map if no results are found. */ - public static List doRequest(String username) { + public static Map doRequest(String username) { try { Request request = new Request.Builder() .url("https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=" + username) @@ -30,13 +30,13 @@ public static List doRequest(String username) { if(response.code() != 200) { log.error(response.code() + " - No results found for username `" + username.replace("%20", " ") + "`"); - return new ArrayList<>(); + return new HashMap<>(); } return Parser.parseStats(response.body().string()); } catch(IOException e) { log.error("There was a problem processing that request. [Highscores OSRS]"); - return new ArrayList<>(); + return new HashMap<>(); } } } diff --git a/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java b/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java index 8946cc6..7e11878 100644 --- a/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java +++ b/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java @@ -3,7 +3,9 @@ import com.baseketbandit.runeapi.entity.Skill; import org.junit.Test; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.junit.Assert.*; @@ -11,25 +13,32 @@ public class RuneAPITest { @Test public void getStats() { - List stats = RuneAPI.getStats("xwr"); - for(Skill skill: stats) { + Map stats = RuneAPI.getStats("xwr"); + for(Skill skill: stats.values()) { System.out.println(skill.getName() + " - " + skill.getLevel()); } } @Test public void getStatsNotFound() { - List stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz"); - for(Skill skill: stats) { + Map stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz"); + for(Skill skill: stats.values()) { System.out.println(skill.getName() + " - " + skill.getLevel()); } } @Test public void getStatsNameSpace() { - List stats = RuneAPI.getStats("Iron Lewiso"); - for(Skill skill: stats) { + Map stats = RuneAPI.getStats("Iron Lewiso"); + for(Skill skill: stats.values()) { System.out.println(skill.getName() + " - " + skill.getLevel()); } } + + @Test + public void getStatsMapped() { + Map stats = RuneAPI.getStats("xwr"); + Skill cooking = stats.get("Cooking"); + System.out.println(cooking.getLevel()); + } } \ No newline at end of file