From a412f9d751ad66678d88fccbe0b7237d0347f4d7 Mon Sep 17 00:00:00 2001 From: drewg13 Date: Thu, 22 Apr 2021 04:28:03 -0400 Subject: [PATCH] Changes to the item logic process to eliminate DB write-locking and dropped changes (#108) --- module/blades-helpers.js | 89 ++++++++++++++++++++++------------------ module/blades.js | 24 +++++------ 2 files changed, 62 insertions(+), 51 deletions(-) diff --git a/module/blades-helpers.js b/module/blades-helpers.js index df6c838..12f69a6 100644 --- a/module/blades-helpers.js +++ b/module/blades-helpers.js @@ -2,9 +2,9 @@ export class BladesHelpers { /** * Removes a duplicate item type from charlist. - * - * @param {Object} item_data - * @param {Entity} actor + * + * @param {Object} item_data + * @param {Entity} actor */ static removeDuplicatedItemType(item_data, actor) { @@ -22,10 +22,10 @@ export class BladesHelpers { /** * Add item modification if logic exists. - * @param {Object} item_data - * @param {Entity} entity + * @param {Object} item_data + * @param {Entity} entity */ - static callItemLogic(item_data, entity) { + static async callItemLogic(item_data, entity) { if ('logic' in item_data.data && item_data.data.logic !== '') { let logic = JSON.parse(item_data.data.logic); @@ -36,28 +36,33 @@ export class BladesHelpers { } if (logic) { - + let logic_update = { "_id": entity.data._id }; logic.forEach(expression => { // Different logic behav. dep on operator. switch (expression.operator) { - + // Add when creating. case "addition": - entity.update({ - [expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, "data." + expression.attribute)) + expression.value - }); - break; + mergeObject( + logic_update, + {[expression.attribute]: Number(SaVHelpers.getNestedProperty(entity, prefix + expression.attribute)) + expression.value}, + {insertKeys: true} + ); + break; // Change name property. case "attribute_change": - entity.update({ - [expression.attribute]: expression.value - }); - break; - + mergeObject( + logic_update, + {[expression.attribute]: expression.value}, + {insertKeys: true} + ); + break; + } }); + await Actor.update( logic_update ); } } @@ -69,10 +74,10 @@ export class BladesHelpers { * @todo * - Remove all items and then Add them back to * sustain the logic mods - * @param {Object} item_data - * @param {Entity} entity + * @param {Object} item_data + * @param {Entity} entity */ - static undoItemLogic(item_data, entity) { + static async undoItemLogic(item_data, entity) { if ('logic' in item_data.data && item_data.data.logic !== '') { let logic = JSON.parse(item_data.data.logic) @@ -83,7 +88,7 @@ export class BladesHelpers { } if (logic) { - + let logic_update = { "_id": entity.data._id }; var entity_data = entity.data; logic.forEach(expression => { @@ -92,10 +97,12 @@ export class BladesHelpers { // Subtract when removing. case "addition": - entity.update({ - [expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, "data." + expression.attribute)) - expression.value - }); - break; + mergeObject( + logic_update, + {[expression.attribute]: Number(SaVHelpers.getNestedProperty(entity, prefix + expression.attribute)) - expression.value}, + {insertKeys: true} + ); + break; // Change name back to default. case "attribute_change": @@ -103,12 +110,16 @@ export class BladesHelpers { let default_expression_attribute_path = expression.attribute + '_default'; let default_name = default_expression_attribute_path.split(".").reduce((o, i) => o[i], entity_data); - entity.update({ - [expression.attribute]: default_name - }); - break; + mergeObject( + logic_update, + {[expression.attribute]: default_name}, + {insertKeys: true} + ); + + break; } }); + await Actor.update( logic_update ); } } @@ -116,8 +127,8 @@ export class BladesHelpers { /** * Get a nested dynamic attribute. - * @param {Object} obj - * @param {string} property + * @param {Object} obj + * @param {string} property */ static getNestedProperty(obj, property) { return property.split('.').reduce((r, e) => { @@ -144,16 +155,16 @@ export class BladesHelpers { /** * Get the list of all available ingame items by Type. - * - * @param {string} item_type - * @param {Object} game + * + * @param {string} item_type + * @param {Object} game */ static async getAllItemsByType(item_type, game) { let list_of_items = []; let game_items = []; let compendium_items = []; - + game_items = game.items.filter(e => e.type === item_type).map(e => {return e.data}); let pack = game.packs.find(e => e.metadata.name === item_type); @@ -171,22 +182,22 @@ export class BladesHelpers { /** * Returns the label for attribute. * - * @param {string} attribute_name + * @param {string} attribute_name * @returns {string} */ static getAttributeLabel(attribute_name) { // Calculate Dice to throw. let attribute_labels = {}; const attributes = game.system.model.Actor.character.attributes; - + for (var attibute_name in attributes) { attribute_labels[attibute_name] = attributes[attibute_name].label; for (var skill_name in attributes[attibute_name].skills) { attribute_labels[skill_name] = attributes[attibute_name].skills[skill_name].label; } - + } - + return attribute_labels[attribute_name]; } diff --git a/module/blades.js b/module/blades.js index 1e81438..6f8528f 100644 --- a/module/blades.js +++ b/module/blades.js @@ -60,14 +60,14 @@ Hooks.once("init", async function() { // Multiboxes. Handlebars.registerHelper('multiboxes', function(selected, options) { - + let html = options.fn(this); // Fix for single non-array values. if ( !Array.isArray(selected) ) { selected = [selected]; } - + if (typeof selected !== 'undefined') { selected.forEach(selected_value => { if (selected_value !== false) { @@ -82,7 +82,7 @@ Hooks.once("init", async function() { // Trauma Counter Handlebars.registerHelper('traumacounter', function(selected, options) { - + let html = options.fn(this); var count = 0; @@ -93,7 +93,7 @@ Hooks.once("init", async function() { } if (count > 4) count = 4; - + const rgx = new RegExp(' value=\"' + count + '\"'); return html.replace(rgx, "$& checked=\"checked\""); @@ -250,7 +250,7 @@ Hooks.once("init", async function() { for (let i = 1; i <= parseInt(type); i++) { let checked = (parseInt(current_value) === i) ? 'checked="checked"' : ''; - html += ` + html += ` `; @@ -270,9 +270,9 @@ Hooks.once("ready", function() { // Determine whether a system migration is required const currentVersion = game.settings.get("bitd", "systemMigrationVersion"); const NEEDS_MIGRATION_VERSION = 2.15; - + let needMigration = (currentVersion < NEEDS_MIGRATION_VERSION) || (currentVersion === null); - + // Perform the migration if ( needMigration && game.user.isGM ) { migrations.migrateWorld(); @@ -289,15 +289,15 @@ Hooks.on("preCreateOwnedItem", (parent_entity, child_data, options, userId) => { return true; }); -Hooks.on("createOwnedItem", (parent_entity, child_data, options, userId) => { +Hooks.on("createOwnedItem", async (parent_entity, child_data, options, userId) => { - BladesHelpers.callItemLogic(child_data, parent_entity); + await BladesHelpers.callItemLogic(child_data, parent_entity); return true; }); -Hooks.on("deleteOwnedItem", (parent_entity, child_data, options, userId) => { - - BladesHelpers.undoItemLogic(child_data, parent_entity); +Hooks.on("deleteOwnedItem", async (parent_entity, child_data, options, userId) => { + + await BladesHelpers.undoItemLogic(child_data, parent_entity); return true; }); // getSceneControlButtons