Clean up loadDump

This commit is contained in:
calzoneman 2013-11-24 23:20:41 -06:00
parent 01464ed394
commit 0945fc0d7b
2 changed files with 127 additions and 110 deletions

View file

@ -29,7 +29,9 @@ var InfoGetter = require("./get-info");
var Channel = function(name) {
var self = this;
Logger.syslog.log("Opening channel " + name);
self.initialized = false;
self.shouldDump = false;
self.ready = false;
self.pendingJoins = [];
self.dbloaded = false;
self.server = Server.getServer();
@ -137,9 +139,7 @@ var Channel = function(name) {
else if (!err || err === "channel_unregistered")
self.dbloaded = true;
if(self.registered) {
self.loadDump();
}
self.tryLoadDump();
});
}
@ -160,19 +160,29 @@ Channel.prototype.hasPermission = function(user, key) {
}
/* REGION Channel data */
Channel.prototype.loadDump = function() {
Channel.prototype.tryLoadDump = function () {
var self = this;
if(self.name === "")
// Not sure how this would ever happen, but it was there before
if (self.name === "") {
return;
}
if (!self.registered) {
self.ready = true;
self.handlePendingJoins();
return;
}
fs.stat(path.join(__dirname, "../chandump", self.name),
function (err, stats) {
if (self.dead)
return;
if(!err) {
if (!err) {
var mb = stats.size / 1048576;
mb = parseInt(mb * 100) / 100;
if(mb > 1) {
if (mb > 1) {
Logger.errlog.log("Large chandump detected: " + self.name +
" (" + mb + " MB)");
self.updateMotd("Your channel file has exceeded the " +
@ -182,122 +192,112 @@ Channel.prototype.loadDump = function() {
return;
}
}
fs.readFile(path.join(__dirname, "../chandump", self.name),
function(err, data) {
if (self.dead)
return;
if(err) {
if(err.code == "ENOENT") {
Logger.errlog.log("WARN: missing dump for " + self.name);
self.initialized = true;
self.saveDump();
}
else {
Logger.errlog.log("Failed to open channel dump " + self.name);
Logger.errlog.log(err);
}
return;
self.loadDump();
});
};
Channel.prototype.loadDump = function() {
var self = this;
if (self.dead || !self.name) {
return;
}
fs.readFile(path.join(__dirname, "../chandump", self.name),
function(err, data) {
if (self.dead)
return;
if (err) {
if (err.code == "ENOENT") {
self.shouldDump = true;
self.ready = true;
self.handlePendingJoins();
self.saveDump();
} else {
Logger.errlog.log("Failed to open channel dump " + self.name);
Logger.errlog.log(err);
self.ready = true;
self.handlePendingJoins();
}
try {
self.logger.log("*** Loading channel dump from disk");
data = JSON.parse(data);
/* Load the playlist */
return;
}
// Old
if(data.queue) {
if(data.position < 0)
data.position = 0;
for(var i = 0; i < data.queue.length; i++) {
var e = data.queue[i];
var m = new Media(e.id, e.title, e.seconds, e.type);
var p = self.playlist.makeItem(m);
p.queueby = data.queue[i].queueby ? data.queue[i].queueby
: "";
p.temp = e.temp;
self.playlist.items.append(p);
if(i == data.position)
self.playlist.current = p;
}
try {
self.logger.log("*** Loading channel dump from disk");
data = JSON.parse(data);
/* Load the playlist */
if ("playlist" in data) {
// TODO change callback to return value
self.playlist.load(data.playlist, function() {
if (self.dead)
return;
self.sendAll("playlist", self.playlist.items.toArray());
self.broadcastPlaylistMeta();
self.playlist.startPlayback();
}
// Current
else if(data.playlist) {
self.playlist.load(data.playlist, function() {
if (self.dead)
return;
self.sendAll("playlist", self.playlist.items.toArray());
self.broadcastPlaylistMeta();
self.playlist.startPlayback(data.playlist.time);
});
}
for(var key in data.opts) {
// Gotta love backwards compatibility
if(key == "customcss" || key == "customjs") {
var k = key.substring(6);
self.opts[k] = data.opts[key];
}
else {
self.opts[key] = data.opts[key];
}
}
for(var key in data.permissions) {
self.permissions[key] = data.permissions[key];
}
self.sendAll("setPermissions", self.permissions);
self.broadcastOpts();
self.users.forEach(function (u) {
u.autoAFK();
self.playlist.startPlayback(data.playlist.time);
});
if(data.filters) {
for(var i = 0; i < data.filters.length; i++) {
var f = data.filters[i];
// Backwards compatibility
if(f[0] != undefined) {
var filt = new Filter("", f[0], "g", f[1]);
filt.active = f[2];
self.updateFilter(filt, false);
}
else {
var filt = new Filter(f.name, f.source, f.flags, f.replace);
filt.active = f.active;
filt.filterlinks = f.filterlinks;
self.updateFilter(filt, false);
}
}
self.broadcastChatFilters();
}
if(data.motd) {
self.motd = data.motd;
self.broadcastMotd();
}
self.setLock(!(data.openqueue || false));
self.chatbuffer = data.chatbuffer || [];
for(var i = 0; i < self.chatbuffer.length; i++) {
self.sendAll("chatMsg", self.chatbuffer[i]);
}
self.css = data.css || "";
self.js = data.js || "";
self.sendAll("channelCSSJS", {css: self.css, js: self.js});
self.initialized = true;
}
catch(e) {
Logger.errlog.log("Channel dump load failed: ");
Logger.errlog.log(e.stack);
/* Load playlist lock */
self.setLock(!(data.openqueue || false));
/* Load configurable options */
if ("customcss" in data.opts) {
data.opts.css = data.opts.customcss;
}
});
if ("customjs" in data.opts) {
data.opts.js = data.opts.customjs;
}
for (var key in data.opts) {
self.opts[key] = data.opts[key];
}
/* Load permissions */
for (var key in data.permissions) {
self.permissions[key] = data.permissions[key];
}
/* Load chat filters */
// TODO change the way default filters work?
for (var i = 0; i < data.filters.length; i++) {
var f = data.filters[i];
// Old filter system used to use arrays
if (f instanceof Array) {
continue;
}
var filt = new Filter(f.name, f.source, f.flags, f.replace);
filt.active = f.active;
filt.filterlinks = f.filterlinks;
self.updateFilter(filt, false);
}
/* Load MOTD */
if (data.motd) {
self.motd = data.motd;
}
/* Load chat history */
self.chatbuffer = data.chatbuffer || [];
/* Load CSS, JS */
self.css = data.css || "";
self.js = data.js || "";
self.ready = true;
self.shouldDump = true;
self.handlePendingJoins();
} catch(e) {
Logger.errlog.log("Channel dump load failed: ");
Logger.errlog.log(e.stack);
}
});
}
Channel.prototype.saveDump = function() {
if (this.dead)
return;
if(!this.initialized || this.name === "")
if (!this.shouldDump || this.name === "")
return;
var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) {
for (var i = 0; i < this.filters.length; i++) {
filts[i] = this.filters[i].pack();
}
var dump = {
@ -425,7 +425,7 @@ Channel.prototype.tryRegister = function (user) {
if (self.dead)
return;
self.registered = true;
self.initialized = true;
self.shouldDump = true;
self.saveDump();
self.saveRank(user);
user.socket.emit("registerChannel", {
@ -800,8 +800,23 @@ Channel.prototype.search = function(query, callback) {
/* REGION User interaction */
Channel.prototype.handlePendingJoins = function () {
var self = this;
self.pendingJoins.forEach(function (u) {
self.userJoin(u);
});
delete self["pendingJoins"];
};
Channel.prototype.userJoin = function(user) {
var self = this;
if (!self.ready) {
if (!("pendingJoins" in self)) {
self.pendingJoins = [];
}
self.pendingJoins.push(user);
return;
}
var parts = user.ip.split(".");
var slash24 = parts[0] + "." + parts[1] + "." + parts[2];
// GTFO
@ -820,6 +835,9 @@ Channel.prototype.userJoin = function(user) {
// Join the socket pool for this channel
user.socket.join(this.name);
// Enable AFK timer
user.autoAFK();
// Prevent duplicate login
if(user.name != "") {
for(var i = 0; i < this.users.length; i++) {

View file

@ -156,7 +156,6 @@ User.prototype.initCallbacks = function () {
});
}
self.channel.userJoin(self);
self.autoAFK();
});
self.socket.on("login", function (data) {