From c4d5b1cd1d4ae41a9606eeeb9e8f06223950bf7c Mon Sep 17 00:00:00 2001 From: calzoneman Date: Thu, 15 Aug 2013 14:43:35 -0500 Subject: [PATCH] finish refactoring acp (kind of -- depends on auth --- acp.js | 70 ++++++++++++++++++++--------------------------------- database.js | 25 +++++++++++++++++++ 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/acp.js b/acp.js index 7115e413..124a0099 100644 --- a/acp.js +++ b/acp.js @@ -70,30 +70,23 @@ module.exports = function (Server) { user.socket.on("acp-reset-password", function(data) { if(Auth.getGlobalRank(data.name) >= user.global_rank) return; - try { - var hash = Server.db.generatePasswordReset(user.ip, data.name, data.email); - ActionLog.record(user.ip, user.name, "acp-reset-password", data.name); - } - catch(e) { - user.socket.emit("acp-reset-password", { - success: false, - error: e - }); - return; - } - if(hash) { - user.socket.emit("acp-reset-password", { - success: true, - hash: hash - }); - } - else { - user.socket.emit("acp-reset-password", { - success: false, - error: "Reset failed" - }); - } + db.genPasswordReset(user.ip, data.name, data.email, + function (err, hash) { + var pkt = { + success: !err + }; + + if(err) { + pkt.error = err; + } else { + pkt.hash = hash; + } + + user.socket.emit("acp-reset-password", pkt); + ActionLog.record(user.ip, user.name, + "acp-reset-password", data.name); + }); }); user.socket.on("acp-set-rank", function(data) { @@ -103,21 +96,13 @@ module.exports = function (Server) { if(Auth.getGlobalRank(data.name) >= user.global_rank) return; - var db = Server.db.getConnection(); - if(!db) - return; + db.setGlobalRank(data.name, data.rank, function (err, res) { - ActionLog.record(user.ip, user.name, "acp-set-rank", data); - var query = Server.db.createQuery( - "UPDATE registrations SET global_rank=? WHERE uname=?", - [data.rank, data.name] - ); - - var res = db.querySync(query); - if(!res) - return; - - user.socket.emit("acp-set-rank", data); + ActionLog.record(user.ip, user.name, "acp-set-rank", + data); + if(!err) + user.socket.emit("acp-set-rank", data); + }); }); user.socket.on("acp-list-loaded", function() { @@ -174,13 +159,10 @@ module.exports = function (Server) { }); user.socket.on("acp-view-stats", function () { - var db = Server.db.getConnection(); - if(!db) - return; - var query = "SELECT * FROM stats ORDER BY time ASC"; - var results = db.querySync(query); - if(results) - user.socket.emit("acp-view-stats", results.fetchAllSync()); + db.listStats(function (err, res) { + if(!err) + user.socket.emit("acp-view-stats", res); + }); }); } } diff --git a/database.js b/database.js index b9e0bd5a..2b739e08 100644 --- a/database.js +++ b/database.js @@ -38,6 +38,9 @@ Database.prototype.query = function (query, sub, callback) { sub = false; } + if(typeof callback !== "function") + callback = blackHole; + var self = this; self.pool.getConnection(function (err, conn) { if(err) { @@ -667,6 +670,17 @@ Database.prototype.searchUser = function (name, callback) { self.query(query, [name], callback); }; +/* rank */ + +Database.prototype.setGlobalRank = function (name, rank, callback) { + var self = this; + if(typeof callback !== "function") + callback = blackHole; + + var query = "UPDATE registrations SET global_rank=? WHERE uname=?"; + self.query(query, [rank, name], callback); +}; + /* email and profile */ Database.prototype.getUserProfile = function (name, callback) { @@ -983,4 +997,15 @@ Database.prototype.listIPsForName = function (name, callback) { }; /* END REGION */ + +/* REGION stats */ + +Database.prototype.listStats = function (callback) { + var self = this; + if(typeof callback !== "function") + return; + + var query = "SELECT * FROM stats ORDER BY time ASC"; + self.query(query, callback); +}; module.exports = Database;