Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var copyCharacter = function(name){
var characters = findObjs({type:'character'});
var i = characters.length;
var old = _.filter(characters,function(o){return o.get('name') === name})[0];
if(old === undefined){log("Character "+name+" not found!");return;}
var nr = 0;
var c_name = name+" "+nr;
while(_.filter(characters,function(o){return o.get('name') === c_name}).length>0){
nr++;//Increment counter until we found a unique character
c_name = name+" "+nr;
}
var c_avatar = old.get('avatar') === undefined ? '':old.get('avatar');
var c_archived = old.get('archived') === undefined ? false:old.get('archived');
var c_inplayerjournals = old.get('inplayerjournals') === undefined ? '':old.get('inplayerjournals');
var chr = createObj('character', {
name: c_name,
avatar: c_avatar,
archived: c_archived,
inplayerjournals: c_inplayerjournals
});
var c_uuid = chr.get('id');
//Attributes
var old_attr = findObjs({type:'attribute',_characterid:old.get('id')});
var id_swap = {};
for(var attr in old_attr){
attr = old_attr[attr];
var attr_name = attr.get('name');
var c_current = attr.get('current') === undefined ? '0': attr.get('current');
var c_max = attr.get('max') === undefined ? '0': attr.get('max');
if(attr_name.indexOf("_reporder")!=-1){continue;}//Skip reporder
var matches = attr_name.match(/repeating_[^_]*_/g);
if(matches){
var prefix = matches[0];
//Remove prefix
var attr_id = attr_name.replace(/repeating_[^_]*_/g,"").replace(/_.*/g,"");
if(!(attr_id in id_swap)){
var new_row_id = generateUUID().replace(/_/g,'Z');
//log("Save row ID conversion "+new_row_id);
id_swap[attr_id] = new_row_id;
}
//Recall row_id conversion
var c_id = id_swap[attr_id];
//log("NEW ID:")
var c_attr_name = attr_name.replace(attr_id,c_id);
//log("Copy Attribute "+attr_name+" to "+c_attr_name);
createObj('attribute', {
characterid: c_uuid,
name: c_attr_name,
current: c_current,
max: c_max
});
}else{
//log("[ELSE] Process:"+attr_name);
//log(attr);
createObj('attribute', {
characterid: c_uuid,
name: attr_name,
current: c_current,
max: c_max
});
}
}
//Abilities
var old_abil = findObjs({type:'ability',_characterid:old.get('id')});
for(var abil in old_abil){
abil = old_abil[abil];
createObj('ability', {
characterid: c_uuid,
name: abil.get('name'),
description: abil.get('description'),
action: abil.get('action'),
istokenaction: abil.get('istokenaction')
});
}
log("Copied character "+name+" to "+name+"_"+nr);
//log(characters.get(c_name));
return chr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment