Skip to content

Commit

Permalink
feat(active_effects): very WIP active effect implementation
Browse files Browse the repository at this point in the history
* extends WIP active effect implementation and direct item embedding for armor

#1215
  • Loading branch information
wrycu committed May 11, 2023
1 parent 7d04399 commit 91b7b53
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 40 deletions.
23 changes: 23 additions & 0 deletions modules/helpers/item-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
*/
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)
Expand Down
2 changes: 2 additions & 0 deletions modules/items/item-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
103 changes: 69 additions & 34 deletions modules/items/item-ffg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions modules/items/item-sheet-ffg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,6 @@ async _onModControl(event) {
}

async _onDropItem(event) {
console.log("dropped")
let data;
const obj = this.object;
const li = event.currentTarget;
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions templates/items/embedded/partial/ffg-modifier.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<option value="{{ t.value }}">{{ localize t.label}}</option>
{{/each}}
{{/if}}
{{#if (eq this.mod.modtype "Armor Stat") }}
{{#each this.config.armor_stats as |t|}}
<option value="{{ t.value }}">{{ localize t.label}}</option>
{{/each}}
{{/if}}
{{/select}}
</select>
<input type="text" name="{{ mod_id }}-{{ index }}-modifier_value" value="{{ this.mod.value }}" placeholder="0"/>
Expand Down
4 changes: 2 additions & 2 deletions templates/items/ffg-armour-sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ <h1 class="charname"><input name="name" type="text" value="{{item.name}}" placeh
<div class="characteristic">
<div class="characteristic-value">
<input type="text" name="data.hardpoints.value" value="{{data.hardpoints.value}}" data-dtype="Number" />
{{#if (ne data.hardpoints.current data.hardpoints.value)}}
<div class="adjustedvalues-left {{#if (gt data.hardpoints.current data.hardpoints.value)}}positive{{/if}}">{{data.hardpoints.current}}</div>
{{#if (ne data.hardpoints.adjusted data.hardpoints.value)}}
<div class="adjustedvalues-left {{#if (gt data.hardpoints.current data.hardpoints.value)}}positive{{/if}}">{{data.hardpoints.adjusted}}</div>
{{/if}}
</div>
</div>
Expand Down

0 comments on commit 91b7b53

Please sign in to comment.