From 09cd81b60783c6babb377092759384daa3883411 Mon Sep 17 00:00:00 2001 From: Alexander Corn Date: Mon, 7 Sep 2015 00:00:19 -0400 Subject: [PATCH] Added CEconItem and getUserInventory --- classes/CEconItem.js | 56 +++++++++++++++++++++++++++++++++++++++++ components/users.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 classes/CEconItem.js diff --git a/classes/CEconItem.js b/classes/CEconItem.js new file mode 100644 index 0000000..9b2165f --- /dev/null +++ b/classes/CEconItem.js @@ -0,0 +1,56 @@ +module.exports = CEconItem; + +function CEconItem(item, descriptions) { + var thing; + for(thing in item) { + if(item.hasOwnProperty(thing)) { + this[thing] = item[thing]; + } + } + + this.assetid = this.id = (this.id || this.assetid); + this.instanceid = this.instanceid || '0'; + this.amount = parseInt(this.amount, 10); + + // Merge the description + var description = descriptions[this.classid + '_' + this.instanceid]; + if(description) { + for(thing in description) { + if(description.hasOwnProperty(thing)) { + this[thing] = description[thing]; + } + } + } + + this.tradable = !!this.tradable; + this.marketable = !!this.marketable; + this.commodity = !!this.commodity; + this.market_tradable_restriction = (this.market_tradable_restriction ? parseInt(this.market_tradable_restriction, 10) : 0); + this.market_marketable_restriction = (this.market_marketable_restriction ? parseInt(this.market_marketable_restriction, 10) : 0); +} + +CEconItem.prototype.getImageURL = function() { + return "https://steamcommunity-a.akamaihd.net/economy/image/" + this.icon_url + "/"; +}; + +CEconItem.prototype.getLargeImageURL = function() { + if(!this.icon_url_large) { + return this.getImageURL(); + } + + return "https://steamcommunity-a.akamaihd.net/economy/image/" + this.icon_url_large + "/"; +}; + +CEconItem.prototype.getTag = function(category) { + if(!this.tags) { + return null; + } + + for(var i = 0; i < this.tags.length; i++) { + if(this.tags[i].category == category) { + return this.tags[i]; + } + } + + return null; +}; diff --git a/components/users.js b/components/users.js index 41e9de0..42c57dd 100644 --- a/components/users.js +++ b/components/users.js @@ -1,5 +1,6 @@ var SteamCommunity = require('../index.js'); var SteamID = require('steamid'); +var CEconItem = require('../classes/CEconItem.js'); SteamCommunity.prototype.addFriend = function(userID, callback) { if(typeof userID === 'string') { @@ -236,3 +237,62 @@ SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) { callback(null, data); }); }; + +SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, tradableOnly, callback) { + var self = this; + + if(typeof userID === 'string') { + userID = new SteamID(userID); + } + + var endpoint = "/profiles/" + userID.getSteamID64(); + get([], []); + + function get(inventory, currency, start) { + self.request({ + "uri": "https://steamcommunity.com" + endpoint + "/inventory/json/" + appID + "/" + contextID, + "qs": { + "start": start, + "trading": tradableOnly ? 1 : undefined + }, + "json": true + }, function(err, response, body) { + if(self._checkHttpError(err, response, callback)) { + return; + } + + if(!body || !body.success || !body.rgInventory || !body.rgDescriptions || !body.rgCurrency) { + callback(new Error(body.Error || "Malformed response")); + return; + } + + var i; + for(i in body.rgInventory) { + if(!body.rgInventory.hasOwnProperty(i)) { + continue; + } + + inventory.push(new CEconItem(body.rgInventory[i], body.rgDescriptions)); + } + + for(i in body.rgCurrency) { + if(!body.rgCurrency.hasOwnProperty(i)) { + continue; + } + + currency.push(new CEconItem(body.rgInventory[i], body.rgDescriptions)); + } + + if(body.more) { + var match = response.request.uri.href.match(/\/(profiles|id)\/([^\/]+)\//); + if(match) { + endpoint = "/" + match[1] + "/" + match[2]; + } + + get(inventory, currency, body.more_start); + } else { + callback(null, inventory, currency); + } + }); + } +};