diff --git a/repl.py b/scripts/repl.py similarity index 100% rename from repl.py rename to scripts/repl.py diff --git a/src/Program.cs b/src/Program.cs index d4f22c2..71f0c2f 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -13,14 +13,13 @@ namespace Dungeoneer { public static Dictionary RollMacros = new Dictionary { // TODO: disadvantage ( roll twice and take lowest ) - // TODO: pool ( any of NdX > D => success ) + { "pool", (Command)Dungeoneer.RollMacro.Pool } }; public static void Main() { Console.WriteLine("Starting"); - Scripting.Run("roll.py"); - Scripting.Run("repl.py"); + Scripting.Run("scripts/repl.py"); } } diff --git a/src/Roll.cs b/src/Roll.cs index 72a23c2..9558d32 100644 --- a/src/Roll.cs +++ b/src/Roll.cs @@ -5,13 +5,50 @@ using Dungeoneer.Lexing; namespace Dungeoneer { + public class RollResult { + public dynamic Value { get; private set; } + + private RollResult() { Value = null; } + public RollResult(string expression) { + Value = Scripting.Expr($"eval('{expression}')"); + } + + internal static class Style { + internal const string Default = "\x1b[1m"; + + internal const string BoolTrue = "\x1b[32;1m"; + internal const string BoolFalse = "\x1b[31;1m"; + } + + public override string ToString() { + string style; + switch(Value) { + case bool boolValue: + style = boolValue ? Style.BoolTrue : Style.BoolFalse; + break; + default: + style = Style.Default; + break; + } + + return $"{style}{Value}{Format.Reset}"; + } + + public static RollResult Wrap(dynamic value) { + var output = new RollResult(); + output.Value = value; + return output; + } + + } + public class RollExpression { private IList Parts; public string Print { get; private set; } public string Expression { get; private set; } - public dynamic Result { + public RollResult Result { get { - try { return Scripting.Expr($"eval('{Expression}')"); } + try { return new RollResult(Expression); } catch { return null; } } } @@ -40,5 +77,33 @@ namespace Dungeoneer { } + public static class RollMacro { + + public static void Pool(IList args) { + DiceToken? dice = null; + int? dc = null; + + for(int i = 1; i < args.Count; i++) { + string arg = args[i]; + var dieCheck = DiceToken.Match(arg); + if(dieCheck.Success) + dice = new DiceToken(dieCheck); + else + dc = int.Parse(arg); + } + + var rolls = dice.Result; + bool success = false; + foreach(var roll in rolls) + if(roll > dc.Value) { + success = true; + break; + } + var result = RollResult.Wrap(success); + Console.WriteLine($"{dice.Format()}\n => {result}"); + } + + } + }