Chiddix

Highscores

Nov 15th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5.  
  6. /**
  7.  * A wrapper for the RuneScape old school highscores API.
  8.  * @author Ryan Greene
  9.  *
  10.  */
  11. public final class Highscores {
  12.  
  13.     /**
  14.      * A private constructor to prevent initialization.
  15.      */
  16.     private Highscores() {
  17.        
  18.     }
  19.  
  20.     /**
  21.      * The base URL of the API request.
  22.      */
  23.     private static final String BASE_URL = "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=";
  24.  
  25.     /**
  26.      * The amount of skills in old school RuneScape.
  27.      */
  28.     private static final int SKILL_COUNT = 24;
  29.  
  30.     /**
  31.      * Gets all of the specified player's skills.
  32.      * @param playerName The name of the player to get the skills of.
  33.      * @return The skills of the player.
  34.      * @throws IllegalArgumentException if the player name isn't a valid one.
  35.      */
  36.     public static Skill[] getSkills(final String playerName) {
  37.         try {
  38.             final Skill[] skills = new Skill[SKILL_COUNT];
  39.             try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(new StringBuffer(BASE_URL).append(playerName).toString()).openStream()))) {
  40.                 for (int skill = 0; skill < SKILL_COUNT; skill++) {
  41.                     final String[] skillEncodedSplit = reader.readLine().split(",");
  42.                     skills[skill] = new Skill(Integer.parseInt(skillEncodedSplit[0]), Integer.parseInt(skillEncodedSplit[1]), Integer.parseInt(skillEncodedSplit[2]));
  43.                 }
  44.             }
  45.             return skills;
  46.         } catch (final IOException e) {
  47.             throw new IllegalArgumentException("Username not valid");
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Gets the specified skill of the specified player.
  53.      * @param playerName The name of the player to get the skill of.
  54.      * @param skillId The id of the skill to get.
  55.      * @return The skill of the player.
  56.      */
  57.     public static Skill getSkill(final String playerName, int skillId) {   
  58.         return getSkills(playerName)[skillId];
  59.     }
  60.  
  61.     /**
  62.      * Represents a single skill.
  63.      * @author Ryan Greene
  64.      *
  65.      */
  66.     public static class Skill {
  67.  
  68.         /**
  69.          * The rank of the skill.
  70.          */
  71.         private final int rank;
  72.  
  73.         /**
  74.          * The level of the skill.
  75.          */
  76.         private final int level;
  77.  
  78.         /**
  79.          * The xp of the skill.
  80.          */
  81.         private final int xp;
  82.  
  83.         /**
  84.          * Constructs a new skill with he specified rank, level, and xp.
  85.          * @param rank The rank of the skill.
  86.          * @param level The level of the skill.
  87.          * @param xp The xp of the skill.
  88.          */
  89.         private Skill(final int rank, final int level, final int xp) {
  90.             this.rank = rank;
  91.             this.level = level;
  92.             this.xp = xp;
  93.         }
  94.  
  95.         /**
  96.          * Gets the rank of the skill.
  97.          * @return The rank of the skill.
  98.          */
  99.         public int getRank() {
  100.             return rank;
  101.         }
  102.  
  103.         /**
  104.          * Gets the level of the skill.
  105.          * @return The level of the skill.
  106.          */
  107.         public int getLevel() {
  108.             return level;
  109.         }
  110.  
  111.         /**
  112.          * Gets the xp of the skill.
  113.          * @return The xp of the skill.
  114.          */
  115.         public int getXp() {
  116.             return xp;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment