Template updates, ready to branch web

This commit is contained in:
binaryatrocity committed 2014-07-25 12:27:37 -05:00
1 parent 7f84716b59
commit a9eda304f3
50 files changed
+3718 -75

No files matched your search

+3 -2
View File
@@ -20,6 +20,7 @@ function Commands(irc, db, options) {
__self.config = options || {};
__self.config.bot_name = options.bot_name || '';
__self.config.currency = options.currency || 'coins';
__self.config.exchange = options.exchange || {};
__self.command_list = [];
}
@@ -66,7 +67,8 @@ Commands.prototype.commands = function(data) {
}
});
} else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', ';
var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', !top, !rank, ';
if(Object.keys(__self.config.exchange).length != 0) commands += '!exchange, ';
for (var i = 0; i < __self.command_list.length; i++) {
if (i !== __self.command_list.length - 1) {
commands += '!' + __self.command_list[i] + ', ';
@@ -74,7 +76,6 @@ Commands.prototype.commands = function(data) {
commands += '!' + __self.command_list[i];
}
}
console.log("!!!!! Right before EMIT");
__self.irc.emit('message',{message:commands, options:null});
}
};
+69 -14
View File
@@ -59,6 +59,7 @@
var https = require('https'),
http = require('http'),
querystr = require('querystring'),
file = require('fs'),
utils = require('./utils.js');
@@ -77,6 +78,7 @@ function Currency(irc, db, commands, options) {
__self.config.website = options.website || '';
__self.config.modpowers = options.modpowers || false;
__self.config.ignorelist = options.ignorelist || ['jtv'];
__self.config.exchanges = options.exchanges || {};
// general settings
__self.pre_text = '> ' + __self.config.currency + ': ';
@@ -155,6 +157,8 @@ function Currency(irc, db, commands, options) {
// message to new subscribers
__self.sub_ty_msg = options.sub_ty_msg || '';
// stored twitch stream data
}
//-------- Methods ---------
@@ -270,10 +274,8 @@ Currency.prototype.commands = function (data) {
// 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 {
@@ -300,10 +302,8 @@ Currency.prototype.commands = function (data) {
// open / close voting system
if (data[4] === 'vote') {
console.log("vote command found");
switch (data[5]) {
case 'open':
console.log("vote open command found");
if (data[6] && data[7]) {
__self.votes(true, data);
} else {
@@ -458,6 +458,9 @@ Currency.prototype.commands = function (data) {
case '!rank':
__self.get_rank(__self.irc.caller(data[0]).toLowerCase());
break;
case '!exchange':
if(Object.keys(__self.config.exchanges).length != 0)
__self.currency_exchange(__self.irc.caller(data[0]), parseInt(data[4], 10), data[5]);
}
// place a bet
@@ -561,6 +564,67 @@ Currency.prototype.get_coins = function (caller) {
}
}
};
Currency.prototype.currency_exchange = function (name, amount, type) {
console.log('INSIDE CURR EXCHANGE');
var __self = this,
rate = 15,
cost = (amount * (rate / 100))
total = amount + cost;
if( name && amount && type ) {
console.log('ready to post');
if(Object.keys(__self.config.exchanges).indexOf(type) > -1) {
__self.query_coins(name.toLowerCase(), function (rows) {
var message = '',
response = new String();
if(rows[0].points >= total) {
// Prepare for POST
var data = querystr.stringify({'name': name, 'amount': amount});
var opts = {
port: __self.config.exchanges[type],
path: '/api/exchange',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var request = http.request(opts, function(res) {
res.setEncoding('utf-8');
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
if(response == '1') {
// Exchanged succesfully, let's remove local currency
__self.adjust_currency('remove', total, name);
message = name+' exchanged '+amount+' for '+type+'. Cost '+total;
__self.irc.emit('message', {message:__self.pre_text + message, timer: 1});
}
});
});
request.write(data);
request.end();
}
});
}
} else {
message = '!exchange <amount> <type> [Valid types: ';
keys = Object.keys(__self.config.exchanges);
keys.forEach(function(element, index, array) {
message += keys[index]+', ';
});
message+=']';
__self.irc.emit('message', {message:__self.pre_text + message, timer: 1});
}
};
Currency.prototype.get_rank = function (data) {
var __self = this;
@@ -602,7 +666,7 @@ Currency.prototype.get_rank = function (data) {
Currency.prototype.get_top = function (data) {
var __self = this;
var limit = 0;
var limit = 5;
function do_work() {
var response = '';
@@ -623,12 +687,6 @@ Currency.prototype.get_top = function (data) {
__self.top_flood = [];
}
// check that we got a number
( isNaN(data) ) ? limit = 3 : limit = data;
// limit the number
if (limit > 6) limit = 3;
// add flood users to array
if (__self.top_flood.indexOf(limit) < 0) {
__self.top_flood.push(limit);
@@ -892,7 +950,6 @@ Currency.prototype.handout_coins = function () {
if (viewer_idx >= 0) {
__self.viewer_list.splice(viewer_idx,1);
} else {
console.log("[*ERROR*] User "+viewer+" PART'ed but was not in viewer_list.");
}
}
@@ -1769,7 +1826,6 @@ Currency.prototype.collect_votes = function (caller, vote, amount) {
__self.votes_flood.push(__self.votes_viewers.length - 1);
}
}
console.log(__self.votes_viewers);
// check if flood has a set amount of requests and output
// if not, set the output timer
@@ -2059,7 +2115,6 @@ Currency.prototype.collect_bets = function (caller, bet, 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
+9 -6
View File
@@ -29,6 +29,7 @@ exports.initialize = function(options) {
});
commands = require('./commands.js')(irc, db, {
bot_name : config.twitch.bot.name,
exchange : config.exchanges,
currency : config.currency.name
});
currency = require('./currency.js')(irc, db, commands, {
@@ -38,15 +39,17 @@ exports.initialize = function(options) {
website : config.currency.website,
modpowers : config.currency.modpowers,
ignorelist : config.ignorelist,
exchanges : config.exchanges,
sub_ty_msg : config.currency.sub_ty_msg
});
web = require('./web.js')(db, currency, {
port : config.web.port,
title : config.twitch.channel,
slogan : config.web.slogan,
logo : config.web.logo,
twitter : config.web.twitter,
statdir : config.web.statdir
port : config.web.port,
title : config.twitch.channel,
currency : config.currency.name,
logo : config.web.logo,
twitter : config.web.twitter,
fanart : config.web.fanart,
statdir : config.twitch.channel
});
//-------- Start -------
-2
View File
@@ -98,8 +98,6 @@ function IRC(options) {
__self.first_check = true;
__self.store_chat = __self.options.chatlog || false;
__self.chat_log = '';
}
IRC.prototype = new events.EventEmitter();
+66 -4
View File
@@ -1,4 +1,6 @@
var express = require('express'),
bodyParser = require('body-parser'),
utils = require('./utils.js'),
https = require('https');
//---- Construct ----
@@ -10,10 +12,14 @@ function WEB(db, currency, options) {
__self.port = options.port || 9000;
__self.title = options.title;
__self.slogan = options.slogan;
__self.currency_name = options.currency;
__self.logo = options.logo;
__self.twitter = options.twitter;
__self.fanart = options.fanart;
__self.statdir = options.statdir;
__self.check_data = 4;//minutes
__self.first_check = true;
__self.slogan = '';
}
// ---- Methods ----
@@ -22,6 +28,7 @@ WEB.prototype.start = function () {
__self.srv = express();
__self.srv.set('view engine', 'jade');
__self.srv.set('views', 'web/templates');
__self.srv.use(bodyParser());
__self.srv.use(express.static('./web/public'));
__self.render_opts = {
@@ -29,8 +36,49 @@ WEB.prototype.start = function () {
slogan: __self.slogan,
logo: __self.logo,
twitter: __self.twitter,
fanart: __self.fanart,
currency: __self.currency_name,
};
// get twitch/twitter data
function update_data() {
var time = utils.make_interval(__self.check_data);
if (time === 0 || __self.first_check) {
if(__self.first_check) __self.first_check = false;
https.get('https://api.twitch.tv/kraken/channels/' + __self.title, function (response) {
var body = '';
// put together response
response.on('data', function (chunk) {
body += chunk;
});
// log file creation
response.on('end', function () {
var json = null;
try {
json = JSON.parse(body);
__self.slogan = json.status;
__self.render_opts.slogan = json.status;
} catch (err) {
console.log('Error grabbing Twitch data in Web.JS: '+err);
}
setTimeout(update_data, 1000);
});
});
} else {
setTimeout(update_data, time);
}
}
// ---- Helpers ----
__self.srv.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
__self.srv.locals.slicelast = function(value){
return value.slice(0, -1);
};
// ---- Routes -----
__self.srv.get('/', function(req, res) {
@@ -60,15 +108,15 @@ WEB.prototype.start = function () {
opts.statdir = __self.statdir;
res.render('stats', opts);
});
/* Raw data for android app */
__self.srv.get('/fanart', function(req, res) {
res.render('fanart', __self.render_opts);
});
/* Raw data for android app */
__self.srv.get('/api/test', function(req, res) {
res.send("Hey, its Potatr. This data was pulled from the web.");
});
__self.srv.all('/api/data', function(req, res) {
__self.srv.get('/api/viewer_dump', function(req, res) {
sql = 'SELECT * FROM viewers ORDER BY points DESC;';
__self.db.execute(sql, function(rows) {
ladder_data = new Object();
@@ -78,8 +126,22 @@ WEB.prototype.start = function () {
res.send(ladder_data);
});
});
__self.srv.post('/api/exchange', function(req, res) {
var name = req.body.name.toLowerCase(),
amount = parseInt(req.body.amount, 10);
if (req.ip == '127.0.0.1') {
if ( __self.currency.viewer_list.indexOf(name) > -1 ) {
__self.currency.adjust_currency('add', amount, name);
res.send('1');
} else {
res.send('0');
}
}
});
__self.srv.listen(__self.port);
update_data();
console.log('Started website at '+__self.port);
};