add roll-requesting function

This commit is contained in:
Mike Bloy 2023-11-18 19:28:12 -06:00
parent 57b00686c8
commit 0fba8a73b0

View File

@ -7,52 +7,55 @@ export async function requestRollFromTokens (tokens, rollType, rollDesc, options
// options: // options:
// title: title for the roll dialog. Will have "- {{ token name }}" // title: title for the roll dialog. Will have "- {{ token name }}"
// appended // appended
// flavour: flavor text for the roll card. Defaults to title
// mods: list of modifiers {label: "", value: 0, ignore: false} // mods: list of modifiers {label: "", value: 0, ignore: false}
// modCallback: callback function that takes a token and returns a list of // modCallback: callback function that takes a token and returns a list of
// modifiers in the same format as modifiers, above // modifiers in the same format as modifiers, above
options.requestingUserId = shim.user.id const requestingUser = shim.user
const title = options?.title || `${requestingUser.name} requests a ${rollDesc} roll`
const flavour = options?.flavour || options?.flavor || title
const promises = [] const promises = []
for (const token in tokens) { for (const token of tokens) {
const owner = shim.warpgateUtil.firstOwner(token.document) const owner = shim.warpgateUtil.firstOwner(token.document)
promises.append(shim.socket.executeAsUser(requestTokenRoll, owner.id, const rollOpts = {
token.scene.id, token.id, rollType, rollDesc, options)) title: `${title} - ${token.name}`,
flavour
}
const additionalMods = []
if ('mods' in options) {
for (const mod of options.mods) {
additionalMods.push(mod)
}
}
if ('modCallback' in options) {
const tokenMods = await options.modCallback(token)
for (const tm of tokenMods) {
additionalMods.push(tm)
}
}
if (additionalMods.length > 0) {
rollOpts.additionalMods = additionalMods
}
promises.push(shim.socket.executeAsUser(requestTokenRoll, owner.id,
token.scene.id, token.id, rollType, rollDesc, rollOpts))
} }
results = await Promise.allSettled(promises) const results = await Promise.allSettled(promises)
return results return results
} }
export async function requestTokenRoll ( export async function requestTokenRoll (
sceneId, tokenId, rollType, rollDesc, options sceneId, tokenId, rollType, rollDesc, options
) { ) {
const requestingUser = shim.users.get(options.requestingUserId)
const scene = shim.scenes.get(sceneId) const scene = shim.scenes.get(sceneId)
const token = scene.tokens.get(tokenId) const token = scene.tokens.get(tokenId)
const additionalMods = [] let rollFunc = 'rollAttribute'
if ('mods' in options) {
for (const mod of options.mods) {
additionalMods.push(mod)
}
}
if ('modCallback' in options) {
const tokenMods = await options.modCallback(token)
for (const tm of tokenMods) {
additionalMods.push(tm)
}
}
let rollFunc = token.actor.rollAttribute
let rollId = rollDesc.toLowerCase() let rollId = rollDesc.toLowerCase()
if (rollType === 'skill') { if (rollType === 'skill') {
rollFunc = token.actor.rollSkill rollFunc = 'rollSkill'
rollId = token.actor.items.find(i => ( rollId = token.actor.items.filter(i => i.type === 'skill').find(i => (
(i.system.swid === rollDesc.toLowerCase() || i.system.swid === rollDesc.toLowerCase() ||
i.name.toLowerCase() === rollDesc.toLowerCase()) && i.type === 'skill')) i.name.toLowerCase() === rollDesc.toLowerCase()))?.id
} }
const rollOpts = { const result = await token.actor[rollFunc](rollId, options)
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 } return { sceneId, tokenId, result }
} }