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

@@ -1,13 +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 {
getProperyAncestors, getPropertyDecendants
} from '/imports/api/engine/loadCreatures.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
import { doActionWork } from '/imports/api/engine/actions/doAction.js';
import { CreatureLogSchema } from '/imports/api/creature/log/CreatureLogs.js';
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
const doAction = new ValidatedMethod({
name: 'creatureProperties.doCastSpell',
@@ -39,47 +42,31 @@ const doAction = new ValidatedMethod({
numRequests: 10,
timeInterval: 5000,
},
run({spellId, slotId, targetIds = [], scope = {}}) {
run({ spellId, slotId, targetIds = [], scope = {} }) {
// Get action context
let spell = CreatureProperties.findOne(spellId);
const creatureId = spell.ancestors[0].id;
const actionContext = new ActionContext(creatureId, targetIds, this);
// Check permissions
let creature = getRootCreatureAncestor(spell);
assertEditPermission(creature, this.userId);
// Get all the targets and make sure we can edit them
let targets = [];
targetIds.forEach(targetId => {
let target = Creatures.findOne(targetId);
assertEditPermission(actionContext.creature, this.userId);
actionContext.targets.forEach(target => {
assertEditPermission(target, this.userId);
targets.push(target);
});
// Fetch all the action's ancestor creatureProperties
const ancestorIds = [];
spell.ancestors.forEach(ref => {
if (ref.collection === 'creatureProperties') {
ancestorIds.push(ref.id);
}
});
const ancestors = getProperyAncestors(creatureId, spell._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: spell._id}, {'ancestors.id': spell._id}],
removed: {$ne: true},
}, {
sort: {order: 1},
});
const properties = getPropertyDecendants(creatureId, spell._id);
properties.push(spell);
properties.sort((a, b) => a.order - b.order);
// Spend the appropriate slot
let slotLevel = spell.level || 0;
let slot;
actionContext.scope['slotLevel'] = slotLevel;
if (slotId && !spell.castWithoutSpellSlots){
slot = CreatureProperties.findOne(slotId);
if (!slot){
@@ -104,34 +91,32 @@ const doAction = new ValidatedMethod({
}
slotLevel = slot.spellSlotLevel.value;
damagePropertyWork({
property: slot,
prop: slot,
operation: 'increment',
value: 1,
actionContext,
});
}
scope['slotLevel'] = slotLevel;
// Post the slot level spent to the log
const log = CreatureLogSchema.clean({
creatureId: creature._id,
creatureName: creature.name,
});
if (slot?.spellSlotLevel?.value){
log.content.push({
actionContext.addLog({
name: `Casting using a level ${slotLevel} spell slot`
});
} else if (slotLevel) {
log.content.push({
actionContext.addLog({
name: `Casting at level ${slotLevel}`
});
}
// Do the action
doActionWork({ creature, targets, properties, ancestors, method: this, methodScope: scope, log });
doActionWork({
properties, ancestors, actionContext, methodScope: scope,
});
// Force the characters involved to recalculate
Creatures.update({
_id: { $in: [creature._id, ...targetIds] }
_id: { $in: [creatureId, ...targetIds] }
}, {
$set: { dirty: true },
});