f0ckv1/src/cfg.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-11-22 05:26:12 +00:00
var mysql = require('mysql');
var self = Cfg.prototype;
var haDC = () => {
self.sql = mysql.createConnection(require('../cfg/mysql.json'));
self.sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
self.sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
module.exports = Cfg;
function Cfg() {
}
self.read = (cb, kat, key) => {
// main, websrv, irc
var out = {};
self.sql.query("select * from `f0ck`.`cfg`" + (kat?" where `class` = '"+kat+"'"+(key?" && `key` = '"+key+"'":""):""), (err, rows) => {
if(err || rows.length < 1)
cb(err);
else {
rows.forEach(e => {
out[e.key] = ((type, value) => {
switch(type) {
case 'string':
return value;
break;
case 'int':
return parseInt(value);
break;
case 'bool':
return (value === 'true')?true:false;
break;
case 'json':
return JSON.parse(value);
break;
}
})(e.type, e.value);
});
if(cb)
cb( key?out[key]:out );
}
});
};