Started work on UI for rolling checks

This commit is contained in:
Stefan Zermatten
2020-09-29 16:37:28 +02:00
parent df7000889b
commit 75ab43da00
5 changed files with 233 additions and 10 deletions

View File

@@ -5,15 +5,18 @@ import Creatures from '/imports/api/creature/Creatures.js';
import { assertEditPermission } from '/imports/api/creature/creaturePermissions.js';
import roll from '/imports/parser/roll.js';
const doAction = new ValidatedMethod({
const doCheck = new ValidatedMethod({
name: 'creature.doCheck',
validate: new SimpleSchema({
actionId: SimpleSchema.RegEx.Id,
targetId: {
creatureId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
attributeName: {
type: String,
optional: true,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
@@ -23,18 +26,31 @@ const doAction = new ValidatedMethod({
run({creatureId, attributeName}) {
let creature = Creatures.findOne(creatureId);
assertEditPermission(creature, this.userId);
return doCheckWork({attributeName, creature});
let bonus = getAttributeValue({creature, attributeName})
return doCheckWork({bonus});
},
});
function doCheckWork({attributeName, creature}){
function getAttributeValue({creature, attributeName}){
let att = creature.variables[attributeName];
if (!att) throw new Meteor.Error('No such attribute',
`This creature does not have a ${attributeName} property`);
let bonus = att.attributeType === 'ability'? att.modifier : att.value;
//Always roll 2d20 and let the advantage be decided in UI
let rolls = roll(2,20);
return {rolls, bonus};
return bonus || 0;
}
export default doAction;
export function doCheckWork({bonus, advantage = 0}){
let rolls = roll(2,20);
let chosenRoll;
if (advantage === 1){
chosenRoll = Math.max.apply(rolls);
} else if (advantage === -1){
chosenRoll = Math.min.apply(rolls);
} else {
chosenRoll = rolls[0];
}
let result = chosenRoll + bonus;
return {rolls, bonus, chosenRoll, result};
}
export default doCheck;