Started work on checks

This commit is contained in:
Stefan Zermatten
2020-09-18 14:00:29 +02:00
parent c8ddf9d547
commit bc5c465a32
5 changed files with 61 additions and 6 deletions

View File

@@ -0,0 +1,40 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
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({
name: 'creature.doCheck',
validate: new SimpleSchema({
actionId: SimpleSchema.RegEx.Id,
targetId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({creatureId, attributeName}) {
let creature = Creatures.findOne(creatureId);
assertEditPermission(creature, this.userId);
return doCheckWork({attributeName, creature});
},
});
function doCheckWork({attributeName, creature}){
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};
}
export default doAction;

View File

@@ -1,6 +1,7 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import RollArrayNode from '/imports/parser/parseTree/RollArrayNode.js';
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
import roll from '/imports/parser/roll.js';
export default class RollNode extends ParseNode {
constructor({left, right}) {
@@ -49,12 +50,7 @@ export default class RollNode extends ParseNode {
context,
});
let diceSize = right.value;
let randomSrc = DDP.randomStream('diceRoller');
let values = [];
for (let i = 0; i < number; i++){
let roll = ~~(randomSrc.fraction() * diceSize) + 1
values.push(roll);
}
let values = roll(number, diceSize);
if (context){
context.storeRoll({number, diceSize, values});
}

View File

@@ -0,0 +1,9 @@
export default function roll(number, diceSize){
let values = [];
let randomSrc = DDP.randomStream('diceRoller');
for (let i = 0; i < number; i++){
let roll = ~~(randomSrc.fraction() * diceSize) + 1
values.push(roll);
}
return values;
}

View File

@@ -0,0 +1,9 @@
<template lang="html" />
<script>
export default {
}
</script>
<style lang="css" scoped>
</style>

1
dataSources/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
Renders