diff --git a/components/profile.js b/components/profile.js index e8652021..17c0cf8c 100644 --- a/components/profile.js +++ b/components/profile.js @@ -32,6 +32,50 @@ SteamCommunity.prototype.setupProfile = function(callback) { }); }; +SteamCommunity.prototype.editShowcaseItem = function(showcase, slot, item, callback) { + //The possible options, with the maximum number of slots and the corresponding type + const allowedoptions = { + "trade": { + "maxslots": 6, + "type": 4 + }, + "items": { + "maxslots": 10, + "type": 3 + }, + "games": { + "maxslots": 4, + "type": 2 + } + }; + + if (!allowedoptions.hasOwnProperty(showcase)) { + const err = new Error("The submitted showcase type has no editable items."); + return callback ? callback(err) : undefined; + } + if (slot < 1 || slot > allowedoptions[showcase]["maxslots"]) { + const err = new Error("The submitted slot is outside of range. (Allowed range: 1-"+allowedoptions[showcase]["maxslots"]+")"); + return callback ? callback(err) : undefined; + } + if (!(item.hasOwnProperty("appid") || item.hasOwnProperty("item_contextid") || item.hasOwnProperty("item_assetid"))) { + const err = new Error("The submitted item is not valid."); + return callback ? callback(err) : undefined; + } + const requestdata = Object.assign({}, item); + requestdata["slot"] = slot - 1; + requestdata["customization_type"] = allowedoptions[showcase]["type"]; + requestdata["sessionid"] = this.getSessionID(); + this._myProfile("ajaxsetshowcaseconfig", requestdata, function (err, response, body) { + + if (err || response.statusCode != 200) { + err = err || new Error("HTTP error " + response.statusCode); + return callback ? callback(err) : undefined; + } + return callback ? callback(null) : undefined; + + }); +}; + SteamCommunity.prototype.editProfile = function(settings, callback) { var self = this; this._myProfile("edit", null, function(err, response, body) { @@ -54,16 +98,17 @@ SteamCommunity.prototype.editProfile = function(settings, callback) { } var values = {}; - form.serializeArray().forEach(function(item) { + form.serializeArray().forEach(function (item) { values[item.name] = item.value; }); - for(var i in settings) { - if(!settings.hasOwnProperty(i)) { + var remainingshowcases = $(".profile_showcase_selector").length; + + for (var i in settings) { + if (!settings.hasOwnProperty(i)) { continue; } - - switch(i) { + switch (i) { case 'name': values.personaName = settings[i]; break; @@ -103,7 +148,7 @@ SteamCommunity.prototype.editProfile = function(settings, callback) { break; case 'primaryGroup': - if(typeof settings[i] === 'object' && settings[i].getSteamID64) { + if (typeof settings[i] === 'object' && settings[i].getSteamID64) { values.primary_group_steamid = settings[i].getSteamID64(); } else { values.primary_group_steamid = new SteamID(settings[i]).getSteamID64(); @@ -111,7 +156,199 @@ SteamCommunity.prototype.editProfile = function(settings, callback) { break; - // TODO: profile showcases + + case 'showcases': + + //When supplying a new showcases array, remove the old showcase (order) + for (var val in values) { + if (val.indexOf("[") !== -1) { + if (val.split("[")[0] == "profile_showcase") + delete values[val]; + } + } + + for (var type in settings[i]) { + + if (remainingshowcases === 0) { + break; + } + + remainingshowcases--; + + switch (settings[i][type].showcase) { + + case 'infobox': + values["profile_showcase[8]"] = 8; + + if (settings[i][type].hasOwnProperty("values")) { + if (settings[i][type]["values"].hasOwnProperty("title")) { + values["rgShowcaseConfig[8][0][title]"] = settings[i][type]["values"]["title"]; + } + if (settings[i][type]["values"].hasOwnProperty("notes")) { + values["rgShowcaseConfig[8][0][notes]"] = settings[i][type]["values"]["notes"]; + } + } + + break; + + case 'artwork': + values["profile_showcase[13]"] = 13; + + if (settings[i][type].hasOwnProperty("values")) { + for (var n = 0; n < 4; n++) { + values["profile_showcase[13][" + n + "][publishedfileid]"] = settings[i][type]["values"][n] || ""; + } + } + break; + + case 'trade': + values["profile_showcase[4]"] = 4; + + if (settings[i][type].hasOwnProperty("values")) { + if (settings[i][type]["values"].hasOwnProperty("notes")) { + values["rgShowcaseConfig[4][6][notes]"] = settings[i][type]["values"]["notes"]; + } + } + break; + + case 'items': + values["profile_showcase[3]"] = 3; + break; + + case 'game': + values["profile_showcase[6]"] = 6; + + if (settings[i][type].hasOwnProperty("values")) { + values["rgShowcaseConfig[6][0][appid]"] = settings[i][type]["values"]; + } + break; + + case 'badge': + values["profile_showcase[5]"] = 5; + + if (settings[i][type].hasOwnProperty("values")) { + + if (settings[i][type]["values"].hasOwnProperty("style")) { + var styles = ["rare", "selected", null, "recent", "random"]; + values["profile_showcase_style_5"] = styles.indexOf(settings[i][type]["values"]["style"]); + } + + if (settings[i][type]["values"].hasOwnProperty("badges")) { + var types = ["badgeid", "appid", "border_color"]; + for (var n = 0; n < 6; n++) { + for (var t in types) { + if (settings[i][type]["values"]["badges"][n] != undefined) { + if (settings[i][type]["values"]["badges"][n].hasOwnProperty(types[t])) { + values["rgShowcaseConfig[5][" + n + "][" + types[t] + "]"] = settings[i][type]["values"]["badges"][n][types[t]] || values["rgShowcaseConfig[5][" + n + "][" + types[t] + "]"] || ""; + } + } + } + } + } + } + + break; + + case 'rareachievements': + values["profile_showcase[1]"] = 1; + break; + + case 'screenshot': + values["profile_showcase[7]"] = 7; + + if (settings[i][type].hasOwnProperty("values")) { + for (var n = 0; n < 4; n++) { + if (settings[i][type]["values"][n] != undefined) { + values["rgShowcaseConfig[7][" + n + "][publishedfileid]"] = settings[i][type]["values"][n]; + } + } + } + break; + + case 'group': + values["profile_showcase[9]"] = 9; + + if (settings[i][type].hasOwnProperty("values")) { + if (typeof settings[i][type]["values"] === 'object' && settings[i][type]["values"].getSteamID64) { + values["rgShowcaseConfig[9][0][accountid]"] = settings[i][type]["values"].getSteamID64(); + } else { + values["rgShowcaseConfig[9][0][accountid]"] = new SteamID(settings[i][type]["values"]).getSteamID64(); + } + } + break; + + case 'review': + values["profile_showcase[10]"] = 10; + + if (settings[i][type].hasOwnProperty("values")) { + values["rgShowcaseConfig[10][0][appid]"] = settings[i][type]["values"]; + } + break; + + case 'workshop': + values["profile_showcase[11]"] = 11; + + if (settings[i][type].hasOwnProperty("values")) { + values["rgShowcaseConfig[11][0][appid]"] = settings[i][type]["values"]["appid"]; + values["rgShowcaseConfig[11][0][publishedfileid]"] = settings[i][type]["values"]["publishedfileid"]; + } + break; + + case 'guide': + values["profile_showcase[15]"] = 15; + + if (settings[i][type].hasOwnProperty("values")) { + values["rgShowcaseConfig[15][0][appid]"] = settings[i][type]["values"]["appid"]; + values["rgShowcaseConfig[15][0][publishedfileid]"] = settings[i][type]["values"]["publishedfileid"]; + } + break; + + case 'achievements': + values["profile_showcase[17]"] = 17; + + if (settings[i][type].hasOwnProperty("values") && settings[i][type]["values"].hasOwnProperty("achievements")) { + for (var n = 0; n < 7; n++) { + if (settings[i][type]["values"]["achievements"][n] != undefined) { + values["rgShowcaseConfig[17][" + n + "][appid]"] = settings[i][type]["values"]["achievements"][n]["appid"]; + values["rgShowcaseConfig[17][" + n + "][title]"] = settings[i][type]["values"]["achievements"][n]["title"]; + } + } + } + break; + + case 'games': + values["profile_showcase[2]"] = 2; + break; + + case 'ownguides': + values["profile_showcase[16]"] = 16; + + if (settings[i][type].hasOwnProperty("values")) { + for (var n = 0; n < 4; n++) { + if (settings[i][type]["values"][n] != undefined) { + values["rgShowcaseConfig[16][" + n + "][appid]"] = settings[i][type]["values"][n]["appid"]; + values["rgShowcaseConfig[16][" + n + "][publishedfileid]"] = settings[i][type]["values"][n]["publishedfileid"]; + } + } + } + break; + + case 'ownworkshop': + values["profile_showcase[12]"] = 12; + + if (settings[i][type].hasOwnProperty("values")) { + for (var n = 0; n < 5; n++) { + if (settings[i][type]["values"][n] != undefined) { + values["rgShowcaseConfig[12][" + n + "][appid]"] = settings[i][type]["values"][n]["appid"]; + values["rgShowcaseConfig[12][" + n + "][publishedfileid]"] = settings[i][type]["values"][n]["publishedfileid"]; + } + } + } + break; + } + } + break; + } }