diff --git a/modules/steam/steam.js b/modules/steam/steam.js index 1d1fd25..58ebaa4 100644 --- a/modules/steam/steam.js +++ b/modules/steam/steam.js @@ -77,53 +77,108 @@ var steam = function(dbot) { callback(true, null); } }); + }, + + 'getLibrary': function(steamid, callback) { + request.get(this.ApiRoot + 'IPlayerService/GetOwnedGames/v0001/', { + 'qs': { + 'key': this.config.api_key, + 'steamid': steamid, + 'format': 'json', + 'include_appinfo': 1 + }, + 'json': true + }, function(err, res, body) { + if(_.has(body, 'response') && _.has(body.response, 'game_count')) { + callback(null, body.response); + } else { + callback(true, null); + } + }); } }; this.commands = { + '~games': function(event) { + var u1 = event.rUser, + s1 = event.rProfile.sid, + u2 = event.res[0].user, + s2 = event.res[0].sid; + + this.api.getLibrary(s1, function(err, l1) { + if(!err) { + this.api.getLibrary(s2, function(err, l2) { + if(!err) { + var g1 = _.pluck(l1.games, 'name'), + g2 = _.pluck(l2.games, 'name'), + common = _.filter(g1, function(game) { + return _.include(g2, game); + }), + examples = []; + _.times(5, function() { + var choice = _.random(0, common.length - 1); + if(!_.include(examples, common[choice])) { + examples.push(common[choice]); + } + }); + event.reply(dbot.t('steam_games', { + 'u1': u1.currentNick, + 'u2': u2.currentNick, + 'count': common.length, + 'examples': examples.join(', ') + })); + } else { + event.reply('Unable to fetch steam library :\'('); + } + }); + } else { + event.reply('Unable to fetch steam library :\'('); + } + }.bind(this)); + + }, + '~game': function(event) { var user = event.rUser, - snick = event.rProfile.steam; + snick = event.rProfile.steam, + sid = event.rProfile.sid; if(event.res[0]) { user = event.res[0].user; snick = event.res[0].snick; + sid = event.res[0].sid; } - this.api.getSteamID(snick, function(err, steamid) { - this.api.getProfile(steamid, function(err, player) { - if(!err && player) { - if(_.has(player, 'gameextrainfo')) { - var output = dbot.t('steam_currently_playing', { - 'user': user.currentNick, - 'game': player.gameextrainfo - }); - if(_.has(player, 'gameserverip')) { - var host = player.gameserverip.split(':'); - output += ' (Server: ' + host[0] + ' Port: ' + host[1] + ')'; - } - event.reply(output); - } else { - this.api.getRecentlyPlayed(steamid, function(err, games) { - if(!err) { - if(games.total_count != 0) { - event.reply(dbot.t('steam_last_played', { - 'user': user.currentNick, - 'game': games.games[0].name - })); - } else { - event.reply(dbot.t('steam_not_played', { - 'user': user.currentNick - })); - } - } else { - event.reply('Unknown Steam Username'); - } - }); + this.api.getProfile(sid, function(err, player) { + if(!err && player) { + if(_.has(player, 'gameextrainfo')) { + var output = dbot.t('steam_currently_playing', { + 'user': user.currentNick, + 'game': player.gameextrainfo + }); + if(_.has(player, 'gameserverip')) { + var host = player.gameserverip.split(':'); + output += ' (Server: ' + host[0] + ' Port: ' + host[1] + ')'; } + event.reply(output); } else { - event.reply('Unknown Steam Username'); + this.api.getRecentlyPlayed(sid, function(err, games) { + if(!err) { + if(games.total_count != 0) { + event.reply(dbot.t('steam_last_played', { + 'user': user.currentNick, + 'game': games.games[0].name + })); + } else { + event.reply(dbot.t('steam_not_played', { + 'user': user.currentNick + })); + } + } else { + event.reply('Unknown Steam Username'); + } + }); } - }.bind(this)); + } }.bind(this)); } }; @@ -131,26 +186,41 @@ var steam = function(dbot) { _.each(this.commands, function(command) { command.resolver = function(event, callback) { if(event.rProfile && _.has(event.rProfile, 'steam')) { - if(event.params[1]) { - this.internalAPI.getSteam(event.server, event.params[1], function(user, snick) { - if(user && snick) { - event.res.push({ - 'user': user, - 'snick': snick - }); - callback(false); + this.api.getSteamID(event.rProfile.steam, function(err, sid) { + if(!err) { + event.rProfile.sid = sid; // oh god ew + if(event.params[1]) { + this.internalAPI.getSteam(event.server, event.params[1], function(user, snick) { + if(user && snick) { + this.api.getSteamID(snick, function(err, sid) { + if(!err) { + event.res.push({ + 'user': user, + 'snick': snick, + 'sid': sid + }); + callback(false); + } else { + event.reply('Unknown Steam ID'); + callback(true); + } + }); + } else { + if(!user) { + event.reply('Unknown user.'); + } else { + event.reply(user.currentNick + ': Set a steam "vanity url" with "~set steam username"'); + } + callback(true); + } + }.bind(this)); } else { - if(!user) { - event.reply('Unknown user.'); - } else { - event.reply(user.currentNick + ': Set a steam "vanity url" with "~set steam username"'); - } - callback(true); + callback(false); } - }); - } else { - callback(false); - } + } else { + event.reply('Unknown Steam ID'); + } + }.bind(this)); } else { event.reply(event.user + ': Set a steam "vanity url" with "~set steam username"'); callback(true); diff --git a/modules/steam/strings.json b/modules/steam/strings.json index d0bf968..a0598e7 100644 --- a/modules/steam/strings.json +++ b/modules/steam/strings.json @@ -7,5 +7,8 @@ }, "steam_currently_playing": { "en": "{user} is currently playing {game}" + }, + "steam_games": { + "en": "{u1} and {u2} have {count} games in common, including: {examples}" } }