44 lines
964 B
C#
44 lines
964 B
C#
using System.Security.Cryptography;
|
|
using System.Text.RegularExpressions;
|
|
|
|
using Dungeoneer.Lexing;
|
|
|
|
namespace Dungeoneer {
|
|
|
|
public class RollExpression {
|
|
private IList<string> Parts;
|
|
public string Print { get; private set; }
|
|
public string Expression { get; private set; }
|
|
public dynamic Result {
|
|
get {
|
|
try { return Scripting.Expr($"eval('{Expression}')"); }
|
|
catch { return null; }
|
|
}
|
|
}
|
|
|
|
public RollExpression(IList<string> parts) {
|
|
this.Parts = parts;
|
|
this.Print = "";
|
|
this.Expression = "";
|
|
|
|
// build expression from string parts
|
|
foreach(string piece in Parts) {
|
|
// die expression substitution
|
|
var dieCheck = DiceToken.Match(piece);
|
|
if(dieCheck.Success) {
|
|
var token = new DiceToken(dieCheck);
|
|
|
|
this.Print += $"{token.Format()} ";
|
|
this.Expression += $"{token.Expression()} ";
|
|
} else {
|
|
var part = $"{piece} ";
|
|
this.Expression += part;
|
|
this.Print += part;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|