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.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 API(db, currency, options) {
  7. var __self = this;
  8. __self.db = db;
  9. __self.currency = currency;
  10. __self.port = options.port || 9000;
  11. __self.statdir = options.statdir;
  12. __self.check_data = 4;//minutes
  13. __self.first_check = true;
  14. __self.slogan = '';
  15. }
  16. // ---- Methods ----
  17. API.prototype.start = function () {
  18. var __self = this;
  19. __self.srv = express();
  20. __self.srv.set('view engine', 'jade');
  21. __self.srv.set('views', 'web/templates');
  22. __self.srv.use(bodyParser());
  23. __self.srv.use(express.static('./web/public'));
  24. // ---- Helpers ----
  25. __self.srv.locals.ucfirst = function(value){
  26. return value.charAt(0).toUpperCase() + value.slice(1);
  27. };
  28. __self.srv.locals.slicelast = function(value){
  29. return value.slice(0, -1);
  30. };
  31. // ---- Endpoints ----
  32. __self.srv.get('/api/ladder/all', function(req, res){
  33. // Returns the entire ladder from top to bottom
  34. sql = 'SELECT * FROM viewers ORDER BY points DESC;';
  35. __self.db.execute(sql, function(rows) {
  36. var data = new Object();
  37. data['viewers'] = rows;
  38. data['total'] = rows.length;
  39. res.send(data);
  40. });
  41. });
  42. __self.srv.get('/api/ladder/:numarg([0-9]+)', function(req, res){
  43. // Returns top int(:id) users from the ladder
  44. sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.params.numarg+';';
  45. __self.db.execute(sql, function(rows) {
  46. var data = new Object();
  47. data['viewers'] = rows;
  48. data['total'] = rows.length;
  49. res.send(data);
  50. });
  51. });
  52. __self.srv.get('/api/bet', function(req, res){
  53. // Returns all of the bet data together
  54. data = new Object();
  55. data['status'] = __self.currency.bets_status;
  56. data['board'] = __self.currency.bets_board;
  57. data['viewers'] = __self.currency.bets_viewers;
  58. data['total_viewers'] = __self.currency.bets_viewers.length;
  59. res.send(data);
  60. });
  61. __self.srv.get('/api/bet/status', function(req, res){
  62. // Returns the current bet status
  63. res.send(__self.currency.bets_status);
  64. });
  65. __self.srv.get('/api/bet/board', function(req, res){
  66. // Returns the current bet board
  67. res.send(__self.currency.bets_board);
  68. });
  69. __self.srv.get('/api/bet/viewers', function(req, res){
  70. // Returns the current bet viewers
  71. res.send(__self.currency.bets_viewers);
  72. });
  73. __self.srv.listen(__self.port);
  74. console.log('Started website at '+__self.port);
  75. };
  76. module.exports = function (db, currency, options) {
  77. return new API(db, currency, options);
  78. };