token-action-hud-gurps/scripts/action-handler.js

258 lines
8 KiB
JavaScript

// System Module Imports
import { ACTION_TYPE, ITEM_TYPE } from './constants.js'
import { Utils } from './utils.js'
export let ActionHandler = null
Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
/**
* Extends Token Action HUD Core's ActionHandler class and builds system-defined actions for the HUD
*/
ActionHandler = class ActionHandler extends coreModule.api.ActionHandler {
/**
* Build system actions
* Called by Token Action HUD Core
* @override
* @param {array} groupIds
*/a
async buildSystemActions (groupIds) {
// Set actor and token variables
this.actors = (!this.actor) ? this._getActors() : [this.actor]
this.actorType = this.actor?.type
// Settings
this.displayUnequipped = Utils.getSetting('displayUnequipped')
// Set items variable
if (this.actor) {
let items = this.actor.items
items = coreModule.api.Utils.sortItemsByName(items)
this.items = items
}
if (this.actorType === 'character') {
//debugger;
this.#buildCharacterActions()
} else if (!this.actor) {
this.#buildMultipleTokenActions()
}
}
/**
* Build character actions
* @private
*/
#buildCharacterActions () {
// debugger;
this._get_attributes({id: "attributes", type:"system"})
this._get_skills({id: "skills", type:"system"})
this._get_spells({id: "spells", type:"system"})
this._get_melee_attacks({id: "melee", type:"system"})
this._get_ranged_attacks({id: "ranged", type:"system"})
this.#buildInventory()
}
_get_attributes(parent) {
const macroType = "attributes";
let actions = [];
let attributes = Object.entries(this.actor.system.attributes)
attributes.forEach((a) => {
const key = a[0];
const value = a[1].value
// img
let actionId = key
if ( key == "WILL" ) actionId = "Will";
if ( key == "PER" ) actionId = "Per";
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n("GURPS.attributes" + key),
description: coreModule.api.Utils.i18n('GURPS.attributes'),
encodedValue: [macroType, key].join(this.delimiter),
})
});
this.addActions(actions, parent)
}
_get_melee_attacks(parent) {
// debugger;
const macroType = "melee";
let actions = [];
let melee_attacks = Object.entries(this.actor.system.melee)
melee_attacks.forEach((a) => {
let key = a[1].name;
if (a[1].mode) {
key += ` (${a[1].mode})`;
}
const value = a[1].level
// img
let actionId = key;
let otf = 'M:"' + key + '"'; // This is going to end badly if the delimiter is set to ":"
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.meleeWeapons'),
encodedValue: [macroType, otf].join(this.delimiter),
})
});
this.addActions(actions, parent)
}
_get_ranged_attacks(parent) {
// debugger;
const macroType = "ranged";
let actions = [];
let ranged_attacks = Object.entries(this.actor.system.ranged)
ranged_attacks.forEach((a) => {
let key = a[1].name;
if (a[1].mode) {
key += ` (${a[1].mode})`;
}
const value = a[1].level
// img
let actionId = key;
let otf = 'R:"' + key + '"'; // This is going to end badly if the delimiter is set to ":"
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.rangedWeapons'),
encodedValue: [macroType, otf].join(this.delimiter),
})
});
this.addActions(actions, parent)
}
_get_skills(parent) {
// debugger;
const macroType = "skills";
let actions = [];
let skills = Object.entries(this.actor.system.skills)
skills.forEach((a) => {
const key = a[1].name;
const value = a[1].level
// img
let actionId = key;
let otf = 'Sk:"' + key + '"'; // This is going to end badly if the delimiter is set to ":"
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.Skills'),
encodedValue: [macroType, otf].join(this.delimiter),
})
});
this.addActions(actions, parent)
}
_get_spells(parent) {
// debugger;
const macroType = "spells";
let actions = [];
let spells = Object.entries(this.actor.system.spells)
spells.forEach((a) => {
const key = a[1].name;
const value = a[1].level
// img
let actionId = key;
let otf = 'Sp:"' + key + '"'; // This is going to end badly if the delimiter is set to ":"
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.Spells'),
encodedValue: [macroType, otf].join(this.delimiter),
})
});
this.addActions(actions, parent)
}
/**
* Build multiple token actions
* @private
* @returns {object}
*/
#buildMultipleTokenActions () {
}
/**
* Build inventory
* @private
*/
async #buildInventory () {
if (this.items.size === 0) return
const actionTypeId = 'item'
const inventoryMap = new Map()
for (const [itemId, itemData] of this.items) {
const type = itemData.type
const equipped = itemData.equipped
if (equipped || this.displayUnequipped) {
const typeMap = inventoryMap.get(type) ?? new Map()
typeMap.set(itemId, itemData)
inventoryMap.set(type, typeMap)
}
}
for (const [type, typeMap] of inventoryMap) {
const groupId = ITEM_TYPE[type]?.groupId
if (!groupId) continue
const groupData = { id: groupId, type: 'system' }
// Get actions
const actions = [...typeMap].map(([itemId, itemData]) => {
const id = itemId
const name = itemData.name
const actionTypeName = coreModule.api.Utils.i18n(ACTION_TYPE[actionTypeId])
const listName = `${actionTypeName ? `${actionTypeName}: ` : ''}${name}`
const encodedValue = [actionTypeId, id].join(this.delimiter)
return {
id,
name,
listName,
encodedValue
}
})
// TAH Core method to add actions to the action list
this.addActions(actions, groupData)
}
}
}
})