152 lines
3.5 KiB
JavaScript

export class CONST {
static get SWADE () {
return CONFIG.SWADE.CONST
}
static get FOUNDRY () {
return foundry.CONST
}
static get WARPGATE () {
return warpgate.CONST
}
}
export class shim {
static get ActiveEffect () {
return ActiveEffect
}
static get folders () {
return game.folders
}
static get controlled () {
return canvas.tokens.controlled
}
static get targets () {
return game.user.targets
}
static get notifications () {
return ui.notifications
}
static get user () {
return game.user
}
static get actors () {
return game.actors
}
static mergeObject (...args) {
return mergeObject(...args)
}
static getStatus (label, name, favorite = true) {
const effect = JSON.parse(JSON.stringify(
CONFIG.statusEffects.find(se => se.label === label)))
effect.name = ('name' in effect ? effect.name : effect.label)
if (!('flags' in effect)) {
effect.flags = {}
}
if (favorite) {
if (!('swade' in effect.flags)) {
effect.flags.swade = {}
}
effect.flags.swade.favorite = true
}
effect.flags.core = { statusId: effect.id }
return effect
}
static createEffectDocument (icon, name, durationRounds, changes = null) {
if (changes === null) {
changes = []
}
return {
icon,
name,
duration: { rounds: durationRounds },
changes,
flags: {
swade: {
favorite: true,
expiration: CONST.SWADE.STATUS_EFFECT_EXPIRATION.EndOfTurnPrompt
}
}
}
}
static async applyActiveEffects (token, effectDocuments) {
const mutation = {
embedded: { ActiveEffect: {} }
}
const mutateOptions = {
permanent: true,
description: effectDocuments[effectDocuments.length - 1]
}
for (const effectDocument of effectDocuments) {
mutation.embedded.ActiveEffect[effectDocument.name] = effectDocument
}
await warpgate.mutate(token.document, mutation, {}, mutateOptions)
}
static warpgateMenu (menuData, menuOptions) {
return warpgate.menu(menuData, menuOptions)
}
static warpgateSpawn (...args) {
return warpgate.spawn(...args)
}
static getActorFolderByPath (path) {
const names = path.split('/')
if (names[0] === '') {
names.shift()
}
let name = names.shift()
let folder = shim.folders.filter(
f => f.type === 'Actor' && !f.folder
).find(f => f.name === name)
if (!folder) { return undefined }
while (names.length > 0) {
name = names.shift()
folder = folder.children.find(c => c.folder.name === name)
if (!folder) { return undefined }
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 (shim.user.isGM ||
actor.testUserPermission(
shim.user, CONST.FOUNDRY.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
}
}
export function log (...args) {
console.log('SWADE MB HELPERS |', ...args)
}