/// <summary> /// Evaluates the node and asserts that the number of rolls and result match what is expected. /// </summary> /// <param name="node"></param> /// <param name="conf"></param> /// <param name="expectedRolls"></param> /// <param name="expectedResult"></param> protected static void EvaluateNode(DiceAST node, RollData data, int expectedRolls, string expectedResult) { var result = Roller.Roll(node, data); Assert.AreEqual(expectedRolls, result.NumRolls); Assert.AreEqual(expectedResult, result.ToString()); }
public override void ExitRollFudge([NotNull] DiceGrammarParser.RollFudgeContext context) { // we'll have 1 or 2 + # of extras nodes on the stack, bottom-most 2 nodes are the number of dice and sides List <DiceAST> extras = new List <DiceAST>(); DiceAST numSides = null, numDice; for (int i = 0; i < context.basic_extras().Length + context.basic_function().Length; i++) { extras.Add(Stack.Pop()); } extras.Reverse(); if (context.unary_expr().Length > 1) { numSides = Stack.Pop(); } numDice = Stack.Pop(); var partial = new RollPartialNode(new RollNode(RollType.Fudge, numDice, numSides)); foreach (var node in extras) { if (node is RerollNode) { partial.AddReroll((RerollNode)node); } else if (node is ExplodeNode) { partial.AddExplode((ExplodeNode)node); } else if (node is KeepNode) { partial.AddKeep((KeepNode)node); } else if (node is SuccessNode) { partial.AddSuccess((SuccessNode)node); } else if (node is CritNode) { partial.AddCritical((CritNode)node); } else if (node is SortNode) { partial.AddSort((SortNode)node); } else if (node is FunctionNode) { partial.AddFunction((FunctionNode)node); } } Stack.Push(partial.CreateRollNode()); }
/// <summary> /// Evaluates the node and asserts that a DiceException was thrown with the specified error code. /// </summary> /// <param name="node"></param> /// <param name="conf"></param> /// <param name="error"></param> protected static void EvaluateNode(DiceAST node, RollData data, DiceErrorCode error) { try { Roller.Roll(node, data); Assert.Fail("Expected DiceException with error code {0}, but did not receive an exception.", error.ToString()); } catch (DiceException e) { if (e.ErrorCode != error) { Assert.Fail("Expected DiceException with error code {0}, but got error code {1}.", error.ToString(), e.ErrorCode.ToString()); } } }
internal RollResult(RollData data, DiceAST rollRoot, int numRolls) { RollRoot = rollRoot; // cache some commonly-referenced information directly in this class instead of requiring // the user to drill down into RollRoot for everything (and because RollRoot isn't available // if deserializing). ResultType = rollRoot.ValueType; Value = rollRoot.Value; Values = rollRoot.Values; NumRolls = numRolls; Expression = rollRoot.ToString(); Metadata = data.Metadata; AllRolls = data.InternalContext.AllRolls; AllMacros = data.InternalContext.AllMacros; }
internal ValueSnapshot(DiceAST expr) { Value = expr.Value; Values = expr.Values.ToList(); ValueType = expr.ValueType; }
internal string AddGroupExpression(DiceAST expr) { GroupExpressions.Add(expr); GroupValues.Add(new ValueSnapshot(expr)); return((GroupExpressions.Count - 1).ToString()); }
/// <summary> /// Evaluates the root of the tree, returning the RollResult. /// </summary> /// <param name="root"></param> /// <param name="data"></param> /// <returns></returns> internal static RollResult Roll(DiceAST root, RollData data) { var numRolls = root.Evaluate(data, root, 0); return(new RollResult(data, root, (int)numRolls)); }