Skip to content

Commit

Permalink
added a skill type enum for getting mapped values to help prevent inv…
Browse files Browse the repository at this point in the history
…alid parameters
  • Loading branch information
BasketBandit committed Jun 18, 2019
1 parent 512fae3 commit 3fbaadd
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 70 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ Simple open source Java wrapper for the OSRS portion of the [RuneScape](https://

**#1** - Loop all skills and print their data.
```java
Map<String, Skill> stats = RuneAPI.getStats(username);
Map<Type, Skill> stats = RuneAPI.getStats(username);

for(Skill skill: stats.values()) {
System.out.printf("%s - %,d - %,d - %,d \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
System.out.printf("%s: #%,d - %,d - %,dxp \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
}
```

**#2** - Find specific skill and print its data.
```java
Map<String, Skill> stats = RuneAPI.getStats(username);
Skill overall = stats.get("Overall");
Map<Type, Skill> stats = RuneAPI.getStats(username);
Skill overall = stats.get(Type.OVERALL);

System.out.printf("%s - %,d - %,d - %,d \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience());
System.out.printf("%s: #%,d - %,d - %,dxp \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience());
```

## Download
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/baseketbandit/runeapi/RuneAPI.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.baseketbandit.runeapi;

import com.baseketbandit.runeapi.entity.Skill;
import com.baseketbandit.runeapi.entity.Type;
import com.baseketbandit.runeapi.io.RequestHandler;

import java.util.Map;

public class RuneAPI {

public static Map<String, Skill> getStats(String username) {
public static Map<Type, Skill> getStats(String username) {
return RequestHandler.doGetRequest(username.replace(" ", "%20"));
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/baseketbandit/runeapi/entity/Skill.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.baseketbandit.runeapi.entity;

public class Skill {

private final String name;
private final Type name;
private final int rank;
private final int level;
private final int experience;

public Skill(String name, int rank, int level, int experience) {
public Skill(Type name, int rank, int level, int experience) {
this.name = name;
this.rank = rank;
this.level = level;
this.experience = experience;
}

public String getName() {
return name;
return name.name;
}

public int getRank() {
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/com/baseketbandit/runeapi/entity/Skills.java

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/java/com/baseketbandit/runeapi/entity/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baseketbandit.runeapi.entity;

public enum Type {
OVERALL("Overall"),
ATTACK("Attack"),
DEFENSE("Defense"),
STRENGTH("Strength"),
HITPOINTS("Hitpoints"),
RANGED("Ranged"),
PRAYER("Prayer"),
MAGIC("Magic"),
COOKING("Cooking"),
WOODCUTTING("Woodcutting"),
FLETCHING("Fletching"),
FISHING("Fishing"),
FIREMAKING("Firemaking"),
CRAFTING("Crafting"),
SMITHING("Smithing"),
MINING("Mining"),
HERBLORE("Herblore"),
AGILITY("Agility"),
THIEVING("Thieving"),
SLAYER("Slayer"),
FARMING("Farming"),
RUNECRAFT("Runecraft"),
HUNTER("Hunter"),
CONSTRUCTION("Construction");

final String name;

Type(String name) {
this.name = name;
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/baseketbandit/runeapi/io/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class Parser {
* Breaks down raw data from the API to a hash map of Skill objects.
*
* @param data raw data from the RuneScape API
* @return Map<String, Skill> HashMap of objects of type Skill
* @return Map<Type, Skill> Map of objects of type Skill
*/
static Map<String, Skill> parseStats(String data) {
List<Enum> names = Arrays.asList(Skills.values());
Map<String, Skill> skills = new HashMap<>();
String[] raw = data.split("\n");
static Map<Type, Skill> parseStats(String data) {
List<Type> types = Arrays.asList(Type.values());
EnumMap<Type, Skill> skillMap = new EnumMap<>(Type.class);
String[] lineSplitData = data.split("\n");

for(int i = 0; i < raw.length; i++) {
if(i < Skills.values().length) {
String[] r = raw[i].split(",");
skills.put(names.get(i).name(), new Skill(names.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
for(int i = 0; i < lineSplitData.length; i++) {
if(i < Type.values().length) {
String[] r = lineSplitData[i].split(",");
skillMap.put(types.get(i), new Skill(types.get(i), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
continue;
}
break;
}

return skills;
return skillMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class RequestHandler {
* 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 Map<String, Skill> HashMap of objects of type Skill or an `empty` map if no results are found.
* @return Map<Type, Skill> Map of objects of type Skill or an `empty` map if no results are found.
*/
public static Map<String, Skill> doGetRequest(String username) {
public static Map<Type, Skill> doGetRequest(String username) {
try {
Request request = new Request.Builder()
.url(API_BASE + username)
.build();
Response response = client.newCall(request).execute();

if(response.code() != 200) {
log.error(response.code() + " - No results found for username `" + username.replace("%20", " ") + "`");
log.error(response.code() + ": No results found for username `" + username.replace("%20", " ") + "`");
response.close();
return new HashMap<>();
}
Expand Down
37 changes: 18 additions & 19 deletions src/test/java/com/baseketbandit/runeapi/RuneAPITest.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
package com.baseketbandit.runeapi;

import com.baseketbandit.runeapi.entity.Skill;
import com.baseketbandit.runeapi.entity.Type;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

public class RuneAPITest {

@Test
public void getStats() {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
Map<Type, Skill> stats = RuneAPI.getStats("xwr");
assert(!stats.isEmpty());
assert(stats.size() == 24);

for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
System.out.printf("%s: #%,d - %,d - %,dxp \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
}
}

@Test
public void getStatsNotFound() {
Map<String, Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
}
Map<Type, Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
assert(stats.isEmpty());
}

@Test
public void getStatsNameSpace() {
Map<String, Skill> stats = RuneAPI.getStats("Iron Lewiso");
Map<Type, Skill> stats = RuneAPI.getStats("Iron Lewiso");
assert(!stats.isEmpty());
assert(stats.size() == 24);

for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
}
}

@Test
public void getStatsMapped() {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
Skill cooking = stats.get("Cooking");
System.out.println(cooking.getLevel());
}
Map<Type, Skill> stats = RuneAPI.getStats("xwr");
assert(!stats.isEmpty());
assert(stats.size() == 24);

Skill cooking = stats.get(Type.COOKING);
assert(!cooking.getName().equals(""));

@Test
public void getStatsMappedLowercase() {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
Skill cooking = stats.get("Cooking");
System.out.println(cooking.getLevel());
}
}

0 comments on commit 3fbaadd

Please sign in to comment.