Try this version: /* global TokenMod */
on('ready',()=>{
const OwnerAttrName = 'OwnerRoll20IDs';
const assureOwnerID = (c, p, force = false) => {
let cb = c.get('controlledby');
if(cb.length && ! /\ball\b/i.test(cb)) {
let shouldUpdate = force;
let props = {
type: 'attribute',
characterid: c.id,
name: OwnerAttrName
};
let attr = findObjs(props)[0];
if(!attr) {
attr = createObj('attribute', props);
shouldUpdate = true;
}
if(shouldUpdate){
attr.set({
current: c.get('controlledby')
.split(/,/)
.map( id => findObjs({ type: 'player', id })[0] )
.filter( p => undefined !== p )
.map( p => p.get('d20userid') )
.join(',')
});
return 1;
}
}
return 0;
};
const restoreOwnersOnCharacter = (c) => {
let props = {
type: 'attribute',
characterid: c.id,
name: OwnerAttrName
};
let attr = findObjs(props)[0];
if(attr){
let controlList = attr.get('current')
.split(/,/)
.map( id => findObjs({ type: 'player', d20userid: id})[0] )
.filter( p => undefined !== p )
.map( p => p.id)
.join(',')
;
c.set({
controlledby: controlList,
inplayerjournals: controlList
});
return 1;
}
return 0;
};
const considerChangeOnToken = (t) => setTimeout( () => {
let token = getObj('graphic',t.id);
if(token){
let c = getObj('character',token.get('represents'));
if(c){
assureOwnerID(c,null,true);
}
}
}, 100);
on('change:character:controlledby', assureOwnerID );
on('add:character', restoreOwnersOnCharacter );
if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){
TokenMod.ObserveTokenChange(considerChangeOnToken);
}
on('chat:message', (msg)=>{
if('api' === msg.type && /^!update-ownership\b/i.test(msg.content) && playerIsGM(msg.playerid) ){
let chars = findObjs({type: 'character'});
let updates = 0;
sendChat('',`/w gm <div><b>Considering <code>${chars.length}</code> characters for update.</b></div>`);
const burndown = ()=>{
let c = chars.shift();
if(c){
updates += assureOwnerID(c,null,true);
setTimeout(burndown,0);
} else {
sendChat('',`/w gm <div><b>Updated ownership on <code>${updates}</code> characters.</b></div>`);
}
};
burndown();
}
if('api' === msg.type && /^!update-controlledby\b/i.test(msg.content) && playerIsGM(msg.playerid) ){
let chars = findObjs({type: 'attribute', name: OwnerAttrName})
.map(a=>getObj('character',a.get('characterid')))
.filter( c => undefined !== c);
let updates = 0;
sendChat('',`/w gm <div><b>Considering <code>${chars.length}</code> character owners for update.</b></div>`);
const burndown = ()=>{
let c = chars.shift();
if(c){
updates += restoreOwnersOnCharacter(c);
setTimeout(burndown,0);
} else {
sendChat('',`/w gm <div><b>Updated controlledby on <code>${updates}</code> characters.</b></div>`);
}
};
burndown();
}
});
});