A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.8 KiB

  1. var express = require('express'),
  2. bodyParser = require('body-parser'),
  3. utils = require('./utils.js'),
  4. https = require('https');
  5. //---- Construct ----
  6. function WEB(db, currency, options) {
  7. var __self = this;
  8. __self.db = db;
  9. __self.currency = currency;
  10. __self.port = options.port || 9000;
  11. __self.title = options.title;
  12. __self.currency_name = options.currency;
  13. __self.logo = options.logo;
  14. __self.twitter = options.twitter;
  15. __self.fanart = options.fanart;
  16. __self.statdir = options.statdir;
  17. __self.check_data = 4;//minutes
  18. __self.first_check = true;
  19. __self.slogan = '';
  20. }
  21. // ---- Methods ----
  22. WEB.prototype.start = function () {
  23. var __self = this;
  24. __self.srv = express();
  25. __self.srv.set('view engine', 'jade');
  26. __self.srv.set('views', 'web/templates');
  27. __self.srv.use(bodyParser());
  28. __self.srv.use(express.static('./web/public'));
  29. __self.render_opts = {
  30. title: __self.title,
  31. slogan: __self.slogan,
  32. logo: __self.logo,
  33. twitter: __self.twitter,
  34. fanart: __self.fanart,
  35. currency: __self.currency_name,
  36. };
  37. // get twitch/twitter data
  38. function update_data() {
  39. var time = utils.make_interval(__self.check_data);
  40. if (time === 0 || __self.first_check) {
  41. if(__self.first_check) __self.first_check = false;
  42. https.get('https://api.twitch.tv/kraken/channels/' + __self.title, function (response) {
  43. var body = '';
  44. // put together response
  45. response.on('data', function (chunk) {
  46. body += chunk;
  47. });
  48. // log file creation
  49. response.on('end', function () {
  50. var json = null;
  51. try {
  52. json = JSON.parse(body);
  53. __self.slogan = json.status;
  54. __self.render_opts.slogan = json.status;
  55. } catch (err) {
  56. console.log('Error grabbing Twitch data in Web.JS: '+err);
  57. }
  58. setTimeout(update_data, 1000);
  59. });
  60. });
  61. } else {
  62. setTimeout(update_data, time);
  63. }
  64. }
  65. // ---- Helpers ----
  66. __self.srv.locals.ucfirst = function(value){
  67. return value.charAt(0).toUpperCase() + value.slice(1);
  68. };
  69. __self.srv.locals.slicelast = function(value){
  70. return value.slice(0, -1);
  71. };
  72. // ---- Routes -----
  73. __self.srv.get('/', function(req, res) {
  74. //lets get the top 5
  75. sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT 10;';
  76. __self.db.execute(sql, function(rows) {
  77. var opts = __self.render_opts;
  78. opts.rows = rows;
  79. opts.bet_status = __self.currency.bets_status;
  80. opts.bet_board = __self.currency.bets_board;
  81. opts.bet_viewers = __self.currency.bets_viewers;
  82. res.render('index', opts);
  83. });
  84. });
  85. __self.srv.get('/ladder', function(req, res) {
  86. //get the whole viewer list
  87. sql = 'SELECT * FROM viewers ORDER BY points DESC;';
  88. __self.db.execute(sql, function(rows) {
  89. var opts = __self.render_opts;
  90. opts.rows = rows;
  91. res.render('ladder', opts);
  92. });
  93. });
  94. __self.srv.get('/stats', function(req, res) {
  95. var opts = __self.render_opts;
  96. opts.statdir = __self.statdir;
  97. res.render('stats', opts);
  98. });
  99. __self.srv.get('/fanart', function(req, res) {
  100. res.render('fanart', __self.render_opts);
  101. });
  102. /* Raw data for android app */
  103. __self.srv.get('/api/test', function(req, res) {
  104. res.send("Hey, its Potatr. This data was pulled from the web.");
  105. });
  106. __self.srv.get('/api/viewer_dump', function(req, res) {
  107. sql = 'SELECT * FROM viewers ORDER BY points DESC;';
  108. __self.db.execute(sql, function(rows) {
  109. ladder_data = new Object();
  110. rows.forEach(function(element, index, array){
  111. ladder_data[element.user] = element.points;
  112. });
  113. res.send(ladder_data);
  114. });
  115. });
  116. __self.srv.post('/api/exchange', function(req, res) {
  117. var name = req.body.name.toLowerCase(),
  118. amount = parseInt(req.body.amount, 10);
  119. if (req.ip == '127.0.0.1') {
  120. if ( __self.currency.viewer_list.indexOf(name) > -1 ) {
  121. __self.currency.adjust_currency('add', amount, name);
  122. res.send('1');
  123. } else {
  124. res.send('0');
  125. }
  126. }
  127. });
  128. __self.srv.listen(__self.port);
  129. update_data();
  130. console.log('Started website at '+__self.port);
  131. };
  132. module.exports = function (db, currency, options) {
  133. return new WEB(db, currency, options);
  134. };