Triggers 🤫

This commit is contained in:
Stefan Zermatten
2022-07-20 15:57:38 +02:00
parent 6f7e742eb9
commit 566d6a4fef
16 changed files with 853 additions and 187 deletions

View File

@@ -7,6 +7,7 @@ import note from './applyPropertyByType/applyNote.js';
import roll from './applyPropertyByType/applyRoll.js';
import savingThrow from './applyPropertyByType/applySavingThrow.js';
import toggle from './applyPropertyByType/applyToggle.js';
import applyTriggers from '/imports/api/engine/actions/applyTriggers.js';
const applyPropertyByType = {
action,
@@ -21,7 +22,9 @@ const applyPropertyByType = {
toggle,
};
export default function applyProperty(node, opts, ...rest){
export default function applyProperty(node, opts, ...rest) {
applyTriggers(node, opts, 'before');
opts.scope[`#${node.node.type}`] = node.node;
return applyPropertyByType[node.node.type]?.(node, opts, ...rest);
applyPropertyByType[node.node.type]?.(node, opts, ...rest);
applyTriggers(node, opts, 'after');
}

View File

@@ -0,0 +1,96 @@
import recalculateCalculation from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateCalculation.js';
import recalculateInlineCalculations from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateInlineCalculations.js';
import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import applyProperty from '/imports/api/engine/actions/applyProperty.js';
import { difference, intersection } from 'lodash';
export default function applyTriggers(node, { creature, targets, scope, log }, timing) {
const prop = node.node;
const type = prop.type;
if (creature.triggers?.[type]?.[timing]) {
creature.triggers[type][timing].forEach(trigger => {
// Tags
if (!triggerMatchTags(trigger, prop)) return;
// Condition
if (trigger.condition?.parseNode) {
recalculateCalculation(trigger.condition, scope, log);
if (!trigger.condition.value) return;
}
// Apply
applyTrigger(trigger, { creature, targets, scope, log });
});
}
}
function triggerMatchTags(trigger, prop) {
let matched = false;
// Check the target tags
if (
!trigger.targetTags?.length ||
difference(trigger.targetTags, prop.tags).length === 0
) {
matched = true;
}
// Check the extra tags
trigger.extraTags?.forEach(extra => {
if (extra.operation === 'OR') {
if (matched) return;
if (
!extra.tags.length ||
difference(extra.tags, prop.tags).length === 0
) {
matched = true;
}
} else if (extra.operation === 'NOT') {
if (
extra.tags.length &&
intersection(extra.tags, prop.tags)
) {
return false;
}
}
});
return matched;
}
export function applyTrigger(trigger, { creature, targets, scope, log }) {
if (trigger.firing) {
/*
log.content.push({
name: trigger.name || 'Trigger',
value: 'Trigger can\'t fire itself',
inline: true,
});
*/
return;
}
trigger.firing = true;
// Fire the trigger
const content = {
name: trigger.name || 'Trigger',
value: trigger.summary,
inline: false,
}
if (trigger.summary?.text){
recalculateInlineCalculations(trigger.summary, scope, log);
content.value = trigger.summary.value;
}
log.content.push(content);
// Get all the trigger's properties and apply them
const properties = getPropertyDecendants(creature._id, trigger._id);
properties.sort((a, b) => a.order - b.order);
const propertyForest = nodeArrayToTree(properties);
propertyForest.forEach(node => {
applyProperty(node, {
creature,
targets,
scope,
log,
});
});
trigger.firing = false;
}

View File

@@ -1,14 +1,16 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import {
getCreature, getVariables, getProperyAncestors, getPropertyDecendants, getPropertiesOfType
} from '/imports/api/engine/loadCreatures.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import applyProperty from './applyProperty.js';
import { groupBy, remove } from 'lodash';
const doAction = new ValidatedMethod({
name: 'creatureProperties.doAction',
@@ -38,52 +40,37 @@ const doAction = new ValidatedMethod({
run({actionId, targetIds = [], scope}) {
let action = CreatureProperties.findOne(actionId);
// Check permissions
let creature = getRootCreatureAncestor(action);
const variables = CreatureVariables.findOne({
_creatureId: creature._id
}, {
fields: {_id: 0, _creatureId: 0},
});
creature.variables = variables;
const creatureId = action.ancestors[0].id;
let creature = getCreature(action.ancestors[0].id);
assertEditPermission(creature, this.userId);
// Add the variables to the creature document
const variables = getVariables(creatureId);
delete variables._id;
delete variables._creatureId;
creature.variables = variables;
// Get all the targets and make sure we can edit them
let targets = [];
targetIds.forEach(targetId => {
let target = Creatures.findOne(targetId);
let target = getCreature(targetId);
assertEditPermission(target, this.userId);
const variables = CreatureVariables.findOne({
_creatureId: targetId
}, {
fields: {_id: 0, _creatureId: 0},
});
// add the variables to the target documents
const variables = getVariables(creatureId);
delete variables._id;
delete variables._creatureId;
target.variables = variables;
targets.push(target);
});
// Fetch all the action's ancestor creatureProperties
const ancestorIds = [];
action.ancestors.forEach(ref => {
if (ref.collection === 'creatureProperties') {
ancestorIds.push(ref.id);
}
});
const ancestors = getProperyAncestors(creatureId, action._id);
ancestors.sort((a, b) => a.order - b.order);
// Get cursor of ancestors
const ancestors = CreatureProperties.find({
_id: {$in: ancestorIds},
}, {
sort: {order: 1},
});
// Get cursor of the properties
const properties = CreatureProperties.find({
$or: [{_id: action._id}, {'ancestors.id': action._id}],
removed: {$ne: true},
}, {
sort: {order: 1},
});
const properties = getPropertyDecendants(creatureId, action._id);
properties.push(action);
properties.sort((a, b) => a.order - b.order);
// Do the action
doActionWork({creature, targets, properties, ancestors, method: this, methodScope: scope});
@@ -109,6 +96,14 @@ export function doActionWork({
throw new Meteor.Error(`The action has ${propertyForest.length} top level properties, expected 1`);
}
// Get the triggers
const triggers = getPropertiesOfType(creature._id, 'trigger');
remove(triggers, trigger => trigger.event !== 'doActionProperty');
creature.triggers = groupBy(triggers, 'actionPropertyType');
for (let type in creature.triggers) {
creature.triggers[type] = groupBy(creature.triggers[type], 'timing')
}
// Create the log
if (!log) log = CreatureLogSchema.clean({
creatureId: creature._id,

View File

@@ -1,10 +1,7 @@
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import CreatureProperties,
{ DenormalisedOnlyCreaturePropertySchema as denormSchema }
import { DenormalisedOnlyCreaturePropertySchema as denormSchema }
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { loadedCreatures } from '../loadCreatures.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
import { getProperties, getCreature, getVariables } from '/imports/api/engine/loadCreatures.js';
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
import linkInventory from './buildComputation/linkInventory.js';
@@ -40,53 +37,6 @@ export default function buildCreatureComputation(creatureId){
return computation;
}
function getProperties(creatureId) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = Array.from(creature.properties.values());
const cloneProps = EJSON.clone(props);
return cloneProps
}
// console.time(`Cache miss on creature properties: ${creatureId}`)
const props = CreatureProperties.find({
'ancestors.id': creatureId,
'removed': {$ne: true},
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).fetch();
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return props;
}
function getCreature(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const creature = loadedCreature.creature;
if (creature) return creature;
}
// console.time(`Cache miss on Creature: ${creatureId}`);
const creature = Creatures.findOne(creatureId, {
denormalizedStats: 1,
variables: 1,
dirty: 1,
});
// console.timeEnd(`Cache miss on Creature: ${creatureId}`);
return creature;
}
function getVariables(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const variables = loadedCreature.variables;
if (variables) return variables;
}
// console.time(`Cache miss on variables: ${creatureId}`);
const variables = CreatureVariables.findOne({_creatureId: creatureId});
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
return variables;
}
export function buildComputationFromProps(properties, creature, variables){
const computation = new CreatureComputation(properties, creature, variables);

View File

@@ -32,6 +32,153 @@ function unloadCreature(creatureId, subscription) {
}
}
export function getSingleProperty(creatureId, propertyId) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const property = creature.properties.get(propertyId);
const cloneProp = EJSON.clone(property);
return cloneProp;
}
// console.time(`Cache miss on creature properties: ${creatureId}`)
const prop = CreatureProperties.findOne({
_id: propertyId,
'ancestors.id': creatureId,
'removed': {$ne: true},
}, {
sort: { order: 1 },
fields: { icon: 0 },
});
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return prop;
}
export function getProperties(creatureId) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = Array.from(creature.properties.values());
const cloneProps = EJSON.clone(props);
return cloneProps
}
// console.time(`Cache miss on creature properties: ${creatureId}`)
const props = CreatureProperties.find({
'ancestors.id': creatureId,
'removed': {$ne: true},
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).fetch();
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return props;
}
export function getPropertiesOfType(creatureId, propType) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = []
for (const prop of creature.properties.values()){
if (prop.type === propType) {
props.push(prop);
}
}
const cloneProps = EJSON.clone(props);
return cloneProps
}
// console.time(`Cache miss on creature properties: ${creatureId}`)
const props = CreatureProperties.find({
'ancestors.id': creatureId,
'removed': { $ne: true },
'type': propType,
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).fetch();
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return props;
}
export function getCreature(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const creature = loadedCreature.creature;
if (creature) return creature;
}
// console.time(`Cache miss on Creature: ${creatureId}`);
const creature = Creatures.findOne(creatureId, {
denormalizedStats: 1,
variables: 1,
dirty: 1,
});
// console.timeEnd(`Cache miss on Creature: ${creatureId}`);
return creature;
}
export function getVariables(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const variables = loadedCreature.variables;
if (variables) return variables;
}
// console.time(`Cache miss on variables: ${creatureId}`);
const variables = CreatureVariables.findOne({_creatureId: creatureId});
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
return variables;
}
export function getProperyAncestors(creatureId, propertyId) {
const prop = getSingleProperty(creatureId, propertyId);
if (!prop) return [];
const ancestorIds = [];
prop.ancestors.forEach(ref => {
if (ref.collection === 'creatureProperties') {
ancestorIds.push(ref.id);
}
});
if (loadedCreatures.has(creatureId)) {
// Get the ancestor properties from the cache
const creature = loadedCreatures.get(creatureId);
const props = [];
ancestorIds.forEach(id => {
const prop = creature.properties.get(id);
if (prop) {
props.push(prop);
}
});
const cloneProps = EJSON.clone(props);
return cloneProps
} else {
// Fetch from database
return CreatureProperties.find({
_id: { $in: ancestorIds },
}, {
sort: { order: 1 },
}).fetch();
}
}
export function getPropertyDecendants(creatureId, propertyId) {
const property = getSingleProperty(creatureId, propertyId);
if (!property) return [];
// This prop will always appear at the same position in the ancestor array
// of its decendants, so only check there
const expectedAncestorPostition = property.ancestors.length;
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = [];
for(const prop of creature.properties.values()){
if (prop.ancestors[expectedAncestorPostition]?.id === propertyId) {
props.push(prop);
}
}
const cloneProps = EJSON.clone(props);
return cloneProps
} else {
return CreatureProperties.find({
'ancestors.id': propertyId,
removed: { $ne: true },
}).fetch();
}
}
class LoadedCreature {
constructor(sub, creatureId) {
// This may be called from a subscription, but we don't want the observers