22 lines
1.0 KiB
TypeScript
22 lines
1.0 KiB
TypeScript
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
|
import SimpleSchema from 'simpl-schema';
|
|
import EngineActions, { EngineAction, ActionSchema } from '/imports/api/engine/action/EngineActions';
|
|
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions';
|
|
import { getCreature } from '/imports/api/engine/loadCreatures';
|
|
|
|
export const insertAction: ValidatedMethod = new ValidatedMethod({
|
|
name: 'actions.insertAction',
|
|
validate: new SimpleSchema({
|
|
action: ActionSchema
|
|
}).validator({ clean: true }),
|
|
run: async function ({ action }: { action: EngineAction }) {
|
|
assertEditPermission(getCreature(action.creatureId), this.userId);
|
|
// First remove all other actions on this creature
|
|
// only do one action at a time, don't wait for this to finish
|
|
EngineActions.removeAsync({ creatureId: action.creatureId });
|
|
// Force a random id even if one was provided, we may use it later as the seed for PRNG
|
|
delete action._id;
|
|
return await EngineActions.insertAsync(action);
|
|
},
|
|
});
|