dungeoneer-cs/Command.cs
2024-03-30 01:51:02 -04:00

45 lines
896 B
C#

using System.Text.RegularExpressions;
namespace Dungeoneer {
public sealed class Command {
public static void Roll(IList<string> args) {
if(args.Count == 0)
return;
var expression = "";
foreach(string arg in args) {
string part;
if(arg.Contains('d')) {
var parts = arg.Split('d');
var first = parts[0];
var second = parts[1];
int count;
int sides;
try {
count = int.Parse(first);
sides = int.Parse(second);
} catch {
Console.WriteLine("'{arg}' is not a valid dice expression");
return;
}
var result = 0;
for(int i = 0; i < count; i++)
result += Util.Roll(sides);
part = result.ToString();
} else
part = arg;
expression += $"{part} ";
}
dynamic output = Scripting.Expr($"eval('{expression}')");
Console.WriteLine($"{expression}\n=> {output}");
}
}
}