56 lines
1.6 KiB
JavaScript

import { moduleName } from './globals.js'
import { PowerClasses } from './allPowers.js'
export async function powerEffectManagementHook(effect, data, userId) {
if (game.user.id !== userId) {
return
}
const maintId = effect.getFlag(moduleName, 'maintainingId')
if (!maintId) {
return
}
const mutateOptions = {
permanent: true,
comparisonKeys: {
ActiveEffect: 'id'
}
}
const targetIds = effect.getFlag(moduleName, 'targetIds') || []
for (const targetId of targetIds) {
const mutation = {
embedded: { ActiveEffect: {} }
}
const target = canvas.tokens.get(targetId)
if (!target) {
continue
}
const effects = target.actor.effects.filter(
e => e.getFlag(moduleName, 'maintId') === maintId)
for (const effect of effects) {
mutation.embedded.ActiveEffect[effect.id] = warpgate.CONST.DELETE
}
mutateOptions.description = `${effect.parent.name} is no longer ${effect.name} on ${target.name}`
await warpgate.mutate(target.document, mutation, {}, mutateOptions)
}
}
export async function powers (options = {}) {
const token = 'token' in options ? options.token : null
if (token === undefined || token === null) {
ui.notifications.error('Please select one token to be the caster')
return
}
const targets = 'targets' in options ? Array.from(options.targets) : []
const item = 'item' in options ? options.item : null
const swid = options?.name || item?.system.swid || null
if (swid in PowerClasses) {
const runner = new PowerClasses[swid](token, targets)
runner.powerEffect()
return
}
ui.notifications.error(`No power effect found for ${name}`)
}