mirror of
https://github.com/DoctorMcKay/node-steamcommunity.git
synced 2025-03-29 01:30:09 +08:00
Added some optimizations and backwards-compatibility for inv fetching
This commit is contained in:
parent
ab037b1931
commit
513b0bff1c
@ -272,18 +272,25 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
|
|||||||
"qs": {
|
"qs": {
|
||||||
"l": "english", // Default language
|
"l": "english", // Default language
|
||||||
"count": 5000, // Max items per 'page'
|
"count": 5000, // Max items per 'page'
|
||||||
"start_assetid": start,
|
"start_assetid": start
|
||||||
},
|
},
|
||||||
"json": true
|
"json": true
|
||||||
}, function(err, response, body) {
|
}, function(err, response, body) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
if (err.message == "HTTP error 403" && body === null) {
|
||||||
|
// 403 with a body of "null" means the inventory/profile is private.
|
||||||
|
callback(new Error("This profile is private."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!body || !body.success || !body.assets || !body.descriptions) {
|
if (!body || !body.success || !body.assets || !body.descriptions) {
|
||||||
if(body) {
|
if (body) {
|
||||||
callback(new Error(body.error || "Malformed response"));
|
// Dunno if the error/Error property even exists on this new endpoint
|
||||||
|
callback(new Error(body.error || body.Error || "Malformed response"));
|
||||||
} else {
|
} else {
|
||||||
callback(new Error("Malformed response"));
|
callback(new Error("Malformed response"));
|
||||||
}
|
}
|
||||||
@ -291,31 +298,40 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var i = 0; i < body.assets.length; i++) {
|
for (var i = 0; i < body.assets.length; i++) {
|
||||||
var description = getDescription(body.descriptions, body.assets[i].classid, body.assets[i].instanceid)
|
var description = getDescription(body.descriptions, body.assets[i].classid, body.assets[i].instanceid);
|
||||||
|
|
||||||
if(!tradableOnly || description && description.tradable) {
|
if (!tradableOnly || (description && description.tradable)) {
|
||||||
inventory.push(new CEconItem(body.assets[i], description, contextID));
|
(body.assets[i].currencyid ? currency : inventory).push(new CEconItem(body.assets[i], description, contextID));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dunno how to handle currencies now
|
|
||||||
|
|
||||||
if(body.total_inventory_count > 5000 * step) {
|
if(body.total_inventory_count > 5000 * step) {
|
||||||
get(inventory, currency, step + 1, body.assets[body.assets.length - 1].assetid);
|
get(inventory, currency, step + 1, body.assets[body.assets.length - 1].assetid);
|
||||||
} else {
|
} else {
|
||||||
callback(null, inventory, currency);
|
callback(null, inventory, currency, body.total_inventory_count);
|
||||||
}
|
}
|
||||||
}, "steamcommunity");
|
}, "steamcommunity");
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
function getDescription(descriptions, classID, instanceID) {
|
// A bit of optimization; objects are hash tables so it's more efficient to look up by key than to iterate an array
|
||||||
for(var i = 0; i < descriptions.length; i++) {
|
var quickDescriptionLookup = {};
|
||||||
if(descriptions[i].classid == classID && descriptions[i].instanceid == instanceID) {
|
|
||||||
return descriptions[i];
|
function getDescription(descriptions, classID, instanceID) {
|
||||||
|
instanceID = instanceID || '0'; // instanceID can be undefined, in which case it's 0.
|
||||||
|
|
||||||
|
var key = classID + '_' + instanceID;
|
||||||
|
|
||||||
|
if (quickDescriptionLookup[key]) {
|
||||||
|
return quickDescriptionLookup[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < descriptions.length; i++) {
|
||||||
|
quickDescriptionLookup[key] = descriptions[i];
|
||||||
|
|
||||||
|
if (descriptions[i].classid == classID && descriptions[i].instanceid == instanceID) {
|
||||||
|
return descriptions[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user