token-action-hud-gurps/scripts/roll-handler.js
2024-05-18 19:31:09 +10:00

175 lines
6.3 KiB
JavaScript

import {doRoll} from "/systems/gurps/module/dierolls/dieroll.js"
// Do we need to use executeOTF?
export let RollHandler = null
Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
/**
* Extends Token Action HUD Core's RollHandler class and handles action events triggered when an action is clicked
*/
RollHandler = class RollHandler extends coreModule.api.RollHandler {
/**
* Handle action click
* Called by Token Action HUD Core when an action is left or right-clicked
* @override
* @param {object} event The event
* @param {string} encodedValue The encoded value
*/
async handleActionClick (event, encodedValue) {
const [actionTypeId, actionId] = encodedValue.split('|')
const renderable = ['item']
if (renderable.includes(actionTypeId) && this.isRenderItem()) {
return this.doRenderItem(this.actor, actionId)
}
const knownCharacters = ['character']
// If single actor is selected
if (this.actor) {
await this.#handleAction(event, this.actor, this.token, actionTypeId, actionId)
return
}
const controlledTokens = canvas.tokens.controlled
.filter((token) => knownCharacters.includes(token.actor?.type))
// If multiple actors are selected
for (const token of controlledTokens) {
const actor = token.actor
await this.#handleAction(event, actor, token, actionTypeId, actionId)
}
}
/**
* Handle action hover
* Called by Token Action HUD Core when an action is hovered on or off
* @override
* @param {object} event The event
* @param {string} encodedValue The encoded value
*/
async handleActionHover (event, encodedValue) {}
/**
* Handle group click
* Called by Token Action HUD Core when a group is right-clicked while the HUD is locked
* @override
* @param {object} event The event
* @param {object} group The group
*/
async handleGroupClick (event, group) {}
/**
* Handle action
* @private
* @param {object} event The event
* @param {object} actor The actor
* @param {object} token The token
* @param {string} actionTypeId The action type id
* @param {string} actionId The actionId
*/
async #handleAction (event, actor, token, actionTypeId, actionId) {
switch (actionTypeId) {
case 'item':
this.#handleItemAction(event, actor, actionId)
break
case 'utility':
this.#handleUtilityAction(token, actionId)
break
case 'attributes':
this.#handleAttributeAction(actor, actionId)
break
case 'skills':
this.#handleSkillAction(actor, actionId)
break
case 'spells':
this.#handleSpellAction(actor, actionId)
break
case 'melee':
this.#handleMeleeAction(actor, actionId)
break
case 'ranged':
this.#handleRangedAction(actor, actionId)
break
}
}
/**
* Handle item action
* @private
* @param {object} event The event
* @param {object} actor The actor
* @param {string} actionId The action id
*/
#handleItemAction (event, actor, actionId) {
const item = actor.items.get(actionId)
item.toChat(event)
}
/**
* Handle utility action
* @private
* @param {object} token The token
* @param {string} actionId The action id
*/
async #handleUtilityAction (token, actionId) {
switch (actionId) {
case 'endTurn':
if (game.combat?.current?.tokenId === token.id) {
await game.combat?.nextTurn()
}
break
}
}
async #handleAttributeAction(actor, actionId) {
// actor, formula, targetmods, prefix = '', thing = '', chatthing = '', origtarget = -1, optionalArgs = {},
if ( actionId == "WILL" ) actionId = "Will";
if ( actionId == "PER" ) actionId = "Per";
let formula = '3d6';
let targetmods = null;
let prefix = "Rolls vs" // should be i18n of 'GURPS.rollVs'
let attrname = coreModule.api.Utils.i18n(GURPS.attributeNames[actionId])
let thing = attrname; // actionID ?
let chatthing = attrname + "/[@" + actor.id + "@" + coreModule.api.Utils.i18n(GURPS.attributes[actionId]) +"]";
let target = actor.system.attributes[actionId.toUpperCase()].value;
let opt = {};
doRoll({ actor, formula, targetmods, prefix, thing, chatthing, origtarget: target, optionalArgs: opt })
}
async #handleMeleeAction(actor, actionId) {
/* Melee attacks are done using the otf method. Grab the otf and pass it to the function. */
let GURPS = globalThis.GURPS;
let otf = actionId;
GURPS.executeOTF(otf, actor=actor);
}
async #handleRangedAction(actor, actionId) {
/* Melee attacks are done using the otf method. Grab the otf and pass it to the function. */
let GURPS = globalThis.GURPS;
let otf = actionId;
GURPS.executeOTF(otf, actor=actor);
}
async #handleSkillAction(actor, actionId) {
/* Skills are done using the otf method. Grab the otf and pass it to the function. */
let GURPS = globalThis.GURPS;
let otf = actionId;
GURPS.executeOTF(otf, actor=actor);
}
async #handleSpellAction(actor, actionId) {
/* Spells are done using the otf method. Grab the otf and pass it to the function. */
let GURPS = globalThis.GURPS;
let otf = actionId;
GURPS.executeOTF(otf, actor=actor);
}
}
})