Advertisement
Chiddix

Highscores v1.0.1

Nov 15th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 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.      * The amount of recorded activities in old school RuneScape.
  32.      */
  33.     private static final int ACTIVITY_COUNT = 3;
  34.  
  35.     /**
  36.      * Gets all of the specified player's skills.
  37.      * @param playerName The name of the player to get the skills of.
  38.      * @return The skills of the player.
  39.      * @throws IllegalArgumentException if the player name isn't a valid one.
  40.      */
  41.     public static Skill[] getSkills(final String playerName) {
  42.         try {
  43.             final Skill[] skills = new Skill[SKILL_COUNT];
  44.             try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(new StringBuffer(BASE_URL).append(playerName).toString()).openStream()))) {
  45.                 for (int skill = 0; skill < SKILL_COUNT; skill++) {
  46.                     final String[] skillEncodedSplit = reader.readLine().split(",");
  47.                     skills[skill] = new Skill(Integer.parseInt(skillEncodedSplit[0]), Integer.parseInt(skillEncodedSplit[1]), Integer.parseInt(skillEncodedSplit[2]));
  48.                 }
  49.             }
  50.             return skills;
  51.         } catch (final IOException e) {
  52.             throw new IllegalArgumentException("Username not valid");
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * Gets the specified skill of the specified player.
  58.      * @param playerName The name of the player to get the skill of.
  59.      * @param skillId The id of the skill to get.
  60.      * @return The skill of the player.
  61.      */
  62.     public static Skill getSkill(final String playerName, int skillId) {   
  63.         return getSkills(playerName)[skillId];
  64.     }
  65.  
  66.     /**
  67.      * Gets all of the specified player's activities.
  68.      * @param playerName The name of the player to get the activities of.
  69.      * @return The activities of the player.
  70.      * @throws IllegalArgumentException if the player name isn't a valid one.
  71.      */
  72.     public static Activity[] getActivities(final String playerName) {
  73.         try {
  74.             final Activity[] activities = new Activity[ACTIVITY_COUNT];
  75.             try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(new StringBuffer(BASE_URL).append(playerName).toString()).openStream()))) {
  76.                 for (int skill = 0; skill < SKILL_COUNT; skill++) {
  77.                     reader.readLine();
  78.                 }
  79.                 for (int activity = 0; activity < ACTIVITY_COUNT; activity++) {
  80.                     final String[] activityEncodedSplit = reader.readLine().split(",");
  81.                     activities[activity] = new Activity(Integer.parseInt(activityEncodedSplit[0]), Integer.parseInt(activityEncodedSplit[1]));
  82.                 }
  83.             }
  84.             return activities;
  85.         } catch (final IOException e) {
  86.             throw new IllegalArgumentException("Username not valid");
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Gets the specified activity of the specified player.
  92.      * @param playerName The name of the player to get the activity of.
  93.      * @param activityId The id of the activity to get.
  94.      * @return The activity of the player.
  95.      */
  96.     public static Activity getActivity(final String playerName, int activityId) {  
  97.         return getActivities(playerName)[activityId];
  98.     }
  99.  
  100.     /**
  101.      * Represents a single skill.
  102.      * @author Ryan Greene
  103.      *
  104.      */
  105.     public static class Skill {
  106.  
  107.         /**
  108.          * The rank of the skill.
  109.          */
  110.         private final int rank;
  111.  
  112.         /**
  113.          * The level of the skill.
  114.          */
  115.         private final int level;
  116.  
  117.         /**
  118.          * The xp of the skill.
  119.          */
  120.         private final int xp;
  121.  
  122.         /**
  123.          * Constructs a new skill with the specified rank, level, and xp.
  124.          * @param rank The rank of the skill.
  125.          * @param level The level of the skill.
  126.          * @param xp The xp of the skill.
  127.          */
  128.         private Skill(final int rank, final int level, final int xp) {
  129.             this.rank = rank;
  130.             this.level = level;
  131.             this.xp = xp;
  132.         }
  133.  
  134.         /**
  135.          * Gets the rank of the skill.
  136.          * @return The rank of the skill.
  137.          */
  138.         public int getRank() {
  139.             return rank;
  140.         }
  141.  
  142.         /**
  143.          * Gets the level of the skill.
  144.          * @return The level of the skill.
  145.          */
  146.         public int getLevel() {
  147.             return level;
  148.         }
  149.  
  150.         /**
  151.          * Gets the xp of the skill.
  152.          * @return The xp of the skill.
  153.          */
  154.         public int getXp() {
  155.             return xp;
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Represents a single activity.
  161.      * @author Ryan Greene
  162.      *
  163.      */
  164.     public static class Activity {
  165.  
  166.         /**
  167.          * The rank of the activity.
  168.          */
  169.         private final int rank;
  170.  
  171.         /**
  172.          * The score of the activity.
  173.          */
  174.         private final int score;
  175.  
  176.         /**
  177.          * Constructs a new activity with the specified rank and score.
  178.          * @param rank The rank of the activity.
  179.          * @param score The score of the activity.
  180.          */
  181.         private Activity(final int rank, final int score) {
  182.             this.rank = rank;
  183.             this.score = score;
  184.         }
  185.  
  186.         /**
  187.          * Gets the rank of the activity.
  188.          * @return The rank of the activity.
  189.          */
  190.         public int getRank() {
  191.             return rank;
  192.         }
  193.  
  194.         /**
  195.          * Gets the score of the activity.
  196.          * @return The score of the activity.
  197.          */
  198.         public int getScore() {
  199.             return score;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement