implemented 'pool' roll macro
This commit is contained in:
parent
3c3b6b859c
commit
a0286194b2
3 changed files with 69 additions and 5 deletions
|
@ -13,14 +13,13 @@ namespace Dungeoneer {
|
|||
|
||||
public static Dictionary<string, Command> RollMacros = new Dictionary<string, Command> {
|
||||
// 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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
69
src/Roll.cs
69
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<string> 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<string> 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}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue