package rs.controller; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import rs.gui.Frame; public class Highscore { public static String text = "", info = ""; public static final String link = "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player="; private static String source; private static URL url; public static final int overall = 0; public static final int attack = 1; public static final int defence = 2; public static final int strength = 3; public static final int hitpoints = 4; public static final int ranged = 5; public static final int prayer = 6; public static final int magic = 7; public static final int cooking = 8; public static final int woodcutting = 9; public static final int fletching = 10; public static final int fishing = 11; public static final int firemaking = 12; public static final int crafting = 13; public static final int smithing = 14; public static final int mining = 15; public static final int herblore = 16; public static final int agility = 17; public static final int thieving = 18; public static final int slayer = 19; public static final int farming = 20; public static final int runecraft = 21; public static final int hunter = 22; public static final int construction = 23; private static int[] data = new int[24]; public static void load(String player) { try { extractSource(link + player); parseData(); } catch(Exception e) { info = text + "\n_______________________________\n\n" + "Error retrieving data.\nThis could be due to a few reasons such as:\n" + "(1) You are not connected to the internet.\n" + "(2) Runescape Highscores are down.\n" + "(3) You have misspelled the username.\n"; } } public static int getLevel(int ID) { return data[ID]; } private static void extractSource(String website) throws Exception { BufferedReader reader = null; StringBuffer buffer = null; URLConnection uc = null; String cl; try { URL url = new URL(website); uc = url.openConnection(); uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); uc.connect(); reader = new BufferedReader(new InputStreamReader(uc.getInputStream())); buffer = new StringBuffer(); while ((cl = reader.readLine()) != null) buffer.append(cl + "\n"); source = buffer.toString(); } finally { if(reader != null) reader.close(); } } private static void parseData() { String[] lines = source.split("\n"); String[][] sections = new String[lines.length][lines.length * 3]; for(int line = 0; line < lines.length; line++) { sections[line] = lines[line].split(","); for(int section = 0; section < sections.length; section++) if(section == 1) data[line] = Integer.parseInt(sections[line][section]); } } public void render(Graphics g) { String instructions = "Enter a username then press enter to load stats."; g.setColor(PriceGuide.shadowColor); g.fillRect(0, 0, Frame.WIDTH, Frame.HEIGHT); g.setColor(Color.green); g.drawString(instructions, 250, 20); g.drawString(text, 5, 40); int y = 60; for(String line: info.split("\n")) { g.drawString(line, 5, y); y += 15; } } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER) { if(info.contains(text)) text = ""; info = ""; load(text); if(info.length() == 0) info = text + "\n_______________________________\n" + "overall: " + getLevel(overall) + "\nattack: " + getLevel(attack) + "\nstrength: " + getLevel(strength) + "\ndefence: " + getLevel(defence) + "\nhitpoints: " + getLevel(hitpoints) + "\nranged: " + getLevel(ranged) + "\nmagic: " + getLevel(magic) + "\nprayer: " + getLevel(prayer) + "\ncooking: " + getLevel(cooking) + "\nwoodcutting: " + getLevel(woodcutting) + "\nfletching: " + getLevel(fletching) + "\nfishing: " + getLevel(fishing) + "\nfiremaking: " + getLevel(firemaking) + "\ncrafting: " + getLevel(crafting) + "\nsmithing: " + getLevel(smithing) + "\nmining: " + getLevel(mining) + "\nherblore: " + getLevel(herblore) + "\nagility: " + getLevel(agility) + "\nthieving: " + getLevel(thieving) + "\nslayer: " + getLevel(slayer) + "\nfarming: " + getLevel(farming) + "\nrunecraft: " + getLevel(runecraft) + "\nhunter: " + getLevel(hunter) + "\nconstruction: " + getLevel(construction); text = ""; } else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { if(text.length() > 0) text = text.substring(0, text.length()-1); } else if(Character.isDigit(e.getKeyChar()) || Character.isAlphabetic(e.getKeyChar()) || e.getKeyChar() == ' ' || e.getKeyChar() == '_') { text += e.getKeyChar(); } } }