Brandon Cornejo 11 years ago
parent
commit
30d786f696
  1. 250
      lib/currency.js

250
lib/currency.js

@ -127,6 +127,11 @@ function Currency(irc, db, options) {
__self.bets_board = [];
__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 ---------
@ -138,7 +143,7 @@ Currency.prototype.start = function () {
Currency.prototype.commands = function (data) {
var __self = this,
broadcaster_bot_initiated = __self.irc.caller(data[0]).toLowerCase() === __self.irc.config.broadcaster.toLowerCase() || __self.irc.caller(data[0]).toLowerCase() === __self.irc.config.name.toLowerCase(),
moderator_initiated = __self.irc.mods.indexOf(__self.irc.caller(data[0])) > 0;
moderator_initiated = __self.irc.mods.indexOf(__self.irc.caller(data[0])) >= 0;
// handle !<currency> commands
if (data[3].slice(1) === '!' + __self.config.currency.toLowerCase()) {
@ -184,6 +189,9 @@ Currency.prototype.commands = function (data) {
case 'cancel':
__self.auction('cancel');
break;
case 'help':
__self.irc.emit('message', {message:__self.pre_text + '!' + __self.config.currency.toLowerCase() + ' auction open/close/cancel/draw'});
break;
}
}
@ -222,6 +230,9 @@ Currency.prototype.commands = function (data) {
case 'restore':
__self.raffle('restore');
break;
case 'help':
__self.irc.emit('message', {message:__self.pre_text + '!' + __self.config.currency.toLowerCase() + ' raffle open <ticket cost> <tickets per user>/close/cancel/draw/restore'});
break;
}
}
@ -229,7 +240,7 @@ Currency.prototype.commands = function (data) {
if (data[4] === 'bet') {
switch (data[5]) {
case 'open':
if (data[5] && data[6]) {
if (data[6] && data[7]) {
__self.bets(true, data);
} else {
__self.irc.emit('message', {message:__self.pre_text + 'Unable to open betting, need at least two items to bet against'});
@ -238,8 +249,17 @@ Currency.prototype.commands = function (data) {
case 'close':
__self.bets(false, null);
break;
case 'pool':
__self.bets('pool', null);
break;
case 'winner':
//__self.bets('winner');
__self.bets('winner', data[6]);
break;
case 'cancel':
__self.bets('cancel');
break;
case 'help':
__self.irc.emit('message', {message:__self.pre_text + '!' + __self.config.currency.toLowerCase() + ' bet open <option A> <option B> <etc>/close/pool/winner <option>/cancel'});
break;
}
}
@ -379,7 +399,7 @@ Currency.prototype.commands = function (data) {
// place a bet
if (__self.bets_status === true) {
for (var i = 0; i < __self.bets_board.length; i++) {
if (data[3].slice(1) === '!' + __self.bets_board[i]) {
if (data[3].slice(1) === '!' + __self.bets_board[i].name) {
if (isNaN(parseInt(data[4], 10)) === false) {
if (data[4] >= 0 && data[4] % 1 === 0) {
__self.collect_bets(__self.irc.caller(data[0]), __self.bets_board[i], parseInt(data[4], 10));
@ -1422,16 +1442,20 @@ Currency.prototype.bets = function(status, data) {
// clear previous board / bets
__self.bets_board = [];
__self.bets_viewers = [];
__self.bets_total = 0;
// create new betting board
__self.bets_board = data.join().split(',').filter(function(n){return n}).slice(6);
__self.bets_board = data.join().split(',').filter(function(n){return n}).slice(6).map(function(n){return {name: n, num: 0, total: 0};});
for (var i = 0; i < __self.bets_board.length; i++) {
__self.bets_board[i].idx = i;
}
// create chat message on how to place a bet
for (var i = 0; i < __self.bets_board.length; i++) {
if (i !== __self.bets_board.length - 1) {
wager_msg += '"!' + __self.bets_board[i] + '" / ';
wager_msg += '"!' + __self.bets_board[i].name + '" / ';
} else {
wager_msg += '"!' + __self.bets_board[i] + '"';
wager_msg += '"!' + __self.bets_board[i].name + '"';
}
}
@ -1461,20 +1485,81 @@ Currency.prototype.bets = function(status, data) {
// close out bets
__self.bets_status = false;
// deduct bets from viewers amounts
__self.bets_deduct_bets();
// output to chat
if (__self.bets_viewers.length > 0) {
__self.irc.emit('message', {message:__self.pre_text + 'Betting is now closed'});
for (var i = 0; i < __self.bets_board.length; i++) {
__self.irc.emit('message', {message:'+ ' + __self.bets_board[i].name + ' with ' + __self.bets_board[i].num + ' bets totaling ' + __self.bets_board[i].total});
}
} else {
__self.irc.emit('message', {message:__self.pre_text + 'Betting closed, no bets were placed'});
}
// deduct bets from viewers amounts
__self.bets_deduct_bets();
}
break;
case 'pool':
function do_work() {
if (__self.bets_board.length > 0) {
__self.irc.emit('message', {message:__self.pre_text + 'Current betting pool is:'});
for (var i = 0; i < __self.bets_board.length; i++) {
__self.irc.emit('message', {message:'+ ' + __self.bets_board[i].name + ' with ' + __self.bets_board[i].num + ' bets totaling ' + __self.bets_board[i].total});
}
} else {
__self.irc.emit('message', {message:__self.pre_text + 'No current bet.'});
}
}
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);
}
break;
case 'winner':
// set payout to complete
__self.bets_payout = false;
if (!__self.bets_status) {
if (__self.bets_payout) {
for (var i = 0; i < __self.bets_board.length; i++) {
if (data == __self.bets_board[i].name) {
__self.bets_award_winner(__self.bets_board[i]);
__self.irc.emit('message', {message:__self.pre_text + 'Betting payed out to ' + data});
}
}
} else {
__self.irc.emit('message', {message:__self.pre_text + 'Betting has already payed out'});
}
} else {
__self.irc.emit('message', {message:__self.pre_text + 'Betting must be closed before you can have a winner'});
}
break;
case 'cancel':
if (!__self.bets_status) {
// Return all the currency back to original owners
if (__self.bets_payout) {
sql = '';
for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].amount > 0) {
if (sql.length > 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.irc.emit('message', {message:__self.pre_text + 'Bet was canceled, all ' + __self.config.currency.toLowerCase() + ' returned.'});
});
__self.bets_payout = false;
// Or just cancel the bets
} else {
__self.irc.emit('message', {message:__self.pre_text + 'No bet to cancel.'});
}
} else {
__self.bets_status = false;
__self.bets_payout = false;
__self.irc.emit('message', {message:__self.pre_text + 'Current bet was canceled.'});
}
break;
}
};
@ -1482,32 +1567,139 @@ Currency.prototype.bets = function(status, data) {
Currency.prototype.collect_bets = function (caller, bet, amount) {
var __self = this, has_bet = false;
if (__self.bets_viewers.length > 0) {
for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].viewer === caller) {
has_bet = true;
if (amount >= 1) {
__self.bets_viewers[i].bet = bet;
__self.bets_viewers[i].amount = amount;
} else if (amount === 0 && bet === __self.bets_viewers[i].bet) {
__self.bets_viewers.splice(i, 1);
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});
}
break;
}
}
if (!has_bet && amount >= 1 && amount !== 0) {
__self.bets_viewers.push({viewer: caller, bet: bet, amount: amount});
// 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++) {
amount = Math.min(amount, rows[i].points);
}
if (__self.bets_viewers.length > 0) {
for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].viewer === caller) {
has_bet = true;
if (amount > __self.bets_viewers[i].amount) {
__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;
// add flood users to array
if (__self.bets_flood.indexOf(i) < 0) {
__self.bets_flood.push(i);
}
}
break;
}
}
if (!has_bet && 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);
}
} 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);
}
}
});
};
Currency.prototype.bets_deduct_bets = function () {
var __self = this;
if (__self.bets_viewers.length > 0) {
// Don't trust the on-the-fly numbers
for (var i = 0; i < __self.bets_board.length; i++) {
__self.bets_board[i].num = 0;
__self.bets_board[i].total = 0;
}
} else {
if (amount >= 1 && amount !== 0) {
__self.bets_viewers.push({viewer: caller, bet: bet, amount: amount});
__self.bets_total = 0;
// Remove the points and add them to an escrow
sql = '';
for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].amount > 0) {
if (sql.length > 0) {
sql += '; ';
}
sql += 'UPDATE viewers ';
sql += 'SET points = points - ' + __self.bets_viewers[i].amount + ' ';
sql += 'WHERE user = \'' + __self.bets_viewers[i].viewer + '\'';
__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;
}
}
__self.db.execute(sql, function() {});
}
console.log(__self.bets_viewers);
};
Currency.prototype.bets_deduct_bets = function () {
Currency.prototype.bets_award_winner = function (winner) {
var __self = this;
if (__self.bets_payout) {
// set payout to complete
__self.bets_payout = false;
}
// Payout the points
if (__self.bets_viewers.length > 0) {
sql = '';
for (var i = 0; i < __self.bets_viewers.length; i++) {
if (__self.bets_viewers[i].bet == winner.idx) {
if (sql.length > 0) {
sql += '; ';
}
sql += 'UPDATE viewers ';
sql += 'SET points = points + ' + Math.ceil(__self.bets_total * __self.bets_viewers[i].amount / __self.bets_board[__self.bets_viewers[i].bet].total) + ' ';
sql += 'WHERE user = \'' + __self.bets_viewers[i].viewer + '\'';
}
}
__self.db.execute(sql, function() {});
}
// Clear the board
__self.bets_board = [];
__self.bets_viewers = [];
__self.bets_total = 0;
};
module.exports = function (irc, db, options) {

Loading…
Cancel
Save