Browse Source

Finalized !rank.

master
Brandon Cornejo 11 years ago
parent
commit
31c089a7a8
  1. 143
      lib/currency.js

143
lib/currency.js

@ -89,6 +89,10 @@ function Currency(irc, db, options) {
__self.coin_toggle = false; __self.coin_toggle = false;
__self.coin_toggle_msg = null; __self.coin_toggle_msg = null;
__self.coin_toggle_timer = 180000;//milliseconds __self.coin_toggle_timer = 180000;//milliseconds
__self.top_flood = [];
__self.top_response_reset = true;
__self.rank_flood = [];
__self.rank_response_reset = true;
// auction settings // auction settings
__self.auction_status = false; __self.auction_status = false;
@ -151,12 +155,17 @@ Currency.prototype.commands = function (data) {
case 'on': case 'on':
__self.config.chatterbonus = true; __self.config.chatterbonus = true;
__self.irc.emit('message', {message:__self.pre_text + 'Chatter bonus is on! Earn extra potatoes by being active in the chat.'}); __self.irc.emit('message', {message:__self.pre_text + 'Chatter bonus is on! Earn extra potatoes by being active in the chat.'});
break; break;
case 'off': case 'off':
__self.config.chatterbonus = false; __self.config.chatterbonus = false;
__self.irc.emit('message', {message:__self.pre_text + 'Chatterbonus is now off. Potato deflation!'}); __self.irc.emit('message', {message:__self.pre_text + 'Chatterbonus is now off. Potato deflation!'});
break;
case 'status':
if(!__self.config.chatterbonus) {
__self.irc.emit('message', {message:__self.pre_text + 'Chatterbonus is: Off'});
} else {
__self.irc.emit('message', {message:__self.pre_text + 'Chatterbonus is: On'});
}
break; break;
} }
} }
@ -359,6 +368,15 @@ Currency.prototype.commands = function (data) {
} }
} }
break; break;
case '!chatters':
__self.irc.emit('message', {message:__self.pre_text + 'Active chatters: ' + __self.active_list.toString()});
break;
case '!top':
__self.get_top(data[4]);
break;
case '!rank':
__self.get_rank(__self.irc.caller(data[0]).toLowerCase());
break;
} }
// place a bet // place a bet
@ -449,6 +467,126 @@ Currency.prototype.get_coins = function (caller) {
} }
} }
}; };
Currency.prototype.get_rank = function (data) {
var __self = this;
function do_work() {
__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 = response.slice(1,-2);
__self.irc.emit('message', {message: response, timer: 1});
});
__self.rank_flood = [];
}
// add flood users to array
if (__self.rank_flood.indexOf(data) < 0) {
__self.rank_flood.push(data);
}
// clear timer on flood
if (__self.rank_response_reset) {
clearTimeout(__self.rank_response);
}
// check if flood has hit max requests or set timer
if (__self.rank_flood.length === __self.max_requests) {
do_work();
} else {
if (__self.rank_response_reset) {
__self.rank_response = setTimeout(function () {do_work();}, __self.coin_response_timer);
} else {
setTimeout(function () {do_work();}, __self.coin_response_timer);
}
}
};
Currency.prototype.get_top = function (data) {
var __self = this;
var limit = 0;
function do_work() {
var response = '';
__self.query_top(__self.top_flood, function (rows) {
for (var i = 0; i < rows.length; i++) {
// setup response
if (i !== rows.length - 1) {
response += '#'+(i+1)+': ' + rows[i].user + '('+rows[i].points+'), ';
} else {
response += '#'+(i+1)+': ' + rows[i].user + '('+rows[i].points+')';
}
}
__self.irc.emit('message', {message:'> Top Potato Farmers: ' + response});
});
// clear flood requests
__self.top_flood = [];
}
// check that we got a number
( isNaN(data) ) ? limit = 3 : limit = data;
// limit the number
if (limit > 6) limit = 3;
// add flood users to array
if (__self.top_flood.indexOf(limit) < 0) {
__self.top_flood.push(limit);
}
// clear timer on flood
if (__self.top_response_reset) {
clearTimeout(__self.top_response);
}
// check if flood has hit max requests or set timer
if (__self.top_flood.length === __self.max_requests) {
do_work();
} else {
if (__self.top_response_reset) {
__self.top_response = setTimeout(function () {do_work();}, __self.coin_response_timer);
} else {
setTimeout(function () {do_work();}, __self.coin_response_timer);
}
}
};
Currency.prototype.query_rank = function(data, callback) {
var __self = this;
var ranks = {}, total = 0;
var inner_sql = '', sql = 'set @row_number:=0;';
data.forEach(function(user) {
inner_sql += "user='"+user+"' or";
});
// remove last or
inner_sql=inner_sql.slice(0,-3);
sql += "select * from (select user, points, @row_number:=@row_number+1 from viewers order by points desc) as rank where ("+inner_sql+");select count(1) from viewers where points > 0;";
__self.db.execute(sql, function(rows) {
total = rows[2][0]['count(1)'];
ranks = rows[1];
callback(ranks, total);
});
};
Currency.prototype.query_top = function (data, callback) {
var __self = this, sql = '';
// build sql conditions
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT ' + data + ';';
__self.db.execute(sql, function(rows) {
callback(rows);
});
};
Currency.prototype.query_coins = function (data, callback) { Currency.prototype.query_coins = function (data, callback) {
var __self = this, sql = ''; var __self = this, sql = '';
@ -653,7 +791,6 @@ Currency.prototype.handout_coins = function () {
} }
} else { } else {
__self.viewer_list = []; __self.viewer_list = [];
__self.active_list = [];
} }
}); });

Loading…
Cancel
Save