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.

89 lines
2.7 KiB

  1. var express = require('express'),
  2. https = require('https');
  3. //---- Construct ----
  4. function WEB(db, currency, options) {
  5. var __self = this;
  6. __self.db = db;
  7. __self.currency = currency;
  8. __self.port = options.port || 9000;
  9. __self.title = options.title;
  10. __self.slogan = options.slogan;
  11. __self.logo = options.logo;
  12. __self.twitter = options.twitter;
  13. __self.statdir = options.statdir;
  14. }
  15. // ---- Methods ----
  16. WEB.prototype.start = function () {
  17. var __self = this;
  18. __self.srv = express();
  19. __self.srv.set('view engine', 'jade');
  20. __self.srv.set('views', 'web/templates');
  21. __self.srv.use(express.static('./web/public'));
  22. __self.render_opts = {
  23. title: __self.title,
  24. slogan: __self.slogan,
  25. logo: __self.logo,
  26. twitter: __self.twitter,
  27. };
  28. // ---- Routes -----
  29. __self.srv.get('/', function(req, res) {
  30. //lets get the top 5
  31. sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT 10;';
  32. __self.db.execute(sql, function(rows) {
  33. var opts = __self.render_opts;
  34. opts.rows = rows;
  35. opts.bet_status = __self.currency.bets_status;
  36. opts.bet_board = __self.currency.bets_board;
  37. opts.bet_viewers = __self.currency.bets_viewers;
  38. res.render('index', opts);
  39. });
  40. });
  41. __self.srv.get('/ladder', function(req, res) {
  42. //get the whole viewer list
  43. sql = 'SELECT * FROM viewers ORDER BY points DESC;';
  44. __self.db.execute(sql, function(rows) {
  45. var opts = __self.render_opts;
  46. opts.rows = rows;
  47. res.render('ladder', opts);
  48. });
  49. });
  50. __self.srv.get('/stats', function(req, res) {
  51. var opts = __self.render_opts;
  52. opts.statdir = __self.statdir;
  53. res.render('stats', opts);
  54. });
  55. /* Raw data for android app */
  56. __self.srv.get('/fanart', function(req, res) {
  57. res.render('fanart', __self.render_opts);
  58. });
  59. __self.srv.get('/api/test', function(req, res) {
  60. res.send("Hey, its Potatr. This data was pulled from the web.");
  61. });
  62. __self.srv.all('/api/data', function(req, res) {
  63. sql = 'SELECT * FROM viewers ORDER BY points DESC;';
  64. __self.db.execute(sql, function(rows) {
  65. ladder_data = new Object();
  66. rows.forEach(function(element, index, array){
  67. ladder_data[element.user] = element.points;
  68. });
  69. res.send(ladder_data);
  70. });
  71. });
  72. __self.srv.listen(__self.port);
  73. console.log('Started website at '+__self.port);
  74. };
  75. module.exports = function (db, currency, options) {
  76. return new WEB(db, currency, options);
  77. };