From 91b7b53436757db1c2043cce75166b8b9db1566a Mon Sep 17 00:00:00 2001 From: Wrycu Date: Thu, 11 May 2023 16:28:56 -0700 Subject: [PATCH] feat(active_effects): very WIP active effect implementation * extends WIP active effect implementation and direct item embedding for armor #1215 --- modules/helpers/item-helpers.js | 23 ++++ modules/items/item-editor.js | 2 + modules/items/item-ffg.js | 103 ++++++++++++------ modules/items/item-sheet-ffg.js | 16 ++- .../items/embedded/partial/ffg-modifier.html | 5 + templates/items/ffg-armour-sheet.html | 4 +- 6 files changed, 113 insertions(+), 40 deletions(-) diff --git a/modules/helpers/item-helpers.js b/modules/helpers/item-helpers.js index c4a3c873..c760e014 100644 --- a/modules/helpers/item-helpers.js +++ b/modules/helpers/item-helpers.js @@ -165,6 +165,29 @@ export default class ItemHelpers { } } + /** + * Given a parent and child item, move all modifiers to the parent item + * @param parent_item - item, e.g. armor or weapon + * @param mod_item - item_modifier + * @returns {Promise} + */ + static async createEmbeddedModifier(parent_item, mod_item) { + if (mod_item.type !== 'itemmodifier') { + console.log("you cannot do this for non-modifiers") + return; + } + const embedded_modifiers = parent_item.system.attributes; + for (let modifier of Object.keys(mod_item.system.attributes)) { + // using the same key means multiple drag-and-drops will override the previous value + embedded_modifiers[`attr${Date.now()}`] = mod_item.system.attributes[modifier]; + } + parent_item.update({ + system: { + attributes: embedded_modifiers, + }, + }); + } + static async updateParent(embedded_item, form_data, parent_id) { console.log("updating parent") console.log(embedded_item) diff --git a/modules/items/item-editor.js b/modules/items/item-editor.js index 0a02f402..cfdfa8df 100644 --- a/modules/items/item-editor.js +++ b/modules/items/item-editor.js @@ -45,6 +45,8 @@ export class UpdateEmbeddedAttachment extends FormApplication { key = 'itemmodifier_rollmodifiers'; } else if (new_value === 'Weapon Stat') { key = 'weapon_stats'; + } else if (new_value === 'Armor Stat') { + key = 'armor_stats'; } else { console.log(`UNKNOWN MOD TYPE: ${new_value}`) return; diff --git a/modules/items/item-ffg.js b/modules/items/item-ffg.js index c18b43c9..5e13ff6c 100644 --- a/modules/items/item-ffg.js +++ b/modules/items/item-ffg.js @@ -108,7 +108,7 @@ export class ItemFFG extends ItemBaseFFG { const rangeSetting = (this.type === "shipweapon") ? CONFIG.FFG.vehicle_ranges : CONFIG.FFG.ranges; - if (data?.itemmodifier) { + if (data?.itemmodifier) { // TODO: convert to updated function data.itemmodifier.forEach((modifier) => { if (modifier?.system) { modifier.system.rank_current = modifier.system.rank; @@ -238,47 +238,82 @@ export class ItemFFG extends ItemBaseFFG { data.adjusteditemmodifier = []; if (data?.itemmodifier) { - data.itemmodifier.forEach((modifier) => { - if (modifier?.system) { - modifier.system.rank_current = modifier.system.rank; - } - data.adjusteditemmodifier.push({ ...modifier }); - data.soak.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "soak", "Armor Stat"); - data.defence.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "defence", "Armor Stat"); - data.encumbrance.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "encumbrance", "Armor Stat"); - data.price.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "price", "Armor Stat"); - data.rarity.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "rarity", "Armor Stat"); - data.hardpoints.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "hardpoints", "Armor Stat"); - }); + } if (data?.itemattachment) { - data.itemattachment.forEach((attachment) => { - const activeModifiers = attachment.system.itemmodifier.filter((i) => i.system?.active); - data.soak.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "soak", "Armor Stat"); - data.defence.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "defence", "Armor Stat"); - data.encumbrance.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "encumbrance", "Armor Stat"); - data.price.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "price", "Armor Stat"); - data.rarity.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "rarity", "Armor Stat"); - data.hardpoints.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "hardpoints", "Armor Stat"); - - if (attachment?.system?.itemmodifier) { - const activeMods = attachment.system.itemmodifier.filter((i) => i?.system?.active); - - activeMods.forEach((am) => { - const foundItem = data.adjusteditemmodifier.find((i) => i.name === am.name); - - if (foundItem) { - foundItem.system.rank_current = parseInt(foundItem.system.rank_current, 10) + 1; - } else { - am.system.rank_current = 1; - data.adjusteditemmodifier.push({ ...am, adjusted: true }); - } + data.itemattachment.forEach(function (attachment) { + console.log(attachment) + let active_modifiers = []; + if (Object.keys(attachment.system).includes('installed_mods')) { + // TODO: this should not have a condition + attachment.system.installed_mods.forEach(function (mod) { + mod.modifiers.forEach(function (modifier) { + if (modifier[Object.keys(modifier)[0]].active) { + active_modifiers.push(modifier[Object.keys(modifier)[0]]); + } + }); }); } + + data.soak.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "soak", "Armor Stat"); + data.defence.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "defence", "Armor Stat"); + data.encumbrance.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "encumbrance", "Armor Stat"); + data.price.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "price", "Armor Stat"); + data.rarity.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "rarity", "Armor Stat"); + console.log("before") + console.log(data.hardpoints.adjusted) + data.hardpoints.adjusted += ModifierHelpers.calculateValueFromModifiers(active_modifiers, "hardpoints", "Armor Stat"); + console.log(data.hardpoints.adjusted) + }); } + if (true === false) { + // TODO: remove this code block + if (data?.itemmodifier) { + data.itemmodifier.forEach((modifier) => { + if (modifier?.system) { + modifier.system.rank_current = modifier.system.rank; + } + data.adjusteditemmodifier.push({...modifier}); + data.soak.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "soak", "Armor Stat"); + data.defence.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "defence", "Armor Stat"); + data.encumbrance.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "encumbrance", "Armor Stat"); + data.price.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "price", "Armor Stat"); + data.rarity.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "rarity", "Armor Stat"); + data.hardpoints.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(modifier, [], "hardpoints", "Armor Stat"); + }); + } + + if (data?.itemattachment) { + data.itemattachment.forEach((attachment) => { + const activeModifiers = attachment.system.itemmodifier.filter((i) => i.system?.active); + data.soak.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "soak", "Armor Stat"); + data.defence.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "defence", "Armor Stat"); + data.encumbrance.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "encumbrance", "Armor Stat"); + data.price.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "price", "Armor Stat"); + data.rarity.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "rarity", "Armor Stat"); + data.hardpoints.adjusted += ModifierHelpers.getCalculatedValueFromCurrentAndArray(attachment, activeModifiers, "hardpoints", "Armor Stat"); + + if (attachment?.system?.itemmodifier) { + const activeMods = attachment.system.itemmodifier.filter((i) => i?.system?.active); + + activeMods.forEach((am) => { + const foundItem = data.adjusteditemmodifier.find((i) => i.name === am.name); + + if (foundItem) { + foundItem.system.rank_current = parseInt(foundItem.system.rank_current, 10) + 1; + } else { + am.system.rank_current = 1; + data.adjusteditemmodifier.push({...am, adjusted: true}); + } + }); + } + }); + } + } + if (this.isEmbedded && this.actor && this.actor.system) { let soakAdd = 0, defenceAdd = 0, encumbranceAdd = 0; for (let attr in data.attributes) { diff --git a/modules/items/item-sheet-ffg.js b/modules/items/item-sheet-ffg.js index 127c7803..23bc0f6f 100644 --- a/modules/items/item-sheet-ffg.js +++ b/modules/items/item-sheet-ffg.js @@ -1194,7 +1194,6 @@ async _onModControl(event) { } async _onDropItem(event) { - console.log("dropped") let data; const obj = this.object; const li = event.currentTarget; @@ -1215,7 +1214,8 @@ async _onModControl(event) { console.log(dropee_object) // todo: this is probably much too small of a scope - if (dropped_object.type === 'itemmodifier' && dropee_object.type === 'weapon') { + if (dropped_object.type === 'itemmodifier' && ['weapon'].includes(dropee_object.type)) { + console.log("yes") // todo: validate that the type is appropriate let link_id = randomID(); // used to tie AEs to mod // update attachment data @@ -1306,12 +1306,20 @@ async _onModControl(event) { return; } - await ItemHelpers.createEmbeddedAttachment(dropee_object, dropped_object, randomID()) + if (dropped_object.type === 'itemmodifier' && dropped_object.system.type === 'all') { + console.log("dropped mod directly") + await ItemHelpers.createEmbeddedModifier(dropee_object, dropped_object); + } else { + await ItemHelpers.createEmbeddedAttachment(dropee_object, dropped_object, randomID()); + } + + + //await ItemHelpers.syncActiveEffects(dropee_object); //await ItemHelpers.embedAttachment(dropee_object, dropped_object); // TODO: stop normal duplicating the item for deep embed stuff // TODO: handle all of the rules around adding items currently handled below - if (dropped_object.type === 'itemattachment') { + if (dropped_object.type === 'itemattachment' || dropped_object.type === 'itemmodifier') { return } diff --git a/templates/items/embedded/partial/ffg-modifier.html b/templates/items/embedded/partial/ffg-modifier.html index cd860acd..ac1934b4 100644 --- a/templates/items/embedded/partial/ffg-modifier.html +++ b/templates/items/embedded/partial/ffg-modifier.html @@ -33,6 +33,11 @@ {{/each}} {{/if}} + {{#if (eq this.mod.modtype "Armor Stat") }} + {{#each this.config.armor_stats as |t|}} + + {{/each}} + {{/if}} {{/select}} diff --git a/templates/items/ffg-armour-sheet.html b/templates/items/ffg-armour-sheet.html index 905aded2..307bab68 100644 --- a/templates/items/ffg-armour-sheet.html +++ b/templates/items/ffg-armour-sheet.html @@ -53,8 +53,8 @@

- {{#if (ne data.hardpoints.current data.hardpoints.value)}} -
{{data.hardpoints.current}}
+ {{#if (ne data.hardpoints.adjusted data.hardpoints.value)}} +
{{data.hardpoints.adjusted}}
{{/if}}