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.

85 lines
2.5 KiB

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