Source/Expression/DiceTerm.cs
/// <summary>
/// Gets the TermResult for this DiceTerm which will include the random value rolled
/// </summary>
/// <param name="context">The evaluation context</param>
/// <returns>An IEnumerable of TermResult which will have one item per die rolled</returns>
Gets the TermResult for this DiceTerm which will include the random value rolled
Gets the TermResult for this DiceTerm which will include the random value rolled
public override ExpressionResult Evaluate(EvaluationContext context)
		{
			if (Multiplicity == 0)
			{
				return new DiceExpressionResult()
				{
					Value = 0,
					Sides = Sides,
					TermType = $"0{Constants.DiceSeperator}{Sides}"
				};
			}
			List<DiceExpressionResult> results =
				(from i in Enumerable.Range(0, Multiplicity)
				 select DiceTypeResolver.Roll(Kind, context.Roller, Sides))
				.ToList();
			foreach (IDiceOption option in Settings.Options)
			{
				results = option.Apply(results, context.Roller).ToList();
			}
			if (results.Count == 0)
			{
				throw new Exceptions.ImpossibleDieException("There are no results for this die roll.");
			}
			if (results.Count == 1)
			{
				return results[0];
			}
			return new MultipleDiceTermResult(results)
			{
				TermType = $"{Multiplicity}{Constants.DiceSeperator}{Settings.SidesString}"
			};
		}
public override ExpressionResult Evaluate ( EvaluationContext context ) { if ( Multiplicity == 0 ) { return new DiceExpressionResult ( ) { Value = 0 , Sides = Sides , TermType = $" 0 { Constants . DiceSeperator } { Sides } " } ; } List < DiceExpressionResult > results = ( from i in Enumerable . Range ( 0 , Multiplicity ) select DiceTypeResolver . Roll ( Kind , context . Roller , Sides ) ) . ToList ( ) ; foreach ( IDiceOption option in Settings . Options ) { results = option . Apply ( results , context . Roller ) . ToList ( ) ; } if ( results . Count == 0 ) { throw new Exceptions . ImpossibleDieException ( " " ) ; } if ( results . Count == 1 ) { return results [ 0 ] ; } return new MultipleDiceTermResult ( results ) { TermType = $" { Multiplicity } { Constants . DiceSeperator } { Settings . SidesString } " } ; }
Gets the TermResult for this DiceTerm which will include the random value rolled
Gets the TermResult for this DiceTerm which will include the random value rolled
// a 0 multiplicity die is a bizarre thing. // maybe we should be throwing exceptions, but for now // we will just give them back a 0. // roll the dice now, and force evaluation now // by using ToList, or the rolls will keep flowing. // apply each option in sequence, // realizing the results immediately so that // each option later can safely operate on the results. // todo: investigate performance of this // make sure we have results. // if we only have one result, no need to wrap it up in a multi-term.
An IEnumerable of TermResult which will have one item per die rolled
An IEnumerable of TermResult which will have one item per die rolled
context
EvaluationContext
The evaluation context
The evaluation context