Minetrack/lib/database.js

32 lines
839 B
JavaScript
Raw Normal View History

2015-12-18 07:45:38 +00:00
var util = require('./util');
exports.setup = function() {
var sqlite = require('sqlite3');
2015-12-11 04:06:27 +00:00
var db = new sqlite.Database('database.sql');
2015-12-11 04:06:27 +00:00
db.serialize(function() {
2015-12-11 04:25:24 +00:00
db.run('CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)');
2015-12-11 04:06:27 +00:00
});
exports.log = function(ip, timestamp, playerCount) {
2015-12-11 04:25:24 +00:00
var insertStatement = db.prepare('INSERT INTO pings (timestamp, ip, playerCount) VALUES (?, ?, ?)');
2015-12-18 07:45:38 +00:00
db.serialize(function() {
insertStatement.run(timestamp, ip, playerCount);
});
insertStatement.finalize();
};
exports.queryPings = function(duration, callback) {
var currentTime = util.getCurrentTimeMs();
db.all("SELECT * FROM pings WHERE timestamp >= ? AND timestamp <= ?", [
currentTime - duration,
currentTime
], function(err, data) {
callback(data);
});
};
2015-12-11 04:06:27 +00:00
};