Permalink
Browse files

adding voting addon, fixes #18. fixed issue with afk users no longer …

…in the room
  • Loading branch information...
1 parent 67c8b78 commit f6baf14ebab88da312282afb7e9059fc035487ac Mac Angell committed Mar 8, 2013
Showing with 313 additions and 58 deletions.
  1. +4 −0 app.js
  2. +11 −0 lib/AfkDjAddOn.js
  3. +140 −58 lib/DjAddOn.js
  4. +12 −0 lib/ExtendedBot.js
  5. +146 −0 lib/VotingAddOn.js
View
@@ -73,6 +73,7 @@ var ExtendedBot = require('./lib/ExtendedBot.js'),
AfkDjAddOn = require('./lib/AfkDjAddOn.js'),
DjQueueAddOn = require('./lib/DjQueueAddOn.js'),
LastFmAddOn = require('./lib/LastFmAddOn.js'),
+ VotingAddOn = require('./lib/VotingAddOn.js'),
bot = new ExtendedBot(config.authToken, config.userId, config.roomId),
dbConnectionString = 'mongodb://';
@@ -103,6 +104,9 @@ bot.registerAddOn(DjQueueAddOn, { djSongLimit: 2 });
// register lastfm addon
bot.registerAddOn(LastFmAddOn);
+// register voting addon
+bot.registerAddOn(VotingAddOn);
+
// register afk addon
bot.registerAddOn(AfkAddOn, { afkThreshold: 60 * 15 }); // afk threshold of 15 minutes
View
@@ -17,6 +17,10 @@ module.exports = function AfkDjAddOn(bot) {
},
beginWarnSequence = function(userId) {
var djs = bot.getCurrentDjsInRoom();
+ if (!djs[userId]) {
+ delete afkDjs[userId];
+ return;
+ }
bot.speak(getInitialWarning().replace('<user>', djs[userId].name));
setTimeout(function() {
@@ -55,6 +59,11 @@ module.exports = function AfkDjAddOn(bot) {
if (afkDjs[userId]) {
delete afkDjs[userId];
}
+ },
+ deregisterHandler = function(data) {
+ if (afkDjs[data.user[0].userid]) {
+ delete afkDjs[data.user[0].userid];
+ }
};
@@ -78,12 +87,14 @@ module.exports = function AfkDjAddOn(bot) {
bot.on('afk', afkHandler);
bot.on('active', activeHandler);
bot.on('add_dj', addDjHandler);
+ bot.on('deregistered', deregisterHandler);
};
this.disable = function() {
bot.removeListener('afk', afkHandler);
bot.removeListener('active', activeHandler);
bot.removeListener('add_dj', addDjHandler);
+ bot.removeListener('deregistered', deregisterHandler);
afkDjs = {};
};
View
@@ -2,13 +2,18 @@
module.exports = function DjAddOn(bot) {
- var playlistName = 'default',
+ var self = this,
+ playlistName = 'default',
emptyPlaylistName = 'empty',
+ voteInProgress = false,
+ activePlaylist,
+ votePlayed = true,
_ = require('underscore'),
initialize = function() {
bot.on('connect', function() {
bot.playlistListAll(function(data) {
if (data.success) {
+ activePlaylist = _.find(data.list, function(list) { return list.active; }).name;
if (!_.some(data.list, function(list) { return list.name === emptyPlaylistName; })) {
bot.playlistCreate(emptyPlaylistName);
}
@@ -107,72 +112,87 @@ module.exports = function DjAddOn(bot) {
});
},
snagSong = function(msgData, issuerId, replyFunc) {
- var song = bot.getCurrentSong();
- if (song) {
- addToPlaylistEndNoSwitch(playlistName, song._id, function(err, data) {
- if (err) {
- return replyFunc('Sorry, there was a problem adding the song to my playlist.');
- }
- replyFunc('Ok, I\'ve added "' + song.song + '" to my playlist.');
- });
+ if (!voteInProgress || !self.options.voteForNextSongEnabled.value) {
+ var song = bot.getCurrentSong();
+ if (song) {
+ addToPlaylistEndNoSwitch(playlistName, song._id, function(err, data) {
+ if (err) {
+ return replyFunc('Sorry, there was a problem adding the song to my playlist.');
+ }
+ replyFunc('Ok, I\'ve added "' + song.song + '" to my playlist.');
+ });
+ }
+ else {
+ replyFunc('Sorry, there is no song playing at the moment.');
+ }
}
else {
- replyFunc('Sorry, there is no song playing at the moment.');
+ replyFunc('Please wait until the voting is over to snag this song.');
}
},
dropSong = function(msgData, issuerId, replyFunc) {
- var song = bot.getCurrentSong();
- if (song) {
- removeFromPlaylistNoSwitch(playlistName, song._id, function(err, data) {
- if (err) {
- if (err.message === 'Not Found') {
- return replyFunc('Sorry, I could not find this song in my playlist.');
+ if (!voteInProgress || !self.options.voteForNextSongEnabled.value) {
+ var song = bot.getCurrentSong();
+ if (song) {
+ removeFromPlaylistNoSwitch(playlistName, song._id, function(err, data) {
+ if (err) {
+ if (err.message === 'Not Found') {
+ return replyFunc('Sorry, I could not find this song in my playlist.');
+ }
+ return replyFunc('Sorry, there was a problem removing the song from my playlist.');
}
- return replyFunc('Sorry, there was a problem removing the song from my playlist.');
- }
- replyFunc('Ok, I\'ve removed "' + song.song + '" from my playlist.');
- });
+ replyFunc('Ok, I\'ve removed "' + song.song + '" from my playlist.');
+ });
+ }
+ else {
+ replyFunc('Sorry, there is no song playing at the moment.');
+ }
}
else {
- replyFunc('Sorry, there is no song playing at the moment.');
+ replyFunc('Please wait until the voting is over to drop this song from my playlist.');
}
},
wipeSongs = function(msgData, issuerId, replyFunc) {
- bot.playlistListAll(function(data) {
- if (data.success) {
- bot.playlistAll(playlistName, function(playlistData) {
- if (playlistData.success) {
- if (playlistData.list.length < 1) {
- replyFunc('My playlist is currently empty.');
+ if (!voteInProgress || !self.options.voteForNextSongEnabled.value) {
+ bot.playlistListAll(function(data) {
+ if (data.success) {
+ bot.playlistAll(playlistName, function(playlistData) {
+ if (playlistData.success) {
+ if (playlistData.list.length < 1) {
+ replyFunc('My playlist is currently empty.');
+ }
+ var active = _.find(data.list, function(list) {
+ return list.active;
+ });
+ for (var i = 0; i < playlistData.list.length; i++) {
+ (function(idx, isLast) {
+ setTimeout(function() {
+ if (isLast) {
+ bot.playlistRemove(playlistName, 0, function() {
+ bot.playlistSwitch(active.name);
+ });
+ }
+ else {
+ bot.playlistRemove(playlistName, 0);
+ }
+ }, 500*idx);
+ }(i, (i===playlistData.list.length-1)));
+ }
+ replyFunc('My playlist is in the process of being cleared.');
}
- var active = _.find(data.list, function(list) {
- return list.active;
- });
- for (var i = 0; i < playlistData.list.length; i++) {
- (function(idx, isLast) {
- setTimeout(function() {
- if (isLast) {
- bot.playlistRemove(playlistName, 0, function() {
- bot.playlistSwitch(active.name);
- });
- }
- else {
- bot.playlistRemove(playlistName, 0);
- }
- }, 500*idx);
- }(i, (i===playlistData.list.length-1)));
+ else {
+ replyFunc('Sorry, there was a problem removing all songs from my playlist.');
}
- replyFunc('My playlist is in the process of being cleared.');
- }
- else {
- replyFunc('Sorry, there was a problem removing all songs from my playlist.');
- }
- });
- }
- else {
- replyFunc('Sorry, there was a problem removing all songs from my playlist.');
- }
- });
+ });
+ }
+ else {
+ replyFunc('Sorry, there was a problem removing all songs from my playlist.');
+ }
+ });
+ }
+ else {
+ replyFunc('Please wait until the voting is over to wipe my playlist.');
+ }
},
announceTopSongs = function(msgData, issuerId, replyFunc) {
getPlaylistInfoNoSwitch(playlistName, function(err, data) {
@@ -205,9 +225,7 @@ module.exports = function DjAddOn(bot) {
togglePlaylist = function(msgData, issuerId, replyFunc) {
bot.playlistListAll(function(data) {
if (data.success) {
- var to, active = _.find(data.list, function(list) {
- return list.active;
- });
+ var to, active = _.find(data.list, function(list) { return list.active; });
if (active.name === playlistName) {
to = emptyPlaylistName;
bot.playlistSwitch(emptyPlaylistName);
@@ -216,9 +234,64 @@ module.exports = function DjAddOn(bot) {
to = playlistName;
bot.playlistSwitch(playlistName);
}
+ activePlaylist = active.name;
replyFunc('Switched to playlist: ' + to);
}
});
+ },
+ newSongHandler = function(data) {
+ if (data.success && self.options.voteForNextSongEnabled.value) {
+ if (data.room.metadata.current_dj === bot.userId) {
+ votePlayed = true;
+ }
+ if (!voteInProgress && activePlaylist === playlistName && votePlayed) {
+ var djs = data.room.metadata.djs,
+ currentDjPos = djs.indexOf(data.room.metadata.current_dj),
+ botPos = djs.indexOf(bot.userId),
+ isUpNext;
+ if (botPos > -1) {
+ isUpNext = (botPos === (currentDjPos + 1) || (botPos === 0 && currentDjPos === (djs.length-1)));
+ if (isUpNext) {
+ voteInProgress = new Date().getTime() + '-dj';
+ votePlayed = false;
+ getPlaylistInfoNoSwitch(playlistName, function(err, data) {
+ if (data.list.length < 1) {
+ return;
+ }
+ var choices = _.first(_.shuffle(_.first(data.list, data.list.length-2)), 3),
+ choiceIndeces = _.map(choices, function(c) { return _.indexOf(data.list, c)}),
+ choiceNames = _.map(choices, function(c) { return '"' + c.metadata.song + '" by ' + c.metadata.artist; }),
+ endVoteHandler = function(voteId, winner) {
+ if (voteId === voteInProgress) {
+ bot.removeListener('endVote', endVoteHandler);
+ bot.removeListener('voteIgnored', ignoredVoteHandler);
+ var playlistIndex = choiceIndeces[choiceNames.indexOf(winner.name)];
+ bot.playlistReorder(playlistName, playlistIndex, 0);
+ voteInProgress = false;
+ }
+ },
+ ignoredVoteHandler = function(voteId) {
+ if (voteId === voteInProgress) {
+ bot.speak('Since there is currently a vote in progress, I will not begin a vote for my next song.');
+ bot.removeListener('endVote', endVoteHandler);
+ bot.removeListener('voteIgnored', ignoredVoteHandler);
+ voteInProgress = false;
+ }
+ };
+
+ setTimeout(function() {
+ bot.on('endVote', endVoteHandler);
+ bot.on('voteIgnored', ignoredVoteHandler);
+ bot.emit('beginVote', voteInProgress, 'Which song should I play next?', choiceNames);
+ }, 5000);
+
+
+ });
+ }
+ }
+ }
+
+ }
};
this.name = 'dj';
@@ -277,11 +350,20 @@ module.exports = function DjAddOn(bot) {
}
];
+ this.options = {
+ voteForNextSongEnabled: {
+ value: true,
+ type: Boolean,
+ description: 'A flag used to control whether or not users can vote on the next song that I play.'
+ }
+ };
+
this.enable = function() {
-
+ bot.on('newsong', newSongHandler);
};
this.disable = function() {
+ bot.removeListener('newsong', newSongHandler);
};
initialize();
View
@@ -547,6 +547,18 @@ function ExtendedBot(auth, userId, roomId) {
};
/*
+ Gets the collection of add on names that are registered with the bot and enabled.
+
+ Params:
+ None
+ Returns:
+ An arry containing the names of the add ons that are enables
+ */
+ this.getEnabledAddOns = function() {
+ return _.filter(_.keys(addOns), function(name) { return addOns[name].isEnabled; });
+ };
+
+ /*
Enables the specified add on.
Params:
Oops, something went wrong.

0 comments on commit f6baf14

Please sign in to comment.