Progress on action choices

This commit is contained in:
Thaum Rystra
2024-03-30 21:12:35 +02:00
parent 6c3d4b91eb
commit 6138be8083
33 changed files with 210 additions and 55 deletions

View File

@@ -5,6 +5,7 @@ import { assertEditPermission } from '/imports/api/sharing/sharingPermissions';
import { getCreature } from '/imports/api/engine/loadCreatures';
import applyAction from '/imports/api/engine/action/functions/applyAction';
import writeChangedAction from '../functions/writeChangedAction';
import getReplayChoicesInputProvider from '/imports/api/engine/action/functions/userInput/getReplayChoicesInputProvider';
export const runAction = new ValidatedMethod({
name: 'actions.runAction',
@@ -13,23 +14,32 @@ export const runAction = new ValidatedMethod({
type: Object,
blackbox: true,
},
userInput: {
decisions: {
type: Array,
},
'decisions.$': {
type: Object,
blackbox: true,
optional: true,
},
stepThrough: {
type: Boolean,
optional: true,
}
}).validator(),
run: async function ({ actionId, userInput }: { actionId: string, userInput?: any }) {
run: async function ({ actionId, decisions }: { actionId: string, decisions: any[] }) {
// Get the action
const action = await EngineActions.findOneAsync(actionId);
if (!action) throw 'Action not found';
if (!action) throw new Meteor.Error('not-found', 'Action not found');
// Permissions
assertEditPermission(getCreature(action.creatureId), this.userId);
// Keep a copy of the original so that a diff can be done later to store what changed
const originalAction = EJSON.clone(action);
// Replay the user's decisions as user input
const userInput = getReplayChoicesInputProvider(actionId, decisions);
// Apply the action
applyAction(action, userInput);
// Persist changes to the action
// Persist changes
const writePromise = writeChangedAction(originalAction, action);
return writePromise;
},