Chiddix

Turntable auto awesome

Jul 9th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.   if(window.izzmo == undefined) window.izzmo = { };
  3.  
  4.   window.izzmo = $.extend(window.izzmo, {
  5.     ttObj: null,
  6.     awesomer: null,
  7.     showArc: true,
  8.     lamed: false,
  9.     arcInt: 0,
  10.     deg: 0.0,
  11.     vote: function(c) {
  12.       var f = $.sha1(window.izzmo.ttObj.roomId + c + window.izzmo.ttObj.currentSong._id);
  13.       var d = $.sha1(Math.random() + "");
  14.       var e = $.sha1(Math.random() + "");
  15.       window.izzmo.socket({
  16.           api: "room.vote",
  17.           roomid: window.izzmo.ttObj.roomId,
  18.           val: c,
  19.           vh: f,
  20.           th: d,
  21.           ph: e
  22.       });
  23.     },
  24.     socket: function (c, a) {
  25.         if (c.api == "room.now") {
  26.             return;
  27.         }
  28.         c.msgid = turntable.messageId;
  29.         turntable.messageId += 1;
  30.         c.clientid = turntable.clientId;
  31.         if (turntable.user.id && !c.userid) {
  32.             c.userid = turntable.user.id;
  33.             c.userauth = turntable.user.auth;
  34.         }
  35.         var d = JSON.stringify(c);
  36.         if (turntable.socketVerbose) {
  37.             LOG(util.nowStr() + " Preparing message " + d);
  38.         }
  39.         var b = $.Deferred();
  40.         turntable.whenSocketConnected(function () {
  41.             if (turntable.socketVerbose) {
  42.                 LOG(util.nowStr() + " Sending message " + c.msgid + " to " + turntable.socket.host);
  43.             }
  44.             if (turntable.socket.transport.type == "websocket") {
  45.                 turntable.socketLog(turntable.socket.transport.sockets[0].id + ":<" + c.msgid);
  46.             }
  47.             turntable.socket.send(d);
  48.             turntable.socketKeepAlive(true);
  49.             turntable.pendingCalls.push({
  50.                 msgid: c.msgid,
  51.                 handler: a,
  52.                 deferred: b,
  53.                 time: util.now()
  54.             });
  55.         });
  56.         return b.promise();
  57.     },
  58.     listener: function(d) {
  59.       if(d.command == "newsong" && d.room.metadata.current_dj != izzmo.ttObj.selfId) {
  60.         clearTimeout(window.izzmo.awesomer);
  61.         clearInterval(window.izzmo.arcInt);
  62.         window.izzmo.lamed = false;
  63.         var timeAmt = Math.floor(Math.random()*window.izzmo.ttObj.currentSong.metadata.length/2*1000);
  64.         window.izzmo.botMessage.find('span').html('');
  65.         window.izzmo.awesomer = setTimeout(function() {
  66.           window.izzmo.vote('up');
  67.         }, timeAmt);
  68.        
  69.         if(!window.izzmo.showArc) return;
  70.  
  71.         window.izzmo.deg = 0.0;
  72.         window.izzmo.degAmt = 180 / timeAmt * 55;
  73.         if(window.izzmo.arcInt != 0) {
  74.           clearInterval(window.izzmo.arcInt);
  75.           window.izzmo.arcInt = 0;
  76.         }
  77.         window.izzmo.arcInt = setInterval(function() {
  78.           if(window.izzmo.deg >= 180) {
  79.             clearInterval(window.izzmo.arcInt);
  80.             window.izzmo.arcInt = 0;
  81.           }
  82.           window.izzmo.setArc(window.izzmo.deg, false);
  83.           window.izzmo.deg += window.izzmo.degAmt;
  84.         }, 50);
  85.       }
  86.     },
  87.     setArc: function(degree, red) {
  88.       if(!window.izzmo.showArc) return;
  89.       var context = window.izzmo.arc[0].getContext('2d');
  90.       context.clearRect(0, 0, 1000, 500);
  91.       context.beginPath();
  92.       context.arc(203, 162, 137, -Math.PI, degree*Math.PI/180 - Math.PI, false);
  93.       context.lineWidth = 5;
  94.       if(red)
  95.         context.strokeStyle = 'rgb(255, 0, 0)';
  96.       else
  97.         context.strokeStyle = 'rgb(0, 200, 0)';
  98.       context.shadowOffsetX = 0;
  99.       context.shadowOffsetY = 0;
  100.       context.shadowBlur = 10;
  101.       if(red)
  102.         context.shadowColor = 'rgba(255, 0, 0, 1)';
  103.       else
  104.         context.shadowColor = 'rgba(0, 255, 0, 1)';
  105.       context.stroke();
  106.     },
  107.     room: '',
  108.     watcher: null,
  109.     stop: function() {
  110.       clearTimeout(window.izzmo.awesomer);
  111.       clearInterval(window.izzmo.arcInt);
  112.       window.izzmo.arcInt = 0;
  113.     },
  114.     init: function() {
  115.       $('.roomView').ready(function() {
  116.         for(var prop in window.turntable) {
  117.           if(window.turntable[prop] != undefined && window.turntable[prop].hasOwnProperty('currentDj'))
  118.             window.izzmo.ttObj = window.turntable[prop];
  119.         }
  120.         if(window.izzmo.ttObj === null) {
  121.           alert('Could not find turntable.fm objects. You should refresh your page and try again.');
  122.           return;
  123.         }
  124.         window.izzmo.room = window.location.pathname;
  125.         var meterObj = $('#meterGauge');
  126.         if(meterObj.length > 0 && meterObj.css('display') != 'none') {
  127.           var meter = $('#meterGauge').position();
  128.           window.izzmo.arc = $('<canvas id="izzmo-arc" width="406" height="158" style="overflow: hidden; position: absolute; z-index: 20000; top: ' + meter.top + 'px; left: ' + meter.left + 'px;">Izzmo\'s AutoAwesome</canvas>');
  129.           $($('.roomView > div')[1]).prepend(window.izzmo.arc);
  130.           window.izzmo.showArc = true;
  131.         }
  132.         else
  133.           window.izzmo.showArc = false;
  134.  
  135.         window.izzmo.botMessage = $('<div id="bot-message">Izzmo\'s AutoAwesome. <span style="font-style: italic;"></span> <a href="#" style="text-decoration: none; color: red; font-weight: bold;">Turn off</a></div>');
  136.         window.izzmo.botMessage.css({
  137.           position: 'fixed',
  138.           color: 'white',
  139.           top: '0px',
  140.           zIndex: '5000',
  141.           textAlign: 'left',
  142.           paddingLeft: '2px',
  143.           paddingTop: '2px',
  144.           paddingRight: '3px',
  145.           paddingBottom: '2px',
  146.           fontSize: '10px',
  147.           fontFace: 'Verdana'
  148.         });
  149.  
  150.         if(!$('#bot-message').length) {
  151.           $('.header').append(window.izzmo.botMessage);
  152.  
  153.           window.izzmo.botMessage.find('a').click(function(e) {
  154.             e.preventDefault();
  155.             window.izzmo.destruct();
  156.             window.turntable.removeEventListener("message", window.izzmo.listener);
  157.             window.izzmo = null;
  158.           });
  159.         }
  160.  
  161.         var buttons = $('.roomView > div:nth-child(2) a[id]'); // 1st is Awesome button, 2nd is Lame
  162.         $(buttons[1]).unbind(); // cancel TT's default callback for the button, add in our own.
  163.         $(buttons[1]).bind('click', function() {
  164.           if(!window.izzmo.lamed) {
  165.             window.izzmo.vote('up');
  166.             window.izzmo.stop();
  167.             window.izzmo.botMessage.find('span').html("Song lamed! Awesomering will resume next song.");
  168.             window.izzmo.lamed = true;
  169.           }
  170.           window.izzmo.setArc(180, true);
  171.           setTimeout(function() {window.izzmo.vote('down');}, 250);
  172.         });
  173.         $(buttons[0]).bind('click', function() {
  174.             window.izzmo.stop();
  175.             window.izzmo.setArc(180, false);    
  176.         });
  177.  
  178.         turntable.addEventListener("message", window.izzmo.listener);
  179.         window.izzmo.vote('up');
  180.         window.izzmo.setArc(180, false);
  181.  
  182.         // Timer for resetting TurnTable's AFK Timers
  183.         // Runs every 60 seconds
  184.         window.izzmo.botResetAFKTimer = setInterval(function() {
  185.           $($('form input:last')[0]).keydown();
  186.         }, 60000);
  187.  
  188.         window.izzmo.watcher = setInterval(function() {
  189.           if(window.location.pathname != window.izzmo.room) {
  190.             console.log('New Room found, reinitializing...');
  191.             window.izzmo.destruct();
  192.             if(window.izzmo.showArc) {
  193.               var meterObj = $('#meterGauge');
  194.               var check = setInterval(function() {
  195.                 if(meterObj.length > 0 && meterObj.css('display') != 'none') {
  196.                   window.izzmo.init();
  197.                   clearInterval(check);
  198.                 }
  199.               }, 1000);
  200.               setTimeout(function() {clearInterval(check);}, 10000);
  201.             }
  202.           }
  203.         }, 3000);
  204.       });
  205.     },
  206.     destruct: function() {
  207.       clearInterval(window.izzmo.botResetAFKTimer);
  208.       clearInterval(window.izzmo.watcher);
  209.       window.izzmo.stop();
  210.       window.izzmo.arc.remove();
  211.       window.izzmo.botMessage.remove();
  212.     }
  213.   });
  214.  
  215.   window.izzmo.init();
  216. });
Advertisement
Add Comment
Please, Sign In to add comment