|
@ -1,7 +1,4 @@ |
|
|
var express = require('express'), |
|
|
|
|
|
bodyParser = require('body-parser'), |
|
|
|
|
|
utils = require('./utils.js'), |
|
|
|
|
|
https = require('https'); |
|
|
|
|
|
|
|
|
var http = require('http'); |
|
|
|
|
|
|
|
|
//---- Construct ----
|
|
|
//---- Construct ----
|
|
|
function API(db, currency, options) { |
|
|
function API(db, currency, options) { |
|
@ -9,78 +6,61 @@ function API(db, currency, options) { |
|
|
|
|
|
|
|
|
__self.db = db; |
|
|
__self.db = db; |
|
|
__self.currency = currency; |
|
|
__self.currency = currency; |
|
|
|
|
|
|
|
|
__self.port = options.port || 9000; |
|
|
__self.port = options.port || 9000; |
|
|
__self.statdir = options.statdir; |
|
|
|
|
|
__self.check_data = 4;//minutes
|
|
|
|
|
|
__self.first_check = true; |
|
|
|
|
|
__self.slogan = ''; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ---- Methods ----
|
|
|
// ---- Methods ----
|
|
|
API.prototype.start = function () { |
|
|
API.prototype.start = function () { |
|
|
var __self = this; |
|
|
var __self = this; |
|
|
__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')); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- 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); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// ---- Endpoints ----
|
|
|
|
|
|
__self.srv.get('/api/ladder/all', function(req, res){ |
|
|
|
|
|
// Returns the entire ladder from top to bottom
|
|
|
|
|
|
sql = 'SELECT * FROM viewers ORDER BY points DESC;'; |
|
|
|
|
|
__self.db.execute(sql, function(rows) { |
|
|
|
|
|
var data = new Object(); |
|
|
|
|
|
data['viewers'] = rows; |
|
|
|
|
|
data['total'] = rows.length; |
|
|
|
|
|
res.send(data); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
__self.srv.get('/api/ladder/:numarg([0-9]+)', function(req, res){ |
|
|
|
|
|
// Returns top int(:id) users from the ladder
|
|
|
|
|
|
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.params.numarg+';'; |
|
|
|
|
|
__self.db.execute(sql, function(rows) { |
|
|
|
|
|
var data = new Object(); |
|
|
|
|
|
data['viewers'] = rows; |
|
|
|
|
|
data['total'] = rows.length; |
|
|
|
|
|
res.send(data); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
__self.srv.get('/api/bet', function(req, res){ |
|
|
|
|
|
// Returns all of the bet data together
|
|
|
|
|
|
data = new Object(); |
|
|
|
|
|
data['status'] = __self.currency.bets_status; |
|
|
|
|
|
data['board'] = __self.currency.bets_board; |
|
|
|
|
|
data['viewers'] = __self.currency.bets_viewers; |
|
|
|
|
|
data['total_viewers'] = __self.currency.bets_viewers.length; |
|
|
|
|
|
res.send(data); |
|
|
|
|
|
}); |
|
|
|
|
|
__self.srv.get('/api/bet/status', function(req, res){ |
|
|
|
|
|
// Returns the current bet status
|
|
|
|
|
|
res.send(__self.currency.bets_status); |
|
|
|
|
|
}); |
|
|
|
|
|
__self.srv.get('/api/bet/board', function(req, res){ |
|
|
|
|
|
// Returns the current bet board
|
|
|
|
|
|
res.send(__self.currency.bets_board); |
|
|
|
|
|
}); |
|
|
|
|
|
__self.srv.get('/api/bet/viewers', function(req, res){ |
|
|
|
|
|
// Returns the current bet viewers
|
|
|
|
|
|
res.send(__self.currency.bets_viewers); |
|
|
|
|
|
|
|
|
__self.srv = http.createServer( function(req, res) { |
|
|
|
|
|
if (req.method == "GET") { |
|
|
|
|
|
if (req.url == "/api/ladder/all") { |
|
|
|
|
|
// Returns the entire ladder from top to bottom
|
|
|
|
|
|
sql = 'SELECT * FROM viewers ORDER BY points DESC;'; |
|
|
|
|
|
__self.db.execute(sql, function(rows) { |
|
|
|
|
|
var data = new Object(); |
|
|
|
|
|
data['viewers'] = rows; |
|
|
|
|
|
data['total'] = rows.length; |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(data)); |
|
|
|
|
|
}); |
|
|
|
|
|
} else if (/^\/api\/ladder\/[0-9]+$/.test(req.url)) { |
|
|
|
|
|
// Returns top X users from the ladder
|
|
|
|
|
|
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.url.match(/\d+/g)+';'; |
|
|
|
|
|
__self.db.execute(sql, function(rows) { |
|
|
|
|
|
var data = new Object(); |
|
|
|
|
|
data['viewers'] = rows; |
|
|
|
|
|
data['total'] = rows.length; |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(data)); |
|
|
|
|
|
}); |
|
|
|
|
|
} else if (req.url == "/api/bet") { |
|
|
|
|
|
// Returns all of the bet data together
|
|
|
|
|
|
data = new Object(); |
|
|
|
|
|
data['status'] = __self.currency.bets_status; |
|
|
|
|
|
data['board'] = __self.currency.bets_board; |
|
|
|
|
|
data['viewers'] = __self.currency.bets_viewers; |
|
|
|
|
|
data['total_viewers'] = __self.currency.bets_viewers.length; |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(data)); |
|
|
|
|
|
} else if (req.url == "/api/bet/status") { |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(__self.currency.bets_status)); |
|
|
|
|
|
} else if (req.url == "/api/bet/board") { |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(__self.currency.bets_board)); |
|
|
|
|
|
} else if (req.url == "/api/bet/viewers") { |
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end(JSON.stringify(__self.currency.bets_viewers)); |
|
|
|
|
|
} else { |
|
|
|
|
|
res.writeHead(404, {'Content-Type': 'application/json'}); |
|
|
|
|
|
res.end("404"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
__self.srv.listen(__self.port); |
|
|
|
|
|
console.log('Started website at '+__self.port); |
|
|
|
|
|
|
|
|
__self.srv.listen(__self.port, '0.0.0.0'); |
|
|
|
|
|
console.log('Serving api data at '+__self.port); |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|