Handle skills/spells in containers

This commit is contained in:
Neill Cox 2024-05-18 19:31:09 +10:00
parent fd29caa7f6
commit e66a425e22
4 changed files with 127 additions and 62 deletions

View file

@ -32,7 +32,6 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
}
if (this.actorType === 'character') {
//debugger;
this.#buildCharacterActions()
} else if (!this.actor) {
this.#buildMultipleTokenActions()
@ -44,7 +43,6 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
* @private
*/
#buildCharacterActions () {
// debugger;
this._get_attributes({id: "attributes", type:"system"})
this._get_skills({id: "skills", type:"system"})
this._get_spells({id: "spells", type:"system"})
@ -81,7 +79,6 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
}
_get_melee_attacks(parent) {
// debugger;
const macroType = "melee";
let actions = [];
@ -112,7 +109,6 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
}
_get_ranged_attacks(parent) {
// debugger;
const macroType = "ranged";
let actions = [];
@ -143,57 +139,146 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
}
_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) + " - test",
// description: coreModule.api.Utils.i18n('GURPS.Skills'),
// encodedValue: [macroType, otf].join(this.delimiter),
// })
// });
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 ":"
// let map = this._get_contents(skills)
let map = this._get_contents(skills, macroType)
let names = Array.from(map.keys()).sort()
names.forEach((a)=> {
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.Skills'),
encodedValue: [macroType, otf].join(this.delimiter),
id: a,
name: map.get(a).name,
description: map.get(a).description,
encodedValue: map.get(a).encodedValue
})
})
});
this.addActions(actions, parent)
}
_get_contents(spells, macroType, result= new Map()) {
console.debug(spells);
console.debug(macroType);
console.debug(result)
if (spells instanceof Array) {
for (const spell in spells) {
let _contains = spells[spell][1].contains;
if (Reflect.has(_contains,"00000")) {
for( const i in _contains) {
result = this._get_contents(_contains[i], macroType, result)
}
}
else {
let name = spells[spell][1].name;
let level = spells[spell][1].level;
let otf_type = null;
if (macroType === "skills") {
otf_type = 'Sk'
}
else {
otf_type = 'Sp'
}
let otf = otf_type + ':"' + name + '"';
if (level) {
result.set(
name,
{
id: name,
name: coreModule.api.Utils.i18n(name),
description: coreModule.api.Utils.i18n('GURPS.Spells'),
encodedValue: [macroType, otf].join(this.delimiter),
}
)
}
}
}
}
else {
if (spells.uuid) {
let _contains = spells.contains;
if (Reflect.has(_contains,"00000")) {
for( const i in _contains) {
result = this._get_contents(_contains[i], macroType, result)
}
}
else {
let spell = spells;
let name = spell.name;
let level = spell.level;
let otf_type = null;
if (macroType === "skills") {
otf_type = 'Sk'
}
else {
otf_type = 'Sp'
}
let otf = otf_type + ':"' + name + '"';
if (level) {
result.set(
name,
{
id: name,
name: coreModule.api.Utils.i18n(name),
description: coreModule.api.Utils.i18n('GURPS.Spells'),
encodedValue: [macroType, otf].join(this.delimiter),
}
)
}
}
}
}
return result;
}
_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 ":"
let spell_map = this._get_contents(spells, macroType)
let spell_names = Array.from(spell_map.keys()).sort()
spell_names.forEach((a)=> {
actions.push({
id: actionId,
name: coreModule.api.Utils.i18n(key),
description: coreModule.api.Utils.i18n('GURPS.Spells'),
encodedValue: [macroType, otf].join(this.delimiter),
id: a,
name: spell_map.get(a).name,
description: spell_map.get(a).description,
encodedValue: spell_map.get(a).encodedValue
})
})
});
this.addActions(actions, parent)
}
@ -256,3 +341,4 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
}
}
})