Refactored entire action engine

Triggers needed action context to function outside of the action engine
proper, so now it's been abstracted into its own class
This commit is contained in:
Stefan Zermatten
2022-08-13 00:22:32 +02:00
parent 13fc0c0b12
commit ee0e764294
29 changed files with 626 additions and 617 deletions

View File

@@ -3,12 +3,9 @@ import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import { groupBy, remove, union } from 'lodash';
import {
getCreature, getVariables, getPropertiesOfType
} from '/imports/api/engine/loadCreatures.js';
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
import { applyTrigger } from '/imports/api/engine/actions/applyTriggers.js';
import { union } from 'lodash';
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
const restCreature = new ValidatedMethod({
name: 'creature.methods.rest',
@@ -27,59 +24,37 @@ const restCreature = new ValidatedMethod({
numRequests: 5,
timeInterval: 5000,
},
run({creatureId, restType}) {
run({ creatureId, restType }) {
// Get action context
const actionContext = new ActionContext(creatureId, [creatureId], this);
// Check permissions
let creature = getCreature(creatureId);
assertEditPermission(creature, this.userId);
assertEditPermission(actionContext.creature, this.userId);
// Add the variables to the creature document
const variables = getVariables(creatureId);
delete variables._id;
delete variables._creatureId;
creature.variables = variables;
const scope = creature.variables;
// Join, sort, and apply before triggers
const beforeTriggers = union(
actionContext.triggers.anyRest?.before, actionContext.triggers[restType]?.before
).sort((a, b) => a.order - b.order);
applyTriggers(beforeTriggers, null, actionContext);
// Get the triggers
let triggers = getPropertiesOfType(creatureId, 'trigger');
remove(triggers, trigger =>
trigger.event !== 'anyRest' &&
trigger.event !== 'longRest' &&
trigger.event !== 'shortRest'
);
triggers = groupBy(triggers, 'event');
for (let type in triggers) {
triggers[type] = groupBy(triggers[type], 'timing')
}
// Create the log
const log = CreatureLogSchema.clean({
creatureId: creature._id,
creatureName: creature.name,
// Rest
actionContext.addLog({
name: restType === 'shortRest' ? 'Short rest' : 'Long rest',
});
doRestWork(restType, actionContext);
const targets = [creature];
// Join, sort, and apply after triggers
const afterTriggers = union(
actionContext.triggers.anyRest?.after, actionContext.triggers[restType]?.after
).sort((a, b) => a.order - b.order);
applyTriggers(afterTriggers, null, actionContext);
applyTriggers(triggers, restType, 'before', { creature, targets, scope, log });
doRestWork(creature, restType);
applyTriggers(triggers, restType, 'after', { creature, targets, scope, log });
insertCreatureLogWork({log, creature, method: this});
// Insert log
actionContext.writeLog();
},
});
function applyTriggers(triggers, restType, timing, opts) {
// Get matching triggers
let selectedTriggers = triggers[restType]?.[timing] || [];
// Get any rest triggers as well
selectedTriggers = union(selectedTriggers, triggers['anyRest']?.[timing]);
selectedTriggers.sort((a, b) => a.order - b.order);
// Apply the triggers
selectedTriggers.forEach(trigger => {
applyTrigger(trigger, opts)
});
}
function doRestWork(creature, restType) {
function doRestWork(restType, actionContext) {
const creatureId = actionContext.creature._id;
// Long rests reset short rest properties as well
let resetFilter;
if (restType === 'shortRest'){
@@ -89,7 +64,7 @@ function doRestWork(creature, restType) {
}
// Only apply to active properties
let filter = {
'ancestors.id': creature._id,
'ancestors.id': creatureId,
reset: resetFilter,
removed: { $ne: true },
inactive: { $ne: true },
@@ -123,7 +98,7 @@ function doRestWork(creature, restType) {
// Reset half hit dice on a long rest, starting with the highest dice
if (restType === 'longRest'){
let hitDice = CreatureProperties.find({
'ancestors.id': creature._id,
'ancestors.id': creatureId,
type: 'attribute',
attributeType: 'hitDice',
removed: {$ne: true},
@@ -144,7 +119,7 @@ function doRestWork(creature, restType) {
hitDice.sort(compare);
// Get the total number of hit dice that can be recovered this rest
let totalHd = hitDice.reduce((sum, hd) => sum + (hd.value || 0), 0);
let resetMultiplier = creature.settings.hitDiceResetMultiplier || 0.5;
let resetMultiplier = actionContext.creature.settings.hitDiceResetMultiplier || 0.5;
let recoverableHd = Math.max(Math.floor(totalHd*resetMultiplier), 1);
// recover each hit dice in turn until the recoverable amount is used up
let amountToRecover, resultingDamage;