2.2.0 release #38

Merged
mike merged 8 commits from develop into main 2023-11-21 04:02:09 +00:00
4 changed files with 65 additions and 92 deletions
Showing only changes of commit 57b00686c8 - Show all commits

View File

@ -1,5 +1,5 @@
import { helpers } from './helpers.js' import { log } from './shim.js'
import { shim, log } from './shim.js' import { requestRollFromTokens } from './helpers.js'
import { powerEffects } from './powerEffects.js' import { powerEffects } from './powerEffects.js'
export class api { export class api {
@ -12,7 +12,8 @@ export class api {
const moduleName = 'swade-mb-helpers' const moduleName = 'swade-mb-helpers'
game.modules.get(moduleName).api = { game.modules.get(moduleName).api = {
DEBUG: true, DEBUG: true,
powerEffects powerEffects,
requestRollFromTokens
} }
} }
} }

View File

@ -1,92 +1,58 @@
import { CONST, shim } from './shim.js' import { shim } from './shim.js'
export class helpers { export async function requestRollFromTokens (tokens, rollType, rollDesc, options = {}) {
static runOnTargetOrSelectedTokens (runFunc) { // tokens: list of tokens to request a roll from
let tokens = [] // rollType: 'attribute' or 'skill
const targets = Array.from(shim.targets) // rollDesc: name of attribute or skill
if (targets.length > 0) { // options:
tokens = targets // title: title for the roll dialog. Will have "- {{ token name }}"
} else if (shim.controlled.length > 0) { // appended
tokens = shim.controlled // mods: list of modifiers {label: "", value: 0, ignore: false}
} // modCallback: callback function that takes a token and returns a list of
if (tokens.length > 0) { // modifiers in the same format as modifiers, above
runFunc(tokens) options.requestingUserId = shim.user.id
} else { const promises = []
shim.notifications.error('Please select or target a token') for (const token in tokens) {
const owner = shim.warpgateUtil.firstOwner(token.document)
promises.append(shim.socket.executeAsUser(requestTokenRoll, owner.id,
token.scene.id, token.id, rollType, rollDesc, options))
} }
results = await Promise.allSettled(promises)
return results
} }
static createEffectDocument (icon, name, durationRounds, changes) { export async function requestTokenRoll (
const effectData = { sceneId, tokenId, rollType, rollDesc, options
icon,
name,
duration: { rounds: durationRounds },
flags: {
swade: {
expiration: CONST.SWADE.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 = {
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 = shim.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 (
shim.user.isGM || actor.testUserPermission(
shim.user, CONST.FOUNDRY.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER)
) { ) {
actors[`${prefix}${actor.name}`] = actor const requestingUser = shim.users.get(options.requestingUserId)
const scene = shim.scenes.get(sceneId)
const token = scene.tokens.get(tokenId)
const additionalMods = []
if ('mods' in options) {
for (const mod of options.mods) {
additionalMods.push(mod)
} }
} }
for (const child of folder.children) { if ('modCallback' in options) {
const newPrefix = `${prefix}${child.folder.name} | ` const tokenMods = await options.modCallback(token)
prefixStack.push(newPrefix) for (const tm of tokenMods) {
folderStack.push(child.folder) additionalMods.push(tm)
} }
} }
return actors let rollFunc = token.actor.rollAttribute
let rollId = rollDesc.toLowerCase()
if (rollType === 'skill') {
rollFunc = token.actor.rollSkill
rollId = token.actor.items.find(i => (
(i.system.swid === rollDesc.toLowerCase() ||
i.name.toLowerCase() === rollDesc.toLowerCase()) && i.type === 'skill'))
} }
const rollOpts = {
title: options?.title || `${requestingUser.name} requests a ${rollDesc} roll`
}
if (additionalMods.length > 0) {
rollOpts.additionalMods = additionalMods
}
const result = await rollFunc(rollId, rollOpts)
return { sceneId, tokenId, result }
} }

View File

@ -1,4 +1,5 @@
import { api } from './api.js' import { api } from './api.js'
import { requestTokenRoll } from './helpers.js'
import { havocResult, shapeChangeOnDismiss } from './powerEffects.js' import { havocResult, shapeChangeOnDismiss } from './powerEffects.js'
import { log, shim } from './shim.js' import { log, shim } from './shim.js'
@ -25,5 +26,6 @@ Hooks.on('ready', () => {
Hooks.on('socketlib.ready', () => { Hooks.on('socketlib.ready', () => {
const socket = socketlib.registerModule('swade-mb-helpers') const socket = socketlib.registerModule('swade-mb-helpers')
socket.register('havocResult', havocResult) socket.register('havocResult', havocResult)
socket.register('requestTokenRoll', requestTokenRoll)
shim._socket = socket shim._socket = socket
}) })

View File

@ -41,6 +41,10 @@ export class shim {
return game.user return game.user
} }
static get users () {
return game.users
}
static get notifications () { static get notifications () {
return ui.notifications return ui.notifications
} }