Got creature recomputation to do things in sheet
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user