Got creature recomputation to do things in sheet

This commit is contained in:
Stefan Zermatten
2020-02-18 13:27:31 +02:00
parent 81cec77c9e
commit 86926e593c
7 changed files with 119 additions and 48 deletions

View File

@@ -3,7 +3,9 @@ import ChildSchema, { RefSchema } from '/imports/api/parenting/ChildSchema.js';
import { recomputeCreature } from '/imports/api/creature/creatureComputation.js'; import { recomputeCreature } from '/imports/api/creature/creatureComputation.js';
import LibraryNodes from '/imports/api/library/LibraryNodes.js'; import LibraryNodes from '/imports/api/library/LibraryNodes.js';
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.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 { import {
setLineageOfDocs, setLineageOfDocs,
getAncestry, getAncestry,
@@ -32,6 +34,7 @@ for (let key in propertySchemasIndex){
schema.extend(propertySchemasIndex[key]); schema.extend(propertySchemasIndex[key]);
schema.extend(CreaturePropertySchema); schema.extend(CreaturePropertySchema);
schema.extend(ChildSchema); schema.extend(ChildSchema);
schema.extend(SoftRemovableSchema);
CreatureProperties.attachSchema(schema, { CreatureProperties.attachSchema(schema, {
selector: {type: key} selector: {type: key}
}); });
@@ -50,9 +53,9 @@ function assertPropertyEditPermission(property, userId){
} }
function recomputeCreatures(property){ function recomputeCreatures(property){
for (let ref in property.ancestors){ for (let ref of property.ancestors){
if (ref.collection === 'creatures') { 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}) { run({_id, path, value}) {
let property = LibraryNodes.findOne(_id); let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId); assertPropertyEditPermission(property, this.userId);
LibraryNodes.update(_id, { CreatureProperties.update(_id, {
$set: {[path.join('.')]: value}, $set: {[path.join('.')]: value},
}, { }, {
selector: {type: property.type}, selector: {type: property.type},
@@ -169,14 +172,14 @@ const updateProperty = new ValidatedMethod({
const damageProperty = new ValidatedMethod({ const damageProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.adjust', name: 'CreatureProperties.methods.adjust',
schema: new SimpleSchema({ validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id, _id: SimpleSchema.RegEx.Id,
operation: { operation: {
type: String, type: String,
allowedValues: ['set', 'increment'] allowedValues: ['set', 'increment']
}, },
value: Number, value: Number,
}), }).validator(),
run({_id, operation, value}) { run({_id, operation, value}) {
let currentProperty = CreatureProperties.findOne(_id); let currentProperty = CreatureProperties.findOne(_id);
// Check permissions // 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 default CreatureProperties;
export { export {
@@ -254,4 +269,5 @@ export {
damageProperty, damageProperty,
pushToProperty, pushToProperty,
pullFromProperty, pullFromProperty,
softRemoveProperty,
}; };

View File

@@ -19,9 +19,9 @@ export const recomputeCreature = new ValidatedMethod({
}).validator(), }).validator(),
run({charId}) { run({charId}) {
console.log(`recomputing ${charId}`)
// Permission // Permission
assertEditPermission(charId, this.userId); assertEditPermission(charId, this.userId);
// Work, call this direcly if you are already in a method that has checked // Work, call this direcly if you are already in a method that has checked
// for permission to edit a given character // for permission to edit a given character
recomputeCreatureById(charId); recomputeCreatureById(charId);
@@ -85,7 +85,7 @@ export function recomputeCreatureById(charId){
*/ */
function writeCreature(char) { function writeCreature(char) {
writeAttributes(char); writeAttributes(char);
writeCreatureProperties(char); writeSkills(char);
writeDamageMultipliers(char); writeDamageMultipliers(char);
writeEffects(char); writeEffects(char);
writeCreatureDoc(char); writeCreatureDoc(char);
@@ -106,11 +106,11 @@ function writeCreatureDoc(char) {
* Write all the attributes from the in-memory char object to the Attirbute docs * Write all the attributes from the in-memory char object to the Attirbute docs
*/ */
function writeAttributes(char) { function writeAttributes(char) {
let bulkWriteOps = _.map(char.atts, (att, variableName) => { let bulkWriteOps = _.map(char.atts, (att, variableName) => {
let op = { let op = {
updateMany: { updateMany: {
filter: {'ancestors.id': char.id, variableName}, filter: {'ancestors.id': char.id, variableName},
update: {$set: { update: {'$set': {
value: att.result, value: att.result,
}}, }},
} }
@@ -128,7 +128,10 @@ function writeAttributes(char) {
}); });
} else { } else {
_.each(bulkWriteOps, op => { _.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 { } else {
_.each(bulkWriteOps, op => { _.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 * @param {type} char description
* @returns {type} description * @returns {type} description
*/ */
function writeCreatureProperties(char) { function writeSkills(char) {
let bulkWriteOps = _.map(char.skills, (skill, variableName) => { let bulkWriteOps = _.map(char.skills, (skill, variableName) => {
let op = { let op = {
updateMany: { updateMany: {
@@ -184,7 +187,10 @@ function writeCreatureProperties(char) {
}); });
} else { } else {
_.each(bulkWriteOps, op => { _.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 { } else {
_.each(bulkWriteOps, op => { _.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 //TODO
// Effects // Effects
else if (prop.type === 'effect'){ else if (prop.type === 'effect'){
let storedEffect = { for (let stat of prop.stats){
_id: effect._id, let storedEffect = {
computed: false, _id: prop._id,
result: 0, computed: false,
operation: prop.operation, result: 0,
calculation: prop.calculation, operation: prop.operation,
}; calculation: prop.calculation,
if (char.atts[effect.stat]) { };
char.atts[effect.stat].effects.push(storedEffect); if (char.atts[stat]) {
} else if (char.skills[effect.stat]) { char.atts[stat].effects.push(storedEffect);
char.skills[effect.stat].effects.push(storedEffect); } else if (char.skills[stat]) {
} else if (char.dms[effect.stat]) { char.skills[stat].effects.push(storedEffect);
char.dms[effect.stat].effects.push(storedEffect); } else if (char.dms[stat]) {
} else { char.dms[stat].effects.push(storedEffect);
char.otherEffects.push(storedEffect); } else {
char.otherEffects.push(storedEffect);
}
} }
} }
// Proficiencies // Proficiencies

View File

@@ -29,16 +29,6 @@ let EffectSchema = new SimpleSchema({
type: String, type: String,
optional: true, optional: true,
}, },
statType: {
type: String,
allowedValues: [
'attribute',
'skill',
'roll',
'attack',
'damage',
],
},
//which stats the effect is applied to //which stats the effect is applied to
stats: { stats: {
type: Array, type: Array,

View File

@@ -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;

View File

@@ -58,7 +58,13 @@
</template> </template>
<script> <script>
import CreatureProperties from '/imports/api/creature/CreatureProperties.js'; import CreatureProperties, {
updateProperty,
damageProperty,
pushToProperty,
pullFromProperty,
softRemoveProperty,
} from '/imports/api/creature/CreatureProperties.js';
import DialogBase from '/imports/ui/dialogStack/DialogBase.vue'; import DialogBase from '/imports/ui/dialogStack/DialogBase.vue';
import { getPropertyName } from '/imports/constants/PROPERTIES.js'; import { getPropertyName } from '/imports/constants/PROPERTIES.js';
import PropertyIcon from '/imports/ui/properties/PropertyIcon.vue'; import PropertyIcon from '/imports/ui/properties/PropertyIcon.vue';
@@ -97,13 +103,13 @@ export default {
methods: { methods: {
getPropertyName, getPropertyName,
change({path, value, ack}){ change({path, value, ack}){
updateLibraryNode.call({_id: this._id, path, value}, (error, result) =>{ updateProperty.call({_id: this._id, path, value}, (error, result) =>{
console.log({error, result}); console.log({error, result});
ack && ack(error); ack && ack(error);
}); });
}, },
push({path, value, ack}){ push({path, value, ack}){
pushToLibraryNode.call({_id: this._id, path, value}, (error, result) =>{ pushToProperty.call({_id: this._id, path, value}, (error, result) =>{
console.log({error, result}); console.log({error, result});
ack && ack(error); ack && ack(error);
}); });
@@ -111,13 +117,13 @@ export default {
pull({path, ack}){ pull({path, ack}){
let itemId = get(this.model, path)._id; let itemId = get(this.model, path)._id;
path.pop(); path.pop();
pullFromLibraryNode.call({_id: this._id, path, itemId}, (error, result) =>{ pullFromProperty.call({_id: this._id, path, itemId}, (error, result) =>{
console.log({error, result}); console.log({error, result});
ack && ack(error); ack && ack(error);
}); });
}, },
remove(){ remove(){
softRemoveLibraryNode.call({_id: this._id}); softRemoveProperty.call({_id: this._id});
this.$store.dispatch('popDialogStack'); this.$store.dispatch('popDialogStack');
}, },
} }

View File

@@ -35,9 +35,9 @@
<text-field <text-field
label="Stat" label="Stat"
class="mr-2" class="mr-2"
:value="model.stat" :value="model.stats[0]"
:items="stats" :items="stats"
@change="(value, ack) => $emit('change', {path: ['stat'], value, ack})" @change="(value, ack) => $emit('change', {path: ['stats'], value: [value], ack})"
/> />
</div> </div>
</template> </template>

View File

@@ -35,6 +35,7 @@ const schemaFormMixin = {
if (this.valid) this.valid = false; if (this.valid) this.valid = false;
errors[error.name] = this.schema.messageForError(error); errors[error.name] = this.schema.messageForError(error);
}); });
console.log(errors);
return errors; return errors;
}, },
}, },