From f1289e700e601b89ff0c0738ff5dcaf02cc48df5 Mon Sep 17 00:00:00 2001 From: Brandon Cornejo Date: Tue, 25 Feb 2014 14:50:37 -0600 Subject: [PATCH] Fixing twitch.tv chat changes, added global ignore list. --- lib/currency.js | 126 +++++++++++++++++++++----------------- lib/initialize.js | 3 +- lib/irc.js | 3 +- web/templates/ladder.jade | 6 +- 4 files changed, 77 insertions(+), 61 deletions(-) diff --git a/lib/currency.js b/lib/currency.js index d173966..ec7ae4c 100644 --- a/lib/currency.js +++ b/lib/currency.js @@ -75,6 +75,7 @@ function Currency(irc, db, options) { __self.config.website = options.website || ''; __self.config.modpowers = options.modpowers || false; __self.config.chatterbonus = options.chatterbonus || false; + __self.config.ignorelist = options.ignorelist || ['jtv']; // general settings __self.pre_text = '> ' + __self.config.currency + ': '; @@ -492,7 +493,7 @@ Currency.prototype.get_rank = function (data) { __self.query_rank(__self.rank_flood, function (ranks, total) { var response = '> Rank (of '+total+'): '; for(var i = 0; i < ranks.length; i++) { - response += ranks[i]['user'] + ' ('+ranks[i]['@row_number:=@row_number+1']+'), '; + response += ranks[i]['user'] + ' ('+ranks[i]['@row_number:=@row_number+1']+') ['+ranks[i]['points']+'], '; } response = response.slice(1,-2); __self.irc.emit('message', {message: response, timer: 1}); @@ -691,6 +692,7 @@ Currency.prototype.handout_coins = function () { response.on('end', function () { var json = JSON.parse(body); __self.streaming = json.stream !== null; + if (__self.streaming && __self.give_coins === false) { insert_coins(); } @@ -739,68 +741,21 @@ Currency.prototype.handout_coins = function () { if (__self.give_coins) { var time = utils.make_interval(__self.give_coins_timer); if (time === 0) { - __self.irc.raw('WHO ' + __self.irc.config.channel); - setTimeout(insert_coins, 1000); - } else { - setTimeout(insert_coins, time); - } - } - } - - // monitor viewers in irc - __self.irc.on('data', function (data) { - if (__self.streaming) { - var data_split = data.split(' '), viewer = ''; - - // viewers from \who - if (data_split[3] == '352') { - if (data_split[6] !== undefined) { - viewer = data_split[6].toLowerCase(); - if (__self.viewer_list.indexOf(viewer) < 0) { - __self.viewer_list.push(viewer); - } - } - } - - // viewers chatting - if (data_split[3] == 'PRIVMSG') { - var servernick = data_split[2].toLowerCase().split('!'); - viewer = servernick[0]; - if (viewer != __self.irc.config.name.toLowerCase()) { - viewer = viewer.slice(1); - } - if (__self.viewer_list.indexOf(viewer) < 0) { - __self.viewer_list.push(viewer); - } - if (__self.active_list.indexOf(viewer) < 0) { - __self.active_list.push(viewer); - } - } - - // give coins after \who and handout_coins is true - if (__self.give_coins && data_split[3] == '315') { - var clone_viewer_list = __self.viewer_list; - var clone_active_list = __self.active_list; - - // clear old list and start recording - __self.viewer_list = []; - __self.active_list = []; - // build sql from the saved viewer list var sql = ''; - for (var i = 0; i < clone_viewer_list.length; i++) { - var currency_amount = __self.subscribers.indexOf(clone_viewer_list[i]) >= 0 ? 2 : 1; + for (var i = 0; i < __self.viewer_list.length; i++) { + var currency_amount = __self.subscribers.indexOf(__self.viewer_list[i]) >= 0 ? 2 : 1; if (__self.config.chatterbonus){ - if(clone_active_list.indexOf(clone_viewer_list[i])) currecy_amount++; + if(active_list.indexOf(__self.viewer_list[i])) currency_amount++; } - if (clone_viewer_list[i] !== '') { - if (i != clone_viewer_list.length - 1) { + if (__self.viewer_list[i] !== '') { + if (i != __self.viewer_list.length - 1) { sql += 'INSERT INTO viewers (user, points) '; - sql += 'VALUES (\'' + clone_viewer_list[i] + '\', ' + currency_amount + ') '; + sql += 'VALUES (\'' + __self.viewer_list[i] + '\', ' + currency_amount + ') '; sql += 'ON DUPLICATE KEY UPDATE points = points + ' + currency_amount + '; '; } else { sql += 'INSERT INTO viewers (user, points) '; - sql += 'VALUES (\'' + clone_viewer_list[i] + '\', ' + currency_amount + ') '; + sql += 'VALUES (\'' + __self.viewer_list[i] + '\', ' + currency_amount + ') '; sql += 'ON DUPLICATE KEY UPDATE points = points + ' + currency_amount; } } @@ -808,9 +763,66 @@ Currency.prototype.handout_coins = function () { // execute query __self.db.execute(sql, function () {}); + setTimeout(insert_coins, 1000); + } else { + setTimeout(insert_coins, time); + } + } + } + + // monitor viewers in irc + __self.irc.on('data', function (data) { + var data_split = data.split(' '), viewer = ''; + + // Processing /names + if (data_split[3] == '353'){ + viewers = data_split.splice(7); + viewers.forEach(function(name) { + if(name.charAt(0) === ':') name = name.slice(1); + if (__self.viewer_list.indexOf(name) < 0 && __self.config.ignorelist.indexOf(name) < 0) { + __self.viewer_list.push(name); + } + }); + } + + // handle JOIN's + if (data_split[3] == 'JOIN') { + var servernick = data_split[2].toLowerCase().split('!'); + viewer = servernick[0]; + if (viewer != __self.irc.config.name.toLowerCase()) { + viewer = viewer.slice(1); + } + if (__self.viewer_list.indexOf(viewer) < 0 && __self.config.ignorelist.indexOf(viewer) < 0) { + __self.viewer_list.push(viewer); + } + } + + // handle PART's + if (data_split[3] == 'PART') { + var servernick = data_split[2].toLowerCase().split('!'); + viewer = servernick[0]; + if (viewer != __self.irc.config.name.toLowerCase()) { + viewer = viewer.slice(1); + } + var viewer_idx = __self.viewer_list.indexOf(viewer); + if (viewer_idx >= 0) { + __self.viewer_list.splice(viewer_idx); + } + } + + // viewers chatting + if (data_split[3] == 'PRIVMSG') { + var servernick = data_split[2].toLowerCase().split('!'); + viewer = servernick[0]; + if (viewer != __self.irc.config.name.toLowerCase()) { + viewer = viewer.slice(1); + } + if (__self.viewer_list.indexOf(viewer) < 0 && __self.config.ignorelist.indexOf(viewer) < 0) { + __self.viewer_list.push(viewer); + } + if (__self.active_list.indexOf(viewer) < 0) { + __self.active_list.push(viewer); } - } else { - __self.viewer_list = []; } }); diff --git a/lib/initialize.js b/lib/initialize.js index 0289fd6..7169986 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -36,7 +36,8 @@ exports.initialize = function(options) { subscribers : config.twitch.subscribers, website : config.currency.website, modpowers : config.currency.modpowers, - chatterbonus: config.currency.chatterbonus + chatterbonus: config.currency.chatterbonus, + ignorelist : config.ignorelist }); web = require('./web.js')(db, { port : config.web.port, diff --git a/lib/irc.js b/lib/irc.js index b91b07d..5e0a7de 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -154,6 +154,7 @@ IRC.prototype.start = function () { stream_status(); __self.connect(); + debugger; __self.monitor_queue(); }; @@ -239,7 +240,7 @@ IRC.prototype.reconnect = function () { IRC.prototype.join = function (data) { var __self = this; - if (data[3] === ':End') { + if (data[3] === ':>') { __self.raw('JOIN ' + __self.config.channel); } }; diff --git a/web/templates/ladder.jade b/web/templates/ladder.jade index 07fe5bd..676d30a 100644 --- a/web/templates/ladder.jade +++ b/web/templates/ladder.jade @@ -36,6 +36,7 @@ html(lang="en") caption Potato Farmer Ladder Rankings thead tr + th Rank th Viewer th Potatoes tbody @@ -62,10 +63,11 @@ html(lang="en") // slice(a, b): a = (n*(x-1))+1, b = n*x where n = perPage and x=curerntPage (skip +1 at end of a for splice) var a = (perPage*(z-1)), - b = perPage*z; + b = perPage*z, + pstart = z-1; data.slice(a, b).forEach(function(element, index, array){ - $('table > tbody').append($('').append(''+element.user+''+element.points+'')); + $('table > tbody').append($('').append(''+(a+index+1)+''+element.user+''+element.points+'')); }); });