Add a reconstructor and a combineWith function to savefile.js

This commit is contained in:
chylex 2016-10-23 23:11:18 +02:00
parent 14b90cd624
commit 74151822c5

View File

@ -65,14 +65,25 @@
* }
*/
var SAVEFILE = function(){
this.meta = {};
this.meta.users = {};
this.meta.userindex = [];
this.meta.servers = [];
this.meta.channels = {};
this.data = {};
var SAVEFILE = function(parsedObj){
if (parsedObj){
this.meta = parsedObj.meta;
this.meta.users = this.meta.users || {};
this.meta.userindex = this.meta.userindex || [];
this.meta.servers = this.meta.servers || [];
this.meta.channels = this.meta.channels || {};
this.data = parsedObj.data;
}
else{
this.meta = {};
this.meta.users = {};
this.meta.userindex = [];
this.meta.servers = [];
this.meta.channels = {};
this.data = {};
}
this.tmp = {};
this.tmp.userlookup = {};
@ -87,6 +98,9 @@ SAVEFILE.prototype.findOrRegisterUser = function(userId, userName){
this.meta.userindex.push(userId);
return this.tmp.userlookup[userId] = this.meta.userindex.length-1;
}
else if (!(userId in this.tmp.userlookup)){
return this.tmp.userlookup[userId] = this.meta.userindex.findIndex(id => id == userId);
}
else{
return this.tmp.userlookup[userId];
}
@ -188,6 +202,24 @@ SAVEFILE.prototype.addMessagesFromDiscord = function(channelId, discordMessageAr
return wasUpdated;
};
SAVEFILE.prototype.combineWith = function(obj){
for(var userId in obj.meta.users){
this.findOrRegisterUser(userId, obj.meta.users[userId].name);
}
for(var channelId in obj.meta.channels){
var oldServer = obj.meta.servers[obj.meta.channels[channelId].server];
var serverIndex = this.findOrRegisterServer(oldServer.name, oldServer.type);
this.tryRegisterChannel(serverIndex, channelId, obj.meta.channels[channelId].name);
}
for(var channelId in obj.data){
for(var messageId in obj.data[channelId]){
this.addMessage(channelId, messageId, obj.data[channelId][messageId]);
}
}
};
SAVEFILE.prototype.toJson = function(){
return JSON.stringify({
meta: this.meta,