alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
317 stars 98 forks source link

no song events #114

Closed samuri51 closed 11 years ago

samuri51 commented 11 years ago

what does this actually mean? like i know it means no song is playing. however there is this glitch that turntable has where sometimes it allows the song to playout in its enterity causing no song to be played and not moving onto the next dj. it only gets fixed when the current dj that its stuck on skips their song. so if i wanted to set a condition for if it gets stuck like that would i do it in no song or end song? i thought of just adding in like a clause that says if the bot is the current dj during an end song event then it should skip, however if it is the only one djing then it would get in an infinite loop of skipping, and it is very necessary to not have it freeze up when it is the only one playing because other djs cant do anything to skip its song.

Izzmo commented 11 years ago

nosong is fired from the Turntable.fm servers after a endsong and implies that there is no one else on deck to start playing a song.

This happens when a room is empty or the dj slots become completely available. It has nothing to do with glitching out.

samuri51 commented 11 years ago

alright so if a song runs its full length and causes the stage to freeze would that be considered end song? or does the song literally have to finish

Izzmo commented 11 years ago

I assume it would not send the nosong event, but instead the endsong event. I assume the stage freezing is because there is a large gap between a endsong and newsong event. I haven't ever tested this though.

samuri51 commented 11 years ago

the stage freezes when a song is allowed to run its full length (the yellow bar in your queue that moves not the actual song itself) its a common problem turntable is supposed to skip before the yellow bar gets maxed out but sometimes it doesnt and the only way to move on to the next song is to have the stuck dj manually skip their song

Izzmo commented 11 years ago

That's not the problem, song changes occur with the two events, and it just means there is a delay between them instead of almost instantaneous.

samuri51 commented 11 years ago

would the delay cause it freeze completely though? i mean you come back 5 hours later and its still frozen

Izzmo commented 11 years ago

Possibly, usually long freeze events are because something went wrong in the server and your connection to it.. so this is why a lot of times, unless the server is down, refreshing will fix it.

technobly commented 11 years ago

FWIW: Turntable only sends a newsong event, not endsong. Endsong is fiction made by the ttapi to give developers peace of mind and an area to process stuff. Keep in mind when endsong fires in your bot, newsong has already fired on TT.FM and has started playing.

That said, it would be pretty easy to create a timeout routine like this... I'm going to implement this and see how well it works. Let me know what you think. It basically warns, then removes the stuck DJ if the new song never comes.

global.curSongWatchdog = null;
global.takedownTimer = null;
global.lastdj = null;

bot.on('newsong', function (data){ 
  var length = data.room.metadata.current_song.metadata.length;

  // If watch dog has been previously set, 
  // clear since we've made it to the next song
  if(curSongWatchdog != null) {
    clearTimeout(curSongWatchdog);
    curSongWatchdog = null;
  }

  // If takedown Timer has been set, 
  // clear since we've made it to the next song
  if(takedownTimer != null) {
    clearTimeout(takedownTimer);
    takedownTimer = null;
    bot.speak("@"+theUsersList[lastdj].name+", Thanks buddy ;-)");
    bot.pm(theUsersList[lastdj].name+" "+lastdj+" SONG WAS STUCK and they SKIPPED :-) ",config.admin.userid);
  }

  // Set this after processing things from last timer calls
  lastdj = data.room.metadata.current_dj;

  // Set a new watchdog timer for the current song.
  curSongWatchdog = setTimeout( function() {
    curSongWatchdog = null;
    bot.speak("@"+theUsersList[lastdj].name+", you have 10 seconds to skip your stuck song before you are removed");
    //START THE 10 SEC TIMER
    takedownTimer = setTimeout( function() {
      takedownTimer = null;
      bot.remDj(lastdj); // Remove Saved DJ from last newsong call
      bot.pm(theUsersList[lastdj].name+" "+lastdj+" SONG WAS STUCK and they got REMOVED :-(",config.admin.userid);
    }, 10 * 1000); // Current DJ has 10 seconds to skip before they are removed
  }, (length + 10) * 1000); // Timer expires 10 seconds after the end of the song, if not cleared by a newsong  

});

