Browse Source

Adding betting feedback using the flood/timer mechanism from currency checks.

master
Uno Polak 11 years ago
parent
commit
e4ab6185da
  1. 57
      lib/currency.js

57
lib/currency.js

@ -121,6 +121,10 @@ function Currency(irc, db, options) {
__self.bets_viewers = []; __self.bets_viewers = [];
__self.bets_payout = false; __self.bets_payout = false;
__self.bets_total = 0; __self.bets_total = 0;
__self.bets_flood = [];
__self.bets_response = null;
__self.bets_response_timer = 3000;
__self.bets_response_reset = true;
} }
//-------- Methods --------- //-------- Methods ---------
@ -1358,6 +1362,30 @@ Currency.prototype.bets = function(status, data) {
Currency.prototype.collect_bets = function (caller, bet, amount) { Currency.prototype.collect_bets = function (caller, bet, amount) {
var __self = this, has_bet = false; 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 // Bound amount by positive currency
__self.query_coins(caller, function (rows) { __self.query_coins(caller, function (rows) {
for (var i = 0; i < rows.length; i++) { 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++) { for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].viewer === caller) { if (__self.bets_viewers[i].viewer === caller) {
has_bet = true; has_bet = true;
if (amount >= 1) {
__self.bets_board[__self.bets_viewers[i].bet].num -= 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_board[__self.bets_viewers[i].bet].total -= __self.bets_viewers[i].amount;
__self.bets_viewers[i].bet = bet.idx; __self.bets_viewers[i].bet = bet.idx;
__self.bets_viewers[i].amount = amount; __self.bets_viewers[i].amount = amount;
if (amount >= 1) {
__self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].num += 1;
__self.bets_board[bet.idx].total += amount; __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; 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_viewers.push({viewer: caller, bet: bet.idx, amount: amount});
__self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].num += 1;
__self.bets_board[bet.idx].total += amount; __self.bets_board[bet.idx].total += amount;
__self.bets_flood.push(__self.bets_viewers.length - 1);
} }
} else { } else {
if (amount >= 1 && amount !== 0) { if (amount >= 1 && amount !== 0) {
__self.bets_viewers.push({viewer: caller, bet: bet.idx, amount: amount}); __self.bets_viewers.push({viewer: caller, bet: bet.idx, amount: amount});
__self.bets_board[bet.idx].num += 1; __self.bets_board[bet.idx].num += 1;
__self.bets_board[bet.idx].total += amount; __self.bets_board[bet.idx].total += amount;
__self.bets_flood.push(__self.bets_viewers.length - 1);
} }
} }
console.log(__self.bets_viewers); 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,6 +1455,7 @@ Currency.prototype.bets_deduct_bets = function () {
__self.bets_total = 0; __self.bets_total = 0;
// Remove the points and add them to an escrow // Remove the points and add them to an escrow
for (var i = 0; i < __self.bets_viewers.length; i++) { for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].amount > 0) {
sql = ''; sql = '';
sql += 'UPDATE viewers '; sql += 'UPDATE viewers ';
sql += 'SET points = points - ' + __self.bets_viewers[i].amount + ' '; sql += 'SET points = points - ' + __self.bets_viewers[i].amount + ' ';
@ -1420,6 +1466,7 @@ Currency.prototype.bets_deduct_bets = function () {
__self.bets_total += __self.bets_viewers[i].amount; __self.bets_total += __self.bets_viewers[i].amount;
} }
} }
}
}; };
Currency.prototype.bets_award_winner = function (winner) { Currency.prototype.bets_award_winner = function (winner) {

Loading…
Cancel
Save