From 86926e593c6f6feb8b0677f2f6d0aed6db320211 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Tue, 18 Feb 2020 13:27:31 +0200 Subject: [PATCH] Got creature recomputation to do things in sheet --- .../api/creature/CreatureProperties.js | 30 +++++++--- .../api/creature/creatureComputation.js | 59 +++++++++++-------- app/imports/api/properties/Effects.js | 10 ---- .../computedPropertySchemasIndex.js | 47 +++++++++++++++ .../CreaturePropertyDialog.vue | 16 +++-- .../ui/properties/forms/EffectForm.vue | 4 +- .../forms/shared/schemaFormMixin.js | 1 + 7 files changed, 119 insertions(+), 48 deletions(-) create mode 100644 app/imports/api/properties/computedPropertySchemasIndex.js diff --git a/app/imports/api/creature/CreatureProperties.js b/app/imports/api/creature/CreatureProperties.js index a6eeb196..7a7e6201 100644 --- a/app/imports/api/creature/CreatureProperties.js +++ b/app/imports/api/creature/CreatureProperties.js @@ -3,7 +3,9 @@ import ChildSchema, { RefSchema } from '/imports/api/parenting/ChildSchema.js'; import { recomputeCreature } from '/imports/api/creature/creatureComputation.js'; import LibraryNodes from '/imports/api/library/LibraryNodes.js'; import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js'; -import propertySchemasIndex from '/imports/api/properties/propertySchemasIndex.js'; +import { softRemove } from '/imports/api/parenting/softRemove.js'; +import SoftRemovableSchema from '/imports/api/parenting/SoftRemovableSchema.js'; +import propertySchemasIndex from '/imports/api/properties/computedPropertySchemasIndex.js'; import { setLineageOfDocs, getAncestry, @@ -32,6 +34,7 @@ for (let key in propertySchemasIndex){ schema.extend(propertySchemasIndex[key]); schema.extend(CreaturePropertySchema); schema.extend(ChildSchema); + schema.extend(SoftRemovableSchema); CreatureProperties.attachSchema(schema, { selector: {type: key} }); @@ -50,9 +53,9 @@ function assertPropertyEditPermission(property, userId){ } function recomputeCreatures(property){ - for (let ref in property.ancestors){ + for (let ref of property.ancestors){ if (ref.collection === 'creatures') { - recomputeCreature.call(ref.id); + recomputeCreature.call({charId: ref.id}); } } } @@ -156,9 +159,9 @@ const updateProperty = new ValidatedMethod({ } }, run({_id, path, value}) { - let property = LibraryNodes.findOne(_id); + let property = CreatureProperties.findOne(_id); assertPropertyEditPermission(property, this.userId); - LibraryNodes.update(_id, { + CreatureProperties.update(_id, { $set: {[path.join('.')]: value}, }, { selector: {type: property.type}, @@ -169,14 +172,14 @@ const updateProperty = new ValidatedMethod({ const damageProperty = new ValidatedMethod({ name: 'CreatureProperties.methods.adjust', - schema: new SimpleSchema({ + validate: new SimpleSchema({ _id: SimpleSchema.RegEx.Id, operation: { type: String, allowedValues: ['set', 'increment'] }, value: Number, - }), + }).validator(), run({_id, operation, value}) { let currentProperty = CreatureProperties.findOne(_id); // Check permissions @@ -244,6 +247,18 @@ const pullFromProperty = new ValidatedMethod({ } }); +const softRemoveProperty = new ValidatedMethod({ + name: 'CreatureProperties.methods.softRemove', + validate: new SimpleSchema({ + _id: SimpleSchema.RegEx.Id + }).validator(), + run({_id}){ + let property = CreatureProperties.findOne(_id); + assertPropertyEditPermission(property, this.userId); + softRemove({_id, collection: CreatureProperties}); + } +}); + export default CreatureProperties; export { @@ -254,4 +269,5 @@ export { damageProperty, pushToProperty, pullFromProperty, + softRemoveProperty, }; diff --git a/app/imports/api/creature/creatureComputation.js b/app/imports/api/creature/creatureComputation.js index 2585c9f8..a9a350e8 100644 --- a/app/imports/api/creature/creatureComputation.js +++ b/app/imports/api/creature/creatureComputation.js @@ -19,9 +19,9 @@ export const recomputeCreature = new ValidatedMethod({ }).validator(), run({charId}) { + console.log(`recomputing ${charId}`) // Permission assertEditPermission(charId, this.userId); - // Work, call this direcly if you are already in a method that has checked // for permission to edit a given character recomputeCreatureById(charId); @@ -85,7 +85,7 @@ export function recomputeCreatureById(charId){ */ function writeCreature(char) { writeAttributes(char); - writeCreatureProperties(char); + writeSkills(char); writeDamageMultipliers(char); writeEffects(char); writeCreatureDoc(char); @@ -106,11 +106,11 @@ function writeCreatureDoc(char) { * Write all the attributes from the in-memory char object to the Attirbute docs */ function writeAttributes(char) { - let bulkWriteOps = _.map(char.atts, (att, variableName) => { + let bulkWriteOps = _.map(char.atts, (att, variableName) => { let op = { updateMany: { filter: {'ancestors.id': char.id, variableName}, - update: {$set: { + update: {'$set': { value: att.result, }}, } @@ -128,7 +128,10 @@ function writeAttributes(char) { }); } else { _.each(bulkWriteOps, op => { - CreatureProperties.update(op.updateMany.filter, op.updateMany.update, {multi: true}); + CreatureProperties.update(op.updateMany.filter, op.updateMany.update, { + multi: true, + selector: {type: 'attribute'} + }); }); } } @@ -148,7 +151,7 @@ function writeEffects(char){ }); } else { _.each(bulkWriteOps, op => { - CreatureProperties.update(op.updateOne.filter, op.updateOne.update); + CreatureProperties.update(op.updateOne.filter, op.updateOne.update, {selector: {type: 'effect'}}); }); } } @@ -160,7 +163,7 @@ function writeEffects(char){ * @param {type} char description * @returns {type} description */ -function writeCreatureProperties(char) { +function writeSkills(char) { let bulkWriteOps = _.map(char.skills, (skill, variableName) => { let op = { updateMany: { @@ -184,7 +187,10 @@ function writeCreatureProperties(char) { }); } else { _.each(bulkWriteOps, op => { - CreatureProperties.update(op.updateMany.filter, op.updateMany.update, {multi: true}); + CreatureProperties.update(op.updateMany.filter, op.updateMany.update, { + multi: true, + selector: {type: 'skill'}, + }); }); } } @@ -213,7 +219,10 @@ function writeDamageMultipliers(char) { }); } else { _.each(bulkWriteOps, op => { - CreatureProperties.update(op.updateMany.filter, op.updateMany.update, {multi: true}); + CreatureProperties.update(op.updateMany.filter, op.updateMany.update, { + multi: true, + selector: {type: 'damageMultiplier'}, + }); }); } } @@ -315,21 +324,23 @@ function buildCreature(charId){ //TODO // Effects else if (prop.type === 'effect'){ - let storedEffect = { - _id: effect._id, - computed: false, - result: 0, - operation: prop.operation, - calculation: prop.calculation, - }; - if (char.atts[effect.stat]) { - char.atts[effect.stat].effects.push(storedEffect); - } else if (char.skills[effect.stat]) { - char.skills[effect.stat].effects.push(storedEffect); - } else if (char.dms[effect.stat]) { - char.dms[effect.stat].effects.push(storedEffect); - } else { - char.otherEffects.push(storedEffect); + for (let stat of prop.stats){ + let storedEffect = { + _id: prop._id, + computed: false, + result: 0, + operation: prop.operation, + calculation: prop.calculation, + }; + if (char.atts[stat]) { + char.atts[stat].effects.push(storedEffect); + } else if (char.skills[stat]) { + char.skills[stat].effects.push(storedEffect); + } else if (char.dms[stat]) { + char.dms[stat].effects.push(storedEffect); + } else { + char.otherEffects.push(storedEffect); + } } } // Proficiencies diff --git a/app/imports/api/properties/Effects.js b/app/imports/api/properties/Effects.js index fee9cd3c..39ee063c 100644 --- a/app/imports/api/properties/Effects.js +++ b/app/imports/api/properties/Effects.js @@ -29,16 +29,6 @@ let EffectSchema = new SimpleSchema({ type: String, optional: true, }, - statType: { - type: String, - allowedValues: [ - 'attribute', - 'skill', - 'roll', - 'attack', - 'damage', - ], - }, //which stats the effect is applied to stats: { type: Array, diff --git a/app/imports/api/properties/computedPropertySchemasIndex.js b/app/imports/api/properties/computedPropertySchemasIndex.js new file mode 100644 index 00000000..5c596bcb --- /dev/null +++ b/app/imports/api/properties/computedPropertySchemasIndex.js @@ -0,0 +1,47 @@ +import SimpleSchema from 'simpl-schema'; +import { ActionSchema } from '/imports/api/properties/Actions.js'; +import { AttackSchema } from '/imports/api/properties/Attacks.js'; +import { ComputedAttributeSchema } from '/imports/api/properties/Attributes.js'; +import { AppliedBuffSchema } from '/imports/api/properties/Buffs.js'; +import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js'; +import { DamageMultiplierSchema } from '/imports/api/properties/DamageMultipliers.js'; +import { ComputedEffectSchema } from '/imports/api/properties/Effects.js'; +import { ExperienceSchema } from '/imports/api/properties/Experiences.js'; +import { FeatureSchema } from '/imports/api/properties/Features.js'; +import { FolderSchema } from '/imports/api/properties/Folders.js'; +import { NoteSchema } from '/imports/api/properties/Notes.js'; +import { ProficiencySchema } from '/imports/api/properties/Proficiencies.js'; +import { RollSchema } from '/imports/api/properties/Rolls.js'; +import { SavingThrowSchema } from '/imports/api/properties/SavingThrows.js'; +import { ComputedSkillSchema } from '/imports/api/properties/Skills.js'; +import { SlotSchema } from '/imports/api/properties/Slots.js'; +import { SpellListSchema } from '/imports/api/properties/SpellLists.js'; +import { SpellSchema } from '/imports/api/properties/Spells.js'; +import { ContainerSchema } from '/imports/api/properties/Containers.js'; +import { ItemSchema } from '/imports/api/properties/Items.js'; + +const propertySchemasIndex = { + action: ActionSchema, + attack: AttackSchema, + attribute: ComputedAttributeSchema, + buff: AppliedBuffSchema, + classLevel: ClassLevelSchema, + damageMultiplier: DamageMultiplierSchema, + effect: ComputedEffectSchema, + experience: ExperienceSchema, + feature: FeatureSchema, + folder: FolderSchema, + note: NoteSchema, + proficiency: ProficiencySchema, + roll: RollSchema, + savingThrow: SavingThrowSchema, + skill: ComputedSkillSchema, + slot: SlotSchema, + spellList: SpellListSchema, + spell: SpellSchema, + container: ContainerSchema, + item: ItemSchema, + any: new SimpleSchema({}), +}; + +export default propertySchemasIndex; diff --git a/app/imports/ui/creature/creatureProperties/CreaturePropertyDialog.vue b/app/imports/ui/creature/creatureProperties/CreaturePropertyDialog.vue index 14c490a0..c2f5e085 100644 --- a/app/imports/ui/creature/creatureProperties/CreaturePropertyDialog.vue +++ b/app/imports/ui/creature/creatureProperties/CreaturePropertyDialog.vue @@ -58,7 +58,13 @@