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 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,
};

View File

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

View File

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

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>
<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 { getPropertyName } from '/imports/constants/PROPERTIES.js';
import PropertyIcon from '/imports/ui/properties/PropertyIcon.vue';
@@ -97,13 +103,13 @@ export default {
methods: {
getPropertyName,
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});
ack && ack(error);
});
},
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});
ack && ack(error);
});
@@ -111,13 +117,13 @@ export default {
pull({path, ack}){
let itemId = get(this.model, path)._id;
path.pop();
pullFromLibraryNode.call({_id: this._id, path, itemId}, (error, result) =>{
pullFromProperty.call({_id: this._id, path, itemId}, (error, result) =>{
console.log({error, result});
ack && ack(error);
});
},
remove(){
softRemoveLibraryNode.call({_id: this._id});
softRemoveProperty.call({_id: this._id});
this.$store.dispatch('popDialogStack');
},
}

View File

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

View File

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