Only thing not shown is how the theUsersList[lastdj].name is implemented... this has been done numerous ways.

EDIT 1: fixed the bot.remDj(); call

EDIT 2: I added two lines to let me know when stuck songs are occurring. The bot will PM me with what happened and the user's name and id just for good measure. config.admin.userid is your userid, which can be entered anyway you see fit.

EDIT 3: Removed "if(curSongWatchdog == null) {" since it's always true at that point in the code.

Izzmo commented 11 years ago

Yeah, @DubbyTT is correct, I assumed a timer was involved, but after looking at the code, I guess it just calls this event to set the temp data and then immediately calls the next event.

Probably would be better to set the endsong as an actual endsong event with some sort of timing in place, but I guess it doesn't really matter.

samuri51 commented 11 years ago

i'll give this a go thanks for postin

samuri51 commented 11 years ago

so i'm kind of confused about this, its supposed to tell them they have 10 seconds to skip right? how does it differentiate between a stuck song and a nonstuck song? i can see it uses a timer but how is it that its not warning people every song?

samuri51 commented 11 years ago

i see it says if(cursongwatchdog == null) where does it not become equal to null? just curious

technobly commented 11 years ago

Since the curSongWatchdog timer is set to the song length plus 10 seconds, it will expire 10 seconds after the next song starts playing theoretically... but normally when the songs don't get stuck the newsong routine is called again and the curSongWatchdog timer is cleared 10 seconds before it is set to expire. Therefore the warning never comes.

curSongWatchdown != null when it's set with "curSongWatchdog = setTimeout".

Then when curSongWatchdog timer expires, the handler is set back to null explicitly so we can easily tell when a timer is running or not.

Give it a go and let me know if I got it right :-)

samuri51 commented 11 years ago

ahh i see now thanks for clearin that up, yeah i'm testin it as we speak : ) looks like itll work but cant tell until that event actually occurs lol, it usually gets stuck in the middle of the night so ill check the log in the morning

technobly commented 11 years ago

I was thinking about that.. if you change "(length + 10) * 1000);" to "(length - 30) * 1000);" it would at least do all of the messages before the song ends... and you could still test by skipping a normal song :-) You are basically creating a virtual endpoint for the song... as if it's stuck 30 seconds before it really ends.

samuri51 commented 11 years ago

true true.... i'll give it a go

samuri51 commented 11 years ago

looks like it works :D thanks man, only thing you forgot was the bot.remDj, i tested if it removed if they didnt skip and if it said thanks buddy if they did on more than one person and it worked so ya

technobly commented 11 years ago

:heart_eyes_cat: bot.remDj(lastdj); // Remove Saved DJ from last newsong call ^ FYI

technobly commented 11 years ago

Oh haha, just saw your comment. Thanks for testing. I'm doing that now as well just found the error. Pretty sweet no more stuck songs! :)

technobly commented 11 years ago

EDIT 2: I added two lines to let me know when stuck songs are occurring. The bot will PM me with what happened and the user's name and id just for good measure. config.admin.userid is your userid, which can be entered anyway you see fit. (see above)

MikeWills commented 11 years ago

If you have working code. How about creating a page in the the wiki for others to use with example code?

technobly commented 11 years ago

EDIT 3: Removed "if(curSongWatchdog == null) {" since it's always true at that point in the code.

technobly commented 11 years ago

I was thinking about doing that Mike, right after it works one time for real :) Come on gimme a stuck song!!!!

MikeWills commented 11 years ago

Thanks. I love working with people to get some of these features working. I have been working on getting a really good bullet proof DJ queue/list working. I'll add something to the wiki on that when that is working.

alaingilbert commented 11 years ago

Wow, thanks guys, you're freaking awesome :D

technobly commented 11 years ago

This script has been working great for me, so I added it to the wiki. https://github.com/alaingilbert/Turntable-API/wiki/Stuck-Song-Detection-and-Correction