/** * api: * Currency(irc object, database object, [required options]) * * example: * var Currency = require('./lib/plugins/currency.js')(irc, db, { * currency : 'currency name', * subscribers : 'google doc spreadsheet until it's available from twitch api' * }); * * commands: * ! * reply with currency amount * * ! on/[off repeat on/off] * toggle currency request status, * repeat status only available when * turning off requests * * ! auction open * open a new auction * * ! auction close * close current auction * * ! auction cancel * cancel current auction * * ! auction draw * draw the next highest bidder * * !bid * place a bid on an open auction, * only valid amounts will be accepted * * ! raffle open * open a new raffle * price and max is optional * default price: 10 * default max: 10 * * ! raffle close * draw the another ticket from raffle * * ! raffle cancel * cancel the open raffle * * ! raffle draw * open a new auction * * ! raffle restore * restores a previous raffle if a new * one is accidentally opened * * !ticket * place a bid on an open auction, * only valid amounts will be accepted */ var https = require('https'), http = require('http'), file = require('fs'), utils = require('./utils.js'); //-------- Construct --------- function Currency(irc, db, commands, options) { var __self = this; __self.irc = irc; __self.db = db; __self.custom_commands = commands; // config __self.config = options || {}; __self.config.currency = options.currency || 'coins'; __self.config.subscribers_json = options.subscribers || ''; __self.config.website = options.website || ''; __self.config.modpowers = options.modpowers || false; __self.config.ignorelist = options.ignorelist || ['jtv']; // general settings __self.pre_text = '> ' + __self.config.currency + ': '; __self.max_requests = 10;//response after 10 request __self.temp = {}; // currency request settings __self.coin_flood = []; __self.coin_response = null; __self.coin_response_timer = 3000; __self.coin_response_reset = true; __self.coin_toggle = false; __self.coin_toggle_msg = null; __self.coin_toggle_timer = 180000;//milliseconds __self.top_flood = []; __self.top_response_reset = true; __self.rank_flood = []; __self.rank_response_reset = true; // auction settings __self.auction_status = false; __self.auction_bids = []; __self.auction_previous_bid = {}; __self.auction_bid_response = null; __self.auction_bid_response_long = null; // handout coins settings __self.viewer_list = []; __self.streaming = false; __self.streaming_check = 4;//minutes __self.give_coins = false; __self.give_coins_timer = options.payrate || 30;//minutes __self.subscriber_check = 4;//minutes __self.subscribers = []; // raffle settings __self.raffle_status = false; __self.raffle_ticket_requests = []; __self.raffle_tickets = []; __self.raffle_ticket_cost = 10;//currency cost per ticket __self.raffle_max_tickets = 10; // raffle restoration __self.raffle_restore_ticket_requests = []; __self.raffle_restore_tickets = []; // gambling settings __self.bets_status = false; __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; // high score table __self.score_bet_pool = 0; __self.score_bet_single = 0; // commands add/rem settings __self.command_add_string = options.add_command || 'addcmd'; __self.command_remove_string = options.remove_command || 'removecmd'; } //-------- Methods --------- Currency.prototype.start = function () { var __self = this; // populate highscore holders __self.db.execute('SELECT * FROM highscores', function(rows) { rows.forEach(function(element, index, array){ switch(element.name) { case 'bet_pool': __self.score_bet_pool = element.value; break; case 'bet_single': __self.score_bet_single = element.value; break; } }); }); __self.handout_coins(); }; 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; // handle ! commands if (data[3].slice(1) === '!' + __self.config.currency.toLowerCase()) { // public commands if (!__self.coin_toggle && data[4] === undefined) { __self.get_coins(__self.irc.caller(data[0])); } // broadcaster only commands if (broadcaster_bot_initiated || (__self.config.modpowers && moderator_initiated)) { //add / remove custom commands if (data[4] === __self.command_add_string && data[5] !== undefined && data[6] !== undefined) { var command = data[5]; var text = data.slice(6); text = text.join(' '); __self.custom_commands.add(command, text); } if (data[4] === __self.command_remove_string && data[5] !== undefined) { var command = data[5]; __self.custom_commands.remove(command); } //open / close auction system if (data[4] === 'auction') { switch (data[5]) { case 'open': __self.auction(true); break; case 'close': __self.auction(false); break; case 'draw': __self.next_auction_winner(); break; 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; } } // open / close raffle system if (data[4] === 'raffle') { switch (data[5]) { case 'open': if (data[6] && data[7] && !__self.raffle_status) { if(parseInt(data[6], 10) > 0 && parseInt(data[7], 10) > 0) { // save default values __self.temp.raffle_ticket_cost = __self.raffle_ticket_cost; __self.temp.raffle_max_tickets = __self.raffle_max_tickets; // set new raffle cost / amount __self.raffle_ticket_cost = data[6]; __self.raffle_max_tickets = data[7]; } } else if (__self.temp.raffle_ticket_cost && __self.temp.raffle_max_tickets && !__self.raffle_status){ __self.raffle_ticket_cost = __self.temp.raffle_ticket_cost; __self.raffle_max_tickets = __self.temp.raffle_max_tickets; delete __self.temp.raffle_ticket_cost; delete __self.temp.raffle_max_tickets; } __self.raffle(true); break; case 'close': __self.raffle(false); break; case 'draw': __self.next_raffle_winner(); break; case 'cancel': __self.raffle('cancel'); break; case 'restore': __self.raffle('restore'); break; case 'help': __self.irc.emit('message', {message:__self.pre_text + '!' + __self.config.currency.toLowerCase() + ' raffle open /close/cancel/draw/restore'}); break; } } // open / close betting system if (data[4] === 'bet') { console.log("bet command found"); switch (data[5]) { case 'open': console.log("bet open command found"); 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'}); } break; case 'close': __self.bets(false, null); break; case 'pool': __self.bets('pool', null); break; case '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