node-steamcommunity/components/market.js

258 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-02-14 13:29:21 +08:00
const Cheerio = require('cheerio');
const SteamCommunity = require('../index.js');
2020-11-05 15:19:31 +08:00
const Helpers = require('./helpers.js');
/**
* Get a list of all apps on the market
* @param {function} callback - First argument is null|Error, second is an object of appid => name
*/
SteamCommunity.prototype.getMarketApps = function(callback) {
2021-07-22 15:44:40 +08:00
this.httpRequest('https://steamcommunity.com/market/', (err, response, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:59:55 +08:00
let $ = Cheerio.load(body);
if ($('.market_search_game_button_group')) {
let apps = {};
$('.market_search_game_button_group a.game_button').each((i, element) => {
2021-07-22 15:44:40 +08:00
let e = Cheerio.load(element);
let name = e('.game_button_game_name').text().trim();
let url = element.attribs.href;
let appid = url.substr(url.indexOf('=') + 1);
apps[appid] = name;
});
2015-07-31 04:45:07 +08:00
callback(null, apps);
} else {
2021-07-22 15:44:40 +08:00
callback(new Error('Malformed response'));
}
2021-07-22 15:44:40 +08:00
}, 'steamcommunity');
2016-03-05 08:35:04 +08:00
};
/**
2018-04-01 15:31:07 +08:00
* Check if an item is eligible to be turned into gems and if so, get its gem value
* @param {int} appid
* @param {int|string} assetid
* @param {function} callback
*/
SteamCommunity.prototype.getGemValue = function(appid, assetid, callback) {
this._myProfile({
2021-07-22 15:44:40 +08:00
endpoint: 'ajaxgetgoovalue/',
qs: {
sessionid: this.getSessionID(),
appid: appid,
contextid: 6,
assetid: assetid
},
2021-07-22 15:44:40 +08:00
checkHttpError: false,
json: true
}, null, (err, res, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:44:40 +08:00
let err2 = Helpers.eresultError(body.success, body.message);
if (err2) {
return callback(err2);
}
if (!body.goo_value || !body.strTitle) {
2021-07-22 15:44:40 +08:00
callback(new Error('Malformed response'));
return;
}
2021-07-22 15:44:40 +08:00
callback(null, {promptTitle: body.strTitle, gemValue: parseInt(body.goo_value, 10)});
});
};
2018-04-01 15:31:07 +08:00
/**
* Turn an eligible item into gems.
* @param {int} appid
* @param {int|string} assetid
* @param {int} expectedGemsValue
* @param {function} callback
*/
SteamCommunity.prototype.turnItemIntoGems = function(appid, assetid, expectedGemsValue, callback) {
this._myProfile({
2021-07-22 15:44:40 +08:00
endpoint: 'ajaxgrindintogoo/',
json: true,
checkHttpError: false
}, {
2021-07-22 15:44:40 +08:00
appid: appid,
contextid: 6,
assetid: assetid,
goo_value_expected: expectedGemsValue,
sessionid: this.getSessionID()
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:44:40 +08:00
let err2 = Helpers.eresultError(body.success, body.message);
if (err2) {
return callback(err2);
}
2021-07-22 15:44:40 +08:00
if (!body['goo_value_received '] || !body.goo_value_total) { // lol valve, that trailing space is real
callback(new Error('Malformed response'));
return;
}
2021-07-22 15:44:40 +08:00
callback(null, {gemsReceived: parseInt(body['goo_value_received '], 10), totalGems: parseInt(body.goo_value_total, 10)});
2021-07-29 14:55:56 +08:00
});
};
2018-04-01 15:38:45 +08:00
2020-05-23 14:24:20 +08:00
/**
* Open a booster pack.
* @param {int} appid
* @param {int|string} assetid
* @param {function} callback
*/
SteamCommunity.prototype.openBoosterPack = function(appid, assetid, callback) {
this._myProfile({
2021-07-22 15:44:40 +08:00
endpoint: 'ajaxunpackbooster/',
json: true,
checkHttpError: false
2020-05-23 14:24:20 +08:00
}, {
2021-07-22 15:44:40 +08:00
appid: appid,
communityitemid: assetid,
sessionid: this.getSessionID()
2020-05-23 14:24:20 +08:00
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:44:40 +08:00
let err2 = Helpers.eresultError(body.success, body.message);
if (err2) {
return callback(err2);
2020-05-23 14:24:20 +08:00
}
if (!body.rgItems) {
2021-07-22 15:44:40 +08:00
callback(new Error('Malformed response'));
2020-05-23 14:24:20 +08:00
return;
}
callback(null, body.rgItems);
2021-07-29 14:55:56 +08:00
});
2020-05-23 14:24:20 +08:00
};
2018-04-01 15:38:45 +08:00
/**
* Get details about a gift in your inventory.
* @param {string} giftID
* @param {function} callback
*/
SteamCommunity.prototype.getGiftDetails = function(giftID, callback) {
this.httpRequestPost({
2021-07-22 15:44:40 +08:00
uri: `https://steamcommunity.com/gifts/${giftID}/validateunpack`,
form: {
sessionid: this.getSessionID()
2018-04-01 15:38:45 +08:00
},
2021-07-22 15:44:40 +08:00
json: true
2018-04-01 15:38:45 +08:00
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:44:40 +08:00
let err2 = Helpers.eresultError(body.success, body.message);
if (err2) {
return callback(err2);
2018-04-01 15:38:45 +08:00
}
if (!body.packageid || !body.gift_name) {
2021-07-22 15:44:40 +08:00
callback(new Error('Malformed response'));
2018-04-01 15:38:45 +08:00
return;
}
callback(null, {
2021-07-22 15:44:40 +08:00
giftName: body.gift_name,
packageID: parseInt(body.packageid, 10),
owned: body.owned
2018-04-01 15:38:45 +08:00
});
});
};
/**
* Unpack a gift in your inventory to your library.
* @param {string} giftID
* @param {function} callback
*/
SteamCommunity.prototype.redeemGift = function(giftID, callback) {
this.httpRequestPost({
2021-07-22 15:44:40 +08:00
uri: `https://steamcommunity.com/gifts/${giftID}/unpack`,
form: {
sessionid: this.getSessionID()
2018-04-01 15:38:45 +08:00
},
2021-07-22 15:44:40 +08:00
json: true
2018-04-01 15:38:45 +08:00
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:44:40 +08:00
let err2 = Helpers.eresultError(body.success, body.message);
if (err2) {
return callback(err2);
2018-04-01 15:38:45 +08:00
}
callback(null);
});
};
/**
* @param {int|string} assetid
2020-11-05 15:19:31 +08:00
* @param {int} denominationIn
* @param {int} denominationOut
* @param {int} quantityIn
* @param {int} quantityOut
* @param {function} callback
2020-11-05 15:19:31 +08:00
* @private
*/
2020-11-05 15:19:31 +08:00
SteamCommunity.prototype._gemExchange = function(assetid, denominationIn, denominationOut, quantityIn, quantityOut, callback) {
this._myProfile({
2020-11-05 15:19:31 +08:00
endpoint: 'ajaxexchangegoo/',
json: true,
checkHttpError: false
}, {
2020-11-05 15:19:31 +08:00
appid: 753,
assetid,
goo_denomination_in: denominationIn,
goo_amount_in: quantityIn,
goo_denomination_out: denominationOut,
goo_amount_out_expected: quantityOut,
sessionid: this.getSessionID()
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
2020-11-05 15:19:31 +08:00
callback(Helpers.eresultError(body.success));
});
};
2020-11-05 15:19:31 +08:00
/**
* Pack gems into sack of gems.
* @param {int|string} assetid - ID of gem stack you want to pack into sacks
* @param {int} desiredSackCount - How many sacks you want. You must have at least this amount * 1000 gems in the stack you're packing
* @param {function} callback
*/
SteamCommunity.prototype.packGemSacks = function(assetid, desiredSackCount, callback) {
this._gemExchange(assetid, 1, 1000, desiredSackCount * 1000, desiredSackCount, callback);
};
/**
* Unpack sack of gems into gems.
2020-11-05 15:19:31 +08:00
* @param {int|string} assetid - ID of sack stack you want to unpack (say that 5 times fast)
* @param {int} sacksToUnpack
* @param {function} callback
*/
2020-11-05 15:19:31 +08:00
SteamCommunity.prototype.unpackGemSacks = function(assetid, sacksToUnpack, callback) {
this._gemExchange(assetid, 1000, 1, sacksToUnpack, sacksToUnpack * 1000, callback);
};