17 changed files
D20Tek.Common | ||
DataTypes/DiceNotation | ||
DiceResult.cs + | ||
DiceResultConverter.cs + | ||
IDice.cs + | ||
IDiceConfiguration.cs + | ||
IDieRoller.cs + | ||
TermResult.cs + | ||
TermResultListConverter.cs + | ||
D20Tek.Common.csproj + | ||
D20Tek.Common.xml + | ||
stylecop.json + | ||
D20Tek.Common.UnitTests | ||
DataTypes/DiceNotation | ||
DiceResultConverterTests.cs + | ||
DiceResultTests.cs + | ||
TermResultListConverterTests.cs + | ||
TermResultTests.cs + | ||
D20Tek.Common.UnitTests.csproj + | ||
azure-pipelines.yml + | ||
D20Tek.Common.sln + | ||
DiceResult.cs
/D20Tek.Common/DataTypes/DiceNotation/DiceResult.cs+105/D20Tek.Common/DataTypes/DiceNotation/DiceResult.cs
Add comment 1 Plus // <copyright file="DiceResult.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus using System;
Add comment 16 Plus using System.Collections.Generic;
Add comment 17 Plus using System.Linq;
Add comment 18 Plus using System.Text.Json.Serialization;
Add comment 19 Plus
Add comment 20 Plus namespace D20Tek.DiceNotation
Add comment 21 Plus {
Add comment 22 Plus /// <summary>
Add comment 23 Plus /// Class the represents the results for any dice expression calculation.
Add comment 24 Plus /// </summary>
Add comment 25 Plus public class DiceResult
Add comment 26 Plus {
Add comment 27 Plus private const string FudgeDiceIdentifier = "f";
Add comment 28 Plus private static readonly TermResultListConverter Converter = new TermResultListConverter();
Add comment 29 Plus
Add comment 30 Plus /// <summary>
Add comment 31 Plus /// Initializes a new instance of the <see cref="DiceResult"/> class.
Add comment 32 Plus /// </summary>
Add comment 33 Plus /// <param name="expression">dice expression rolled.</param>
Add comment 34 Plus /// <param name="results">List for results for each term in dice expression.</param>
Add comment 35 Plus /// <param name="rollerUsed">Define die roller used to get the results.</param>
Add comment 36 Plus /// <param name="config">Dice config to tell whether this result will be bounded or unbounded.</param>
Add comment 37 Plus public DiceResult(string expression, List<TermResult> results, string rollerUsed, IDiceConfiguration config)
Add comment 38 Plus : this(expression, results.Sum(r => (int)Math.Round(r.AppliesToResultCalculation ? r.Value * r.Scalar : 0)), results, rollerUsed, config)
Add comment 39 Plus {
Add comment 40 Plus }
Add comment 41 Plus
Add comment 42 Plus /// <summary>
Add comment 43 Plus /// Initializes a new instance of the <see cref="DiceResult"/> class.
Add comment 44 Plus /// </summary>
Add comment 45 Plus /// <param name="expression">dice expression rolled.</param>
Add comment 46 Plus /// <param name="value">calculated value of the results.</param>
Add comment 47 Plus /// <param name="results">List for results for each term in dice expression.</param>
Add comment 48 Plus /// <param name="rollerUsed">Define die roller used to get the results.</param>
Add comment 49 Plus /// <param name="config">Dice config to tell whether this result will be bounded or unbounded.</param>
Add comment 50 Plus public DiceResult(string expression, int value, List<TermResult> results, string rollerUsed, IDiceConfiguration config)
Add comment 51 Plus {
Add comment 52 Plus this.DiceExpression = expression;
Add comment 53 Plus this.DieRollerUsed = rollerUsed;
Add comment 54 Plus this.Results = results.ToList();
Add comment 55 Plus
Add comment 56 Plus bool boundedResult = expression.Contains(FudgeDiceIdentifier) ? false : config.HasBoundedResult;
Add comment 57 Plus this.Value = boundedResult ? Math.Max(value, config.BoundedResultMinimum) : value;
Add comment 58 Plus }
Add comment 59 Plus
Add comment 60 Plus /// <summary>
Add comment 61 Plus /// Initializes a new instance of the <see cref="DiceResult"/> class.
Add comment 62 Plus /// </summary>
Add comment 63 Plus public DiceResult()
Add comment 64 Plus {
Add comment 65 Plus }
Add comment 66 Plus
Add comment 67 Plus /// <summary>
Add comment 68 Plus /// Gets or sets the dice expression that was evaluated.
Add comment 69 Plus /// </summary>
Add comment 70 Plus public string DiceExpression { get; set; }
Add comment 71 Plus
Add comment 72 Plus /// <summary>
Add comment 73 Plus /// Gets or sets the die roller used to generate this result.
Add comment 74 Plus /// </summary>
Add comment 75 Plus public string DieRollerUsed { get; set; }
Add comment 76 Plus
Add comment 77 Plus /// <summary>
Add comment 78 Plus /// Gets the display text representation of the Results list.
Add comment 79 Plus /// </summary>
Add comment 80 Plus [JsonIgnore]
Add comment 81 Plus public string RollsDisplayText
Add comment 82 Plus {
Add comment 83 Plus get
Add comment 84 Plus {
Add comment 85 Plus if (this.Results == null)
Add comment 86 Plus {
Add comment 87 Plus return string.Empty;
Add comment 88 Plus }
Add comment 89 Plus
Add comment 90 Plus return Converter.Convert(new List<TermResult>(this.Results), typeof(string), null, "en-us") as string;
Add comment 91 Plus }
Add comment 92 Plus }
Add comment 93 Plus
Add comment 94 Plus /// <summary>
Add comment 95 Plus /// Gets or sets the results list for all of the terms in this dice expression.
Add comment 96 Plus /// </summary>
Add comment 97 Plus public IReadOnlyList<TermResult> Results { get; set; }
Add comment 98 Plus
Add comment 99 Plus /// <summary>
Add comment 100 Plus /// Gets or sets the numeric value for this result.
Add comment 101 Plus /// </summary>
Add comment 102 Plus public int Value { get; set; }
Add comment 103 Plus }
Add comment 104 Plus }
Add comment 105 Plus
DiceResultConverter.cs
/D20Tek.Common/DataTypes/DiceNotation/DiceResultConverter.cs+67/D20Tek.Common/DataTypes/DiceNotation/DiceResultConverter.cs
Add comment 1 Plus // <copyright file="DiceResultConverter.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus using System;
Add comment 16 Plus
Add comment 17 Plus namespace D20Tek.DiceNotation
Add comment 18 Plus {
Add comment 19 Plus /// <summary>
Add comment 20 Plus /// Value converter to change a DiceResult into a display string.
Add comment 21 Plus /// </summary>
Add comment 22 Plus public class DiceResultConverter
Add comment 23 Plus {
Add comment 24 Plus /// <summary>
Add comment 25 Plus /// Converts the DiceResult into a string respresentation.
Add comment 26 Plus /// </summary>
Add comment 27 Plus /// <param name="value">value to convert.</param>
Add comment 28 Plus /// <param name="targetType">target type of the conversion.</param>
Add comment 29 Plus /// <param name="parameter">converter parameter.</param>
Add comment 30 Plus /// <param name="language">language.</param>
Add comment 31 Plus /// <returns>Converted value.</returns>
Add comment 32 Plus public virtual object Convert(object value, Type targetType, object parameter, string language)
Add comment 33 Plus {
Add comment 34 Plus if (targetType != typeof(string))
Add comment 35 Plus {
Add comment 36 Plus throw new ArgumentException("Unexpected type passed to converter.", nameof(targetType));
Add comment 37 Plus }
Add comment 38 Plus
Add comment 39 Plus if (value == null)
Add comment 40 Plus {
Add comment 41 Plus throw new ArgumentNullException(nameof(value));
Add comment 42 Plus }
Add comment 43 Plus
Add comment 44 Plus if (!(value is DiceResult dr))
Add comment 45 Plus {
Add comment 46 Plus throw new ArgumentException("Object not of type DiceResult.", nameof(value));
Add comment 47 Plus }
Add comment 48 Plus
Add comment 49 Plus return string.Format("{0} ({1})", dr.Value, dr.DiceExpression);
Add comment 50 Plus }
Add comment 51 Plus
Add comment 52 Plus /// <summary>
Add comment 53 Plus /// Converts back from string representation to a DiceResult.
Add comment 54 Plus /// </summary>
Add comment 55 Plus /// <param name="value">value to convert.</param>
Add comment 56 Plus /// <param name="targetType">target type of the conversion.</param>
Add comment 57 Plus /// <param name="parameter">converter parameter.</param>
Add comment 58 Plus /// <param name="language">language.</param>
Add comment 59 Plus /// <returns>Converted value.</returns>
Add comment 60 Plus public virtual object ConvertBack(object value, Type targetType, object parameter, string language)
Add comment 61 Plus {
Add comment 62 Plus // the reverse conversion is not supported.
Add comment 63 Plus throw new NotSupportedException();
Add comment 64 Plus }
Add comment 65 Plus }
Add comment 66 Plus }
Add comment 67 Plus
IDice.cs
/D20Tek.Common/DataTypes/DiceNotation/IDice.cs+81/D20Tek.Common/DataTypes/DiceNotation/IDice.cs
Add comment 1 Plus // <copyright file="IDice.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus namespace D20Tek.DiceNotation
Add comment 16 Plus {
Add comment 17 Plus /// <summary>
Add comment 18 Plus /// Interface that defines operations on dice.
Add comment 19 Plus /// </summary>
Add comment 20 Plus public interface IDice
Add comment 21 Plus {
Add comment 22 Plus /// <summary>
Add comment 23 Plus /// Gets the current configuration for this set of Dice.
Add comment 24 Plus /// </summary>
Add comment 25 Plus IDiceConfiguration Configuration { get; }
Add comment 26 Plus
Add comment 27 Plus /// <summary>
Add comment 28 Plus /// Clears the current dice expression.
Add comment 29 Plus /// </summary>
Add comment 30 Plus void Clear();
Add comment 31 Plus
Add comment 32 Plus /// <summary>
Add comment 33 Plus /// Creates a DiceTerm with specified values for this dice expression.
Add comment 34 Plus /// </summary>
Add comment 35 Plus /// <param name="sides">sides of die.</param>
Add comment 36 Plus /// <param name="numberDice">number of dice.</param>
Add comment 37 Plus /// <param name="scalar">scalar multiplier.</param>
Add comment 38 Plus /// <param name="choose">choose how many results to return.</param>
Add comment 39 Plus /// <param name="exploding">Exploding threshold for dice re-rolls.</param>
Add comment 40 Plus /// <returns>IDice representing the current terms.</returns>
Add comment 41 Plus IDice Dice(int sides, int numberDice = 1, double scalar = 1, int? choose = null, int? exploding = null);
Add comment 42 Plus
Add comment 43 Plus /// <summary>
Add comment 44 Plus /// Creates a FudgeDiceTerm with specified values for this dice expression.
Add comment 45 Plus /// </summary>
Add comment 46 Plus /// <param name="numberDice">number of dice.</param>
Add comment 47 Plus /// <param name="choose">choose how many results to return.</param>
Add comment 48 Plus /// <returns>IDice representing the current terms.</returns>
Add comment 49 Plus IDice FudgeDice(int numberDice = 1, int? choose = null);
Add comment 50 Plus
Add comment 51 Plus /// <summary>
Add comment 52 Plus /// Creates a ConstantTerm with the specified value for this dice expression.
Add comment 53 Plus /// </summary>
Add comment 54 Plus /// <param name="constant">Constant value.</param>
Add comment 55 Plus /// <returns>IDice representing the current terms.</returns>
Add comment 56 Plus IDice Constant(int constant);
Add comment 57 Plus
Add comment 58 Plus /// <summary>
Add comment 59 Plus /// Concatenates the terms of another Dice expression into this Dice expression.
Add comment 60 Plus /// </summary>
Add comment 61 Plus /// <param name="otherDice">Other IDice terms to concatentate.</param>
Add comment 62 Plus /// <returns>IDice representing the current terms.</returns>
Add comment 63 Plus IDice Concat(IDice otherDice);
Add comment 64 Plus
Add comment 65 Plus /// <summary>
Add comment 66 Plus /// Rolls the dice for all of the terms in this expression.
Add comment 67 Plus /// </summary>
Add comment 68 Plus /// <param name="dieRoller">Die roller to use in calculations.</param>
Add comment 69 Plus /// <returns>Dice results.</returns>
Add comment 70 Plus DiceResult Roll(IDieRoller dieRoller);
Add comment 71 Plus
Add comment 72 Plus /// <summary>
Add comment 73 Plus /// Rolls the dice for the dice expression as a string.
Add comment 74 Plus /// </summary>
Add comment 75 Plus /// <param name="expression">Expression string to parse.</param>
Add comment 76 Plus /// <param name="dieRoller">Die roller to use in calculations.</param>
Add comment 77 Plus /// <returns>Dice results.</returns>
Add comment 78 Plus DiceResult Roll(string expression, IDieRoller dieRoller);
Add comment 79 Plus }
Add comment 80 Plus }
Add comment 81 Plus
IDiceConfiguration.cs
/D20Tek.Common/DataTypes/DiceNotation/IDiceConfiguration.cs+38/D20Tek.Common/DataTypes/DiceNotation/IDiceConfiguration.cs
Add comment 1 Plus // <copyright file="IDiceConfiguration.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus namespace D20Tek.DiceNotation
Add comment 16 Plus {
Add comment 17 Plus /// <summary>
Add comment 18 Plus /// Interface for dice configuration which allows users to customize
Add comment 19 Plus /// some of the default behavior of the DiceNotation system.
Add comment 20 Plus /// </summary>
Add comment 21 Plus public interface IDiceConfiguration
Add comment 22 Plus {
Add comment 23 Plus /// <summary>
Add comment 24 Plus /// Gets or sets the defualt sides of dice to use when it's omitted from dice notation.
Add comment 25 Plus /// </summary>
Add comment 26 Plus int DefaultDieSides { get; set; }
Add comment 27 Plus
Add comment 28 Plus /// <summary>
Add comment 29 Plus /// Gets or sets a value indicating whether these dice have their results bounded to 1 or greater.
Add comment 30 Plus /// </summary>
Add comment 31 Plus bool HasBoundedResult { get; set; }
Add comment 32 Plus
Add comment 33 Plus /// <summary>
Add comment 34 Plus /// Gets or sets the value for the bounded minimum.
Add comment 35 Plus /// </summary>
Add comment 36 Plus int BoundedResultMinimum { get; set; }
Add comment 37 Plus }
Add comment 38 Plus }
IDieRoller.cs
/D20Tek.Common/DataTypes/DiceNotation/IDieRoller.cs+31/D20Tek.Common/DataTypes/DiceNotation/IDieRoller.cs
Add comment 1 Plus // <copyright file="IDieRoller.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus namespace D20Tek.DiceNotation
Add comment 16 Plus {
Add comment 17 Plus /// <summary>
Add comment 18 Plus /// Interface defining the random die roller.
Add comment 19 Plus /// </summary>
Add comment 20 Plus public interface IDieRoller
Add comment 21 Plus {
Add comment 22 Plus /// <summary>
Add comment 23 Plus /// Rolls the die with the specified number of sides.
Add comment 24 Plus /// </summary>
Add comment 25 Plus /// <param name="sides">Number of sides on the die (also its max value).</param>
Add comment 26 Plus /// <param name="factor">Provide a factor to impact the roll (defaults to nothing, but used for fudge dice).</param>
Add comment 27 Plus /// <returns>Random value between 1 and sides.</returns>
Add comment 28 Plus int Roll(int sides, int? factor = null);
Add comment 29 Plus }
Add comment 30 Plus }
Add comment 31 Plus
TermResult.cs
/D20Tek.Common/DataTypes/DiceNotation/TermResult.cs+44/D20Tek.Common/DataTypes/DiceNotation/TermResult.cs
Add comment 1 Plus // <copyright file="TermResult.cs" company="DarthPedro">
Add comment 2 Plus // Copyright (c) 2020 DarthPedro. All rights reserved.
Add comment 3 Plus // </copyright>
Add comment 4 Plus
Add comment 5 Plus //-----------------------------------------------------------------------
Add comment 6 Plus // <summary>
Add comment 7 Plus // This project is licensed under the MIT license.
Add comment 8 Plus //
Add comment 9 Plus // D20Tek.CommonDataTypes is an open source project that parses,
Add comment 10 Plus // evalutes, and rolls dice that conform to the defined Dice notiation.
Add comment 11 Plus // This notation is usable in any form of random dice games and role-playing
Add comment 12 Plus // games like D&D and d20.
Add comment 13 Plus // </summary>
Add comment 14 Plus //-----------------------------------------------------------------------
Add comment 15 Plus namespace D20Tek.DiceNotation
Add comment 16 Plus {
Add comment 17 Plus /// <summary>
Add comment 18 Plus /// The resulting role for a single term in dice notation.
Add comment 19 Plus /// </summary>
Add comment 20 Plus public class TermResult
Add comment 21 Plus {
Add comment 22 Plus /// <summary>
Add comment 23 Plus /// Gets or sets the scalar multiplier of this result.
Add comment 24 Plus /// </summary>
Add comment 25 Plus public double Scalar { get; set; }
Add comment 26 Plus
Add comment 27 Plus /// <summary>
Add comment 28 Plus /// Gets or sets the result value.
Add comment 29 Plus /// </summary>
Add comment 30 Plus public int Value { get; set; }
Add comment 31 Plus
Add comment 32 Plus /// <summary>
Add comment 33 Plus /// Gets or sets the type of the term.
Add comment 34 Plus /// </summary>
Add comment 35 Plus public string Type { get; set; }
Add comment 36 Plus
Add comment 37 Plus /// <summary>
Add comment 38 Plus /// Gets or sets a value indicating whether this term applies to
Add comment 39 Plus /// the final result calculation.
Add comment 40 Plus /// </summary>
Add comment 41 Plus public bool AppliesToResultCalculation { get; set; } = true;
Add comment 42 Plus }
Add comment 43 Plus }
Add comment 44 Plus
TermResultListConverter.cs
/D20Tek.Common/DataTypes/DiceNotation/TermResultListConverter.cs/D20Tek.Common/DataTypes/DiceNotation/TermResultListConverter.cs
DiceResultConverterTests.cs
/D20Tek.Common.UnitTests/DataTypes/DiceNotation/DiceResultConverterTests.cs/D20Tek.Common.UnitTests/DataTypes/DiceNotation/DiceResultConverterTests.cs
DiceResultTests.cs
/D20Tek.Common.UnitTests/DataTypes/DiceNotation/DiceResultTests.cs/D20Tek.Common.UnitTests/DataTypes/DiceNotation/DiceResultTests.cs
TermResultListConverterTests.cs
/D20Tek.Common.UnitTests/DataTypes/DiceNotation/TermResultListConverterTests.cs/D20Tek.Common.UnitTests/DataTypes/DiceNotation/TermResultListConverterTests.cs
TermResultTests.cs
/D20Tek.Common.UnitTests/DataTypes/DiceNotation/TermResultTests.cs/D20Tek.Common.UnitTests/DataTypes/DiceNotation/TermResultTests.cs
D20Tek.Common.UnitTests.csproj
/D20Tek.Common.UnitTests/D20Tek.Common.UnitTests.csproj/D20Tek.Common.UnitTests/D20Tek.Common.UnitTests.csproj