@dice-roller/rpg-dice-roller / Evaluator
Class: Evaluator
Defined in: src/evaluator/Evaluator.ts:87
Walks the AST and builds the result tree.
Evaluation uses the supplied context's roll mode, generator and limits. The same AST can be evaluated in random mode for a roll and in min / max mode for its corresponding totals.
Constructors
Constructor
new Evaluator(
registries):Evaluator
Defined in: src/evaluator/Evaluator.ts:89
Creates an evaluator using the given die and modifier registries.
Parameters
registries
Returns
Evaluator
Properties
registries
readonlyregistries:EvaluatorRegistries
Defined in: src/evaluator/Evaluator.ts:89
Methods
applyModifiers()
protectedapplyModifiers<T>(items,modifiers,target,input):T[]
Defined in: src/evaluator/Evaluator.ts:433
Applies the modifiers in execution order (ModifierRegistry.inExecutionOrder()).
Type Parameters
T
T extends ScoredItem
Parameters
items
readonly T[]
modifiers
readonly ModifierNode[]
target
input
Omit<ModifierInput, "modifier">
Returns
T[]
Throws
a handler doesn't apply to the target (e.g. explode on a group)
applyToFlattenedDice()
protectedapplyToFlattenedDice(result,apply):ExpressionResult
Defined in: src/evaluator/Evaluator.ts:262
Applies apply to the flattened dice of every pool in the result, then writes the dice back into their own pools, in the order apply returned them, and recalculates the result.
Each die is tagged with its pool, so the handler may copy, reorder or remove dice.
Parameters
result
apply
(dice) => TaggedDieResult[]
Returns
Throws
the handler returned a die that didn't come from a pool
createDie()
protectedcreateDie(node,args):ResolvedDie
Defined in: src/evaluator/Evaluator.ts:177
Creates and resolves the die of a pool node, with already evaluated arguments.
Parameters
node
args
readonly number[]
Returns
evaluate()
evaluate(
node,ctx):ExpressionResult
Defined in: src/evaluator/Evaluator.ts:96
Evaluates an expression tree into a result tree, rolling dice according to the context.
Parameters
node
ctx
Returns
Throws
if a node, quantity, operation or modifier result is invalid
evaluateBinary()
protectedevaluateBinary(node,ctx):BinaryResult
Defined in: src/evaluator/Evaluator.ts:373
Uses operators from math.ts.
Parameters
node
ctx
Returns
evaluateFunction()
protectedevaluateFunction(node,ctx):FunctionResult
Defined in: src/evaluator/Evaluator.ts:404
Uses functions from math.ts.
Parameters
node
ctx
Returns
evaluateGroup()
protectedevaluateGroup(node,ctx):GroupResult
Defined in: src/evaluator/Evaluator.ts:191
Evaluates each expression, then applies the modifiers in execution order.
Each handler gets the members, unless its groupHandling says otherwise:
flatten: as in v5, a group with a single expression ({4d6+2d8}k2) gives the handler the dice of every pool inside it. The die results are flattened, then the result tree is walked again to write them back into their pools and recalculate everything above them.each-pool: the handler gets each nested group's members and each pool's dice.
Parameters
node
ctx
Returns
evaluateNumber()
protectedevaluateNumber(node,_ctx):NumberResult
Defined in: src/evaluator/Evaluator.ts:120
Parameters
node
_ctx
Returns
evaluateParen()
protectedevaluateParen(node,ctx):ParenResult
Defined in: src/evaluator/Evaluator.ts:397
Parameters
node
ctx
Returns
evaluatePool()
protectedevaluatePool(node,ctx):PoolResult
Defined in: src/evaluator/Evaluator.ts:135
Evaluates qty and the die's args, validates qty against ctx.limits.maxQty, creates the die with registries.dice.create(node.die.name, ...args), resolves it with resolveDie(), rolls it qty times with rollDie(), then applies the modifiers.
Parameters
node
ctx
Returns
evaluateUnary()
protectedevaluateUnary(node,ctx):UnaryResult
Defined in: src/evaluator/Evaluator.ts:391
Uses negate from math.ts.
Parameters
node
ctx
Returns
handlerFor()
protectedhandlerFor(modifier,target):ModifierHandler
Defined in: src/evaluator/Evaluator.ts:454
The handler of the modifier.
Parameters
modifier
target
Returns
Throws
no handler is registered, or it doesn't apply to the target
rebuildPools()
protectedrebuildPools(result,replace,transformMembers?):ExpressionResult
Defined in: src/evaluator/Evaluator.ts:308
Walks the result tree, replacing each pool with replace(pool, index) and recalculating the values above it. Pools are visited in notation order.
Pools in a pool's quantity or die arguments aren't visited (they've already been used), nor are pools in nested groups unless transformMembers is supplied.
Parameters
result
replace
(pool, index) => PoolResult
transformMembers?
(members) => GroupMemberResult[]
Returns
runHandler()
protectedrunHandler<T>(handler,items,input):T[]
Defined in: src/evaluator/Evaluator.ts:470
Type Parameters
T
T extends ScoredItem
Parameters
handler
items
readonly T[]
input
Returns
T[]
Throws
the handler doesn't return an array