93 lines
2.4 KiB
JavaScript

export class helpers {
static runOnTargetOrSelectedTokens (runFunc) {
let tokens = []
const targets = Array.from(game.user.targets)
if (targets.length > 0) {
tokens = targets
} else if (canvas.tokens.controlled.length > 0) {
tokens = canvas.tokens.controlled
}
if (tokens.length > 0) {
runFunc(tokens)
} else {
ui.notifications.error('Please select or target a token')
}
}
static createEffectDocument (icon, name, durationRounds, changes) {
const effectData = {
icon,
id: name,
label: name,
duration: { rounds: durationRounds },
flags: {
swade: {
expiration: CONFIG.SWADE.CONST.STATUS_EFFECT_EXPIRATION.EndOfTurnPrompt,
loseTurnOnHold: true
}
},
changes
}
return effectData
}
static createMutationWithEffect (icon, name, durationRounds, changes) {
const effect = helpers.createEffectDocument(icon, name, durationRounds, changes)
const mutate = {
embedded: { ActiveEffect: {} }
}
mutate.embedded.ActiveEffect[name] = effect
return mutate
}
static defaultMutationOptions (name) {
const mutateOptions = {
comparisonKeys: { ActiveEffect: 'label' },
name,
permanent: true,
description: name
}
return mutateOptions
}
static getActorFolderByPath (path) {
const names = path.split('/')
if (names[0] === '') {
names.shift()
}
let name = names.shift()
let folder = game.folders.find(f => f.name === name && !f.folder)
while (names.length > 0) {
name = names.shift()
folder = folder.children.find(c => c.folder.name === name)
folder = folder.folder
}
return folder
}
static getActorsInFolder (inFolder) {
const prefixStack = ['']
const actors = {}
const folderStack = [inFolder]
while (folderStack.length > 0) {
const prefix = prefixStack.shift()
const folder = folderStack.shift()
for (const actor of folder.contents) {
if (
game.user.isGM || actor.testUserPermission(
game.user, foundry.CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER)
) {
actors[`${prefix}${actor.name}`] = actor
}
}
for (const child of folder.children) {
const newPrefix = `${prefix}${child.folder.name} | `
prefixStack.push(newPrefix)
folderStack.push(child.folder)
}
}
return actors
}
}