Better handling for missing items in user XML

This commit is contained in:
Alexander Corn 2015-09-20 00:49:42 -04:00
parent f3cf2a4361
commit 4e1453e702

View File

@ -41,7 +41,7 @@ SteamCommunity.prototype.getSteamUser = function(id, callback) {
} }
} }
if(!result.profile.steamID64 || !result.profile.onlineState) { if(!result.profile.steamID64) {
callback(new Error("No valid response")); callback(new Error("No valid response"));
return; return;
} }
@ -55,22 +55,26 @@ function CSteamUser(community, userData, customurl) {
this._community = community; this._community = community;
this.steamID = new SteamID(userData.steamID64[0]); this.steamID = new SteamID(userData.steamID64[0]);
this.name = userData.steamID[0]; this.name = processItem(userData, 'steamID');
this.onlineState = userData.onlineState[0]; this.onlineState = processItem(userData, 'onlineState');
this.stateMessage = userData.stateMessage[0]; this.stateMessage = processItem(userData, 'stateMessage');
this.privacyState = userData.privacyState[0]; this.privacyState = processItem(userData, 'privacyState', 'uncreated');
this.visibilityState = userData.visibilityState[0]; this.visibilityState = processItem(userData, 'visibilityState');
this.avatarHash = userData.avatarIcon[0].match(/([0-9a-f]+)\.[a-z]+$/)[1]; this.avatarHash = processItem(userData, 'avatarIcon', '').match(/([0-9a-f]+)\.[a-z]+$/);
this.vacBanned = !!userData.vacBanned[0]; if(this.avatarHash) {
this.tradeBanState = userData.tradeBanState[0]; this.avatarHash = this.avatarHash[1];
this.isLimitedAccount = !!userData.isLimitedAccount[0]; }
this.customURL = userData.customURL ? userData.customURL[0] : customurl;
this.vacBanned = !!processItem(userData, 'vacBanned', false);
this.tradeBanState = processItem(userData, 'tradeBanState', 'None');
this.isLimitedAccount = !!processItem(userData, 'isLimitedAccount');
this.customURL = processItem(userData, 'customURL', customurl);
if(this.visibilityState == 3) { if(this.visibilityState == 3) {
this.memberSince = new Date(userData.memberSince[0].replace(/(\d{1,2})(st|nd|th)/, "$1")); this.memberSince = new Date(processItem(userData, 'memberSince', '0').replace(/(\d{1,2})(st|nd|th)/, "$1"));
this.location = userData.location[0] || null; this.location = processItem(userData, 'location');
this.realName = userData.realname[0] || null; this.realName = processItem(userData, 'realname');
this.summary = userData.summary[0] || null; this.summary = processItem(userData, 'summary');
} else { } else {
this.memberSince = null; this.memberSince = null;
this.location = null; this.location = null;
@ -93,6 +97,14 @@ function CSteamUser(community, userData, customurl) {
return new SteamID(group.groupID64[0]); return new SteamID(group.groupID64[0]);
}); });
} }
function processItem(name, defaultVal) {
if(!userData[name]) {
return defaultVal;
}
return userData[name][0];
}
} }
CSteamUser.getAvatarURL = function(hash, size, protocol) { CSteamUser.getAvatarURL = function(hash, size, protocol) {