From 397ff82c43c9f7c875f552c10940cc7abf70aa3f Mon Sep 17 00:00:00 2001 From: Thaum Rystra Date: Mon, 18 May 2020 19:58:28 +0200 Subject: [PATCH] Organizing the tree now causes a character recomputation where relevant --- app/imports/api/parenting/organizeMethods.js | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/app/imports/api/parenting/organizeMethods.js b/app/imports/api/parenting/organizeMethods.js index fed8540a..9d7fb941 100644 --- a/app/imports/api/parenting/organizeMethods.js +++ b/app/imports/api/parenting/organizeMethods.js @@ -6,6 +6,7 @@ import { RefSchema } from '/imports/api/parenting/ChildSchema.js'; import { assertDocEditPermission } from '/imports/api/sharing/sharingPermissions.js'; import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js'; import getCollectionByName from '/imports/api/parenting/getCollectionByName.js'; +import { recomputeCreatureById } from '/imports/api/creature/computation/recomputeCreature.js'; const organizeDoc = new ValidatedMethod({ name: 'organize.methods.organizeDoc', @@ -39,6 +40,15 @@ const organizeDoc = new ValidatedMethod({ if (newAncestorId !== oldAncestorId){ reorderDocs({collection, ancestorId: newAncestorId}); } + + // Figure out which creatures need to be recalculated after this move + let docCreatures = getCreatureAncestors(doc); + let parentCreatures = getCreatureAncestors(parent); + let creaturesToRecompute = new Set(...docCreatures, ...parentCreatures); + // Recompute the creatures + creaturesToRecompute.forEach(id => { + recomputeCreatureById(id); + }); }, }); @@ -54,7 +64,11 @@ const reorderDoc = new ValidatedMethod({ run({docRef, order}) { let doc = fetchDocByRef(docRef); assertDocEditPermission(doc, this.userId); - safeUpdateDocOrder({docRef, order}) + safeUpdateDocOrder({docRef, order}); + // Recompute the affected creatures + getCreatureAncestors(doc).forEach(id => { + recomputeCreatureById(id); + }); }, }); @@ -66,4 +80,19 @@ function getRootId(doc){ } } +function getCreatureAncestors(doc){ + let ids = []; + if(doc.type === 'pc' || doc.type === 'npc' || doc.type === 'monster'){ + ids.push(doc._id); + } + if (doc.ancestors){ + doc.ancestors.forEach(ancestorRef => { + if (ancestorRef.collection === 'creatures'){ + ids.push(ancestorRef.id); + } + }); + } + return ids; +} + export { organizeDoc, reorderDoc };