From e4ab6185da43b678681a4f5de6a65ad1ea969380 Mon Sep 17 00:00:00 2001 From: Uno Polak Date: Mon, 6 Jan 2014 14:49:08 -0600 Subject: [PATCH] Adding betting feedback using the flood/timer mechanism from currency checks. --- lib/currency.js | 79 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/lib/currency.js b/lib/currency.js index 6c8570e..6d05aa5 100644 --- a/lib/currency.js +++ b/lib/currency.js @@ -121,6 +121,10 @@ function Currency(irc, db, options) { __self.bets_viewers = []; __self.bets_payout = false; __self.bets_total = 0; + __self.bets_flood = []; + __self.bets_response = null; + __self.bets_response_timer = 3000; + __self.bets_response_reset = true; } //-------- Methods --------- @@ -1358,6 +1362,30 @@ Currency.prototype.bets = function(status, data) { Currency.prototype.collect_bets = function (caller, bet, amount) { var __self = this, has_bet = false; + function do_work() { + var multi_response = []; + for (var i = 0; i < __self.bets_board.length; i++) { + multi_response.push([]); + } + + if (__self.bets_flood.length > 0) {// send flood requests + var bet; + // setup bet response + for (var i = 0; i < __self.bets_flood.length; i++) { + bet = __self.bets_viewers[__self.bets_flood[i]]; + multi_response[bet.bet].push(bet.viewer + ' (' + bet.amount + ')'); + } + for (var i = 0; i < __self.bets_board.length; i++) { + if (multi_response[i].length > 0) { + __self.irc.emit('message', {message:'New bets for ' + __self.bets_board[i].name + ': ' + multi_response[i].join(', '), timer: 1}); + } + } + } + + // clear flood requests + __self.bets_flood = []; + } + // Bound amount by positive currency __self.query_coins(caller, function (rows) { for (var i = 0; i < rows.length; i++) { @@ -1367,17 +1395,20 @@ Currency.prototype.collect_bets = function (caller, bet, amount) { for (var i = 0; i < __self.bets_viewers.length; i++) { if (__self.bets_viewers[i].viewer === caller) { has_bet = true; + __self.bets_board[__self.bets_viewers[i].bet].num -= 1; + __self.bets_board[__self.bets_viewers[i].bet].total -= __self.bets_viewers[i].amount; + __self.bets_viewers[i].bet = bet.idx; + __self.bets_viewers[i].amount = amount; if (amount >= 1) { - __self.bets_board[__self.bets_viewers[i].bet].num -= 1; - __self.bets_board[__self.bets_viewers[i].bet].total -= __self.bets_viewers[i].amount; - __self.bets_viewers[i].bet = bet.idx; - __self.bets_viewers[i].amount = amount; __self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].total += amount; - } else { - __self.bets_board[__self.bets_viewers[i].bet].num -= 1; - __self.bets_board[__self.bets_viewers[i].bet].total -= __self.bets_viewers[i].amount; - __self.bets_viewers.splice(i, 1); + // add flood users to array + if (__self.bets_flood.indexOf(i) < 0) { + __self.bets_flood.push(i); + } + // remove from flood if nullified bet + } else if (__self.bets_flood.indexOf(i) > 0) { + __self.bets_flood.splice(__self.bets_flood.indexOf(i), 1); } break; } @@ -1386,15 +1417,29 @@ Currency.prototype.collect_bets = function (caller, bet, amount) { __self.bets_viewers.push({viewer: caller, bet: bet.idx, amount: amount}); __self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].total += amount; + __self.bets_flood.push(__self.bets_viewers.length - 1); } } else { if (amount >= 1 && amount !== 0) { __self.bets_viewers.push({viewer: caller, bet: bet.idx, amount: amount}); __self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].total += amount; + __self.bets_flood.push(__self.bets_viewers.length - 1); } } console.log(__self.bets_viewers); + + // check if flood has a set amount of requests and output + // if not, set the output timer + if (__self.bets_flood.length === __self.max_requests) { + do_work(); + } else { + if (__self.bets_response_reset) { + __self.bets_response = setTimeout(function () {do_work();}, __self.bets_response_timer); + } else { + setTimeout(function () {do_work();}, __self.bets_response_timer); + } + } }); }; @@ -1410,14 +1455,16 @@ Currency.prototype.bets_deduct_bets = function () { __self.bets_total = 0; // Remove the points and add them to an escrow for (var i = 0; i < __self.bets_viewers.length; i++) { - sql = ''; - sql += 'UPDATE viewers '; - sql += 'SET points = points - ' + __self.bets_viewers[i].amount + ' '; - sql += 'WHERE user = \'' + __self.bets_viewers[i].viewer + '\''; - __self.db.execute(sql, function() {}); - __self.bets_board[__self.bets_viewers[i].bet].num += 1; - __self.bets_board[__self.bets_viewers[i].bet].total += __self.bets_viewers[i].amount; - __self.bets_total += __self.bets_viewers[i].amount; + if (__self.bets_viewers[i].amount > 0) { + sql = ''; + sql += 'UPDATE viewers '; + sql += 'SET points = points - ' + __self.bets_viewers[i].amount + ' '; + sql += 'WHERE user = \'' + __self.bets_viewers[i].viewer + '\''; + __self.db.execute(sql, function() {}); + __self.bets_board[__self.bets_viewers[i].bet].num += 1; + __self.bets_board[__self.bets_viewers[i].bet].total += __self.bets_viewers[i].amount; + __self.bets_total += __self.bets_viewers[i].amount; + } } } };