From 1d082c72632dcb2f7786107436cecd4a2cb8dd55 Mon Sep 17 00:00:00 2001 From: Brandon Cornejo Date: Thu, 4 Jun 2015 09:07:42 -0500 Subject: [PATCH] UTF Encoding. Quote database stuff. --- lib/currency.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- lib/irc.js | 2 +- lib/mysql.js | 16 +++++++++++++--- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/lib/currency.js b/lib/currency.js index f4bc5f9..5fc10a8 100644 --- a/lib/currency.js +++ b/lib/currency.js @@ -236,7 +236,7 @@ Currency.prototype.commands = function (data) { switch (data[5]) { case 'open': if (data[6] && data[7] && !__self.raffle_status) { - if(parseInt(data[6], 10) > 0 && parseInt(data[7], 10) > 0) { + 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; @@ -468,8 +468,18 @@ Currency.prototype.commands = function (data) { case '!exchange': if(Object.keys(__self.config.exchanges).length != 0) __self.currency_exchange(__self.irc.caller(data[0]), parseInt(data[4], 10), data[5]); + break; + case '!addquote': + __self.add_quotation(data); + break; + case '!delquote': + __self.remove_quotation(data); + break; + case '!quote': + __self.get_quotation(); } + // place a bet if (__self.bets_status === true) { for (var i = 0; i < __self.bets_board.length; i++) { @@ -2194,6 +2204,45 @@ Currency.prototype.bets_award_winner = function (winner) { __self.bets_total = 0; }; +// NEW STUFF +Currency.prototype.add_quotation = function (data) { + var __self = this; + + // Chop all the shit out + data.splice(0, 4); + data = data.join(' ') + + sql = 'INSERT INTO quotes (`text`) VALUES (' + __self.db.string_escape(data) + ');'; + __self.db.execute(sql, function() { + __self.irc.emit('message', {message:__self.pre_text + ' New quote added!'}); + }); +}; + +Currency.prototype.remove_quotation = function(data) { + var __self = this; + + // Chop all the shit out + data.splice(0, 4); + if (data === parseInt(data, 10)) { + __self.db.execute('DELETE FROM quotes WHERE id=\''+parseInt(data, 10)+'\';', function(rows) { + __self.irc.emit('message', {message:__self.pre_text + 'Quote #'+data+' deleted!'}); + }); + } +}; + +Currency.prototype.get_quotation = function() { + var __self = this; + + // Let's get the number of quotes in database (should store this on its own later) TODO + __self.db.execute('SELECT COUNT(*) AS quoteCount FROM quotes;', function(rows) { + var quote_count = rows[0].quoteCount; + var selected_quote_id = Math.floor(Math.random() * (quote_count - 0 + 1) + 1); + __self.db.execute('SELECT * FROM quotes WHERE id=\''+selected_quote_id+'\';', function(rows) { + __self.irc.emit('message', {message:__self.pre_text + rows[0].id + ': ' + rows[0].text}); + }); + }); +}; + module.exports = function (irc, db, commands, options) { return new Currency(irc, db, commands, options); }; diff --git a/lib/irc.js b/lib/irc.js index 737749b..a4106a0 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -77,7 +77,7 @@ function IRC(options) { addr : 'irc.twitch.tv', //__self.options.name.toLowerCase() + '.jtvirc.com', port : 6667, channel : __self.options.channel.toLowerCase(), - encoding : 'ascii' + encoding : 'utf-8' }; __self.mods = []; diff --git a/lib/mysql.js b/lib/mysql.js index d13bd7a..f89afb9 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -24,11 +24,14 @@ function DB(options) { __self.user = options.user || ''; __self.password = options.password || ''; __self.database = options.database || ''; + + // Open escape method to other modules + __self.string_escape = mysql.escape; } //-------- Methods --------- DB.prototype.start = function() { - var __self = this, commands ='', viewers = '', scores = ''; + var __self = this, commands ='', viewers = '', scores = '', quotes = ''; // table structure for table commands commands += 'CREATE TABLE IF NOT EXISTS `commands` ('; @@ -53,10 +56,17 @@ DB.prototype.start = function() { scores += '`value` int(11) NOT NULL,'; scores += '`date` timestamp ON UPDATE current_timestamp,'; scores += 'PRIMARY KEY (`name`)'; - scores += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'; + scores += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'; + + // table structure for QUOTES + quotes += 'CREATE TABLE IF NOT EXISTS `quotes` ('; + quotes += '`id` int(11) NOT NULL AUTO_INCREMENT,'; + quotes += '`text` longtext COLLATE utf8_unicode_ci NOT NULL,'; + quotes += 'PRIMARY KEY (`id`)'; + quotes += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;'; // execute sql, create tables if they don't exist - __self.execute(commands + '; ' + viewers + '; ' + scores, function(){}); + __self.execute(commands + '; ' + viewers + '; ' + scores + '; ' + quotes, function(){}); }; DB.prototype.execute = function(sql, callback) {