Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sell OSRS Gold
Sign in to follow this  
orange451

Grand Exchange Item DTO

Recommended Posts

Posted (edited)

Here's a small snippet I wrote for my personal use. This makes querying GE items DTO friendly.

 

public class GrandExchangeUtil {
	
	/** Internal GE data */
	private static JSONObject priceData;
	
	/** Last timestamp GE data was downloaded */
	private static long lastUpdateTime;
	
	/** Rate at which we refresh internally stored GE data */
	public static long REFRESH_GE_TIME = 1000 * 60;
	
	/**
	 * Force fetches all the price data for GE Items.
	 */
	public static void forceUpdateGEPriceData() {
		try {
			String address = "https://rsbuddy.com/exchange/summary.json";
			URL url = new URL(address);
			URLConnection connection = url.openConnection();
			InputStream inputStream = connection.getInputStream();
			InputStreamReader reader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String jsonSource = bufferedReader.readLine();
			bufferedReader.close();
			
			JSONParser parser = new JSONParser();
			priceData = (JSONObject) parser.parse(jsonSource);
			
			lastUpdateTime = System.currentTimeMillis();
		} catch(Exception e ) {
			e.printStackTrace();
		}
	}
	
	private static void updateGEPriceData() {
		if ( priceData == null || System.currentTimeMillis()-lastUpdateTime > REFRESH_GE_TIME )
			forceUpdateGEPriceData();
	}
	
	/**
	 * Get GEItem from OSRS item ID
	 * @param id
	 * @return
	 */
	public static GEItem getItemData(int id) {
		updateGEPriceData();
		
		JSONObject data = (JSONObject) priceData.get(Integer.toString(id));
		if ( data == null )
			return null;
		
		return new GEItem(data);
	}
	
	/**
	 * Get GEItem from RSItem
	 * @param id
	 * @return
	 */
	public static GEItem getItemData(RSItem item) {
		return getItemData(item.getID());
	}
}
public class GEItem {
	
	/** ID of the item */
	private int id;
	
	/** Display name of the item */
	private String name;
	
	/** Is item Members only */
	private boolean members;
	
	/** Average price bought */
	private int buyAverage;
	
	/** Average quantity bought */
	private int buyQuantity;
	
	/** Average price sold */
	private int sellAverage;
	
	/** Average quantity sold */
	private int sellQuantity;
	
	/** Overall average price */ 
	private int overallAverage;
	
	/** Overall average quantity */
	private int overallQuantity;
	
	/** sp */
	private int sp;
	
	public GEItem(Map<String,Object> data) {
		this.id = toIntegerSafe(data.get("id"));
		this.name = toStringSafe(data.get("name"));
		this.members = toBooleanSafe(data.get("members"));
		this.buyAverage = toIntegerSafe(data.get("buy_average"));
		this.buyQuantity = toIntegerSafe(data.get("buy_quantity"));
		this.sellAverage = toIntegerSafe(data.get("sell_average"));
		this.sellQuantity = toIntegerSafe(data.get("sell_quantity"));
		this.overallAverage = toIntegerSafe(data.get("overall_average"));
		this.overallQuantity = toIntegerSafe(data.get("overall_quantity"));
		this.sp = toIntegerSafe(data.get("sp"));
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getId() {
		return this.id;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public boolean isMembers() {
		return this.members;
	}
	
	public void setMembers(boolean members) {
		this.members = members;
	}
	
	public int getBuyAverage() {
		return this.buyAverage;
	}
	
	public void setBuyAverage(int average) {
		this.buyAverage = average;
	}
	
	public int getBuyQuantity() {
		return this.buyQuantity;
	}
	
	public void setBuyQuantity(int buyQuantity) {
		this.buyQuantity = buyQuantity;
	}
	
	public int getSellAverage() {
		return this.sellAverage;
	}
	
	public void setSellAverage(int average) {
		this.sellAverage = average;
	}
	
	public int getSellQuantity() {
		return this.sellQuantity;
	}
	
	public void setSellQuantity(int quantity) {
		this.sellQuantity = quantity;
	}
	
	public int getOverallAverage() {
		return this.overallAverage;
	}
	
	public void setOverallAverage(int average) {
		this.overallAverage = average;
	}
	
	public int getOverallQuantity() {
		return this.overallQuantity;
	}
	
	public void setOverallQuantity(int quantity) {
		this.overallQuantity = quantity;
	}
	
	public int getSP() {
		return this.sp;
	}

	private int toIntegerSafe(Object object) {
		if ( object instanceof String ) {
			try {
				return (int)Double.parseDouble(object.toString());
			} catch(Exception e) {
				e.printStackTrace();
				return 0;
			}
		}
		if ( object instanceof Number )
			return ((Number)object).intValue();
		
		return 0;
	}
	
	private String toStringSafe(Object object) {
		if ( object == null )
			return "";
		
		return object.toString();
	}
	
	private boolean toBooleanSafe(Object object) {
		if ( object == null )
			return false;
		
		if ( !(object instanceof Boolean) )
			return false;
		
		return ((Boolean)object).booleanValue();
	}
	
	@SuppressWarnings("unchecked")
	public JSONObject toJSON() {
		JSONObject json = new JSONObject();
		json.put("id", this.getId());
		json.put("name", this.getName());
		json.put("members", this.isMembers());
		json.put("sp", this.getSP());
		json.put("buy_average", this.getBuyAverage());
		json.put("buy_quantity", this.getBuyQuantity());
		json.put("sell_average", this.getSellAverage());
		json.put("sell_quantity", this.getSellQuantity());
		json.put("overall_average", this.getOverallAverage());
		json.put("overall_quantity", this.getOverallQuantity());
		return json;
	}
}

 

Edited by orange451
  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Our picks

    • Over the past few months, I’ve been working diligently on a new project - TRiBot X. Everything has been written from the ground up, with all of the best practices of software engineering. Every aspect of TRiBot has been re-imagined to support three main goals: flexibility, useability, and reliability.
      • 30 replies
    • Come give us feedback on the next version of TRiBot!
      • 74 replies
    • TRiBot is looking to improve a lot of its customer relationship management, customer on boarding process, customer experience, design elements, community engagement and pretty much everything else you can imagine when it comes to marketing.

      Our goal: To ensure that the marketing done TRULY reflects the experience and does not shine an inaccurate light on what TRiBot is lacking in.

      So I ask, what do you love about TRiBot and what do you hate about TRiBot? What does O S Bot, Rune M8, PowR Bot and Dre amBot do better? (yes I purposely didn't spell it right 😂).

      Love, 

      RileyZ
      • 23 replies
    • Over the last three weeks, I've been working on upgrading our server infrastructure. It's finally ready and is now live!

      Why?

      Increased reliability - less server errors


      Increased availability - less downtime


      Increased security - keeping us and you secure


      Increased capacity - ability to serve you better


      Increased speed - less waiting for things to load


      Faster development - server and service updates will come faster


      What are the changes?

      Move from a single AWS EC2 instance to AWS ECS (Elastic Container Service)


      Distributed computing


      Load balancing


      Git management of server files and filesystem


      Redis caching


      How?

      AWS ECS (with 10 EC2 instances)


      AWS ElastiCache (Redis)


      AWS Load Balancing


      AWS EFS (Elastic file system)


      Please bare with us as I continue to tune the server for maximum performance. Slow loading speeds may occur temporarily. I thank everyone for their patience.

      Please post on this thread if you experience any issues other than slow loading times.
      • 51 replies
    • This release will:

      Fix prayers and world hopper API (Thanks @JoeDezzy1 and @erickho123)


      Improve banking API (Thanks @Encoded)


      Adds methods for returning and using Java Lists, rather than arrays


      Slightly randomizes some hardcoded behaviour


      Removes sleeps from waitConditions; the efficiency saving potential is negligible in these use-cases, therefore cleaner code is preferable


      Other back-end improvements





      Note: If you are using LG, please restart both the RS client and TRiBot.
      • 90 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...