improved roll command
This commit is contained in:
parent
94c401e85c
commit
d4875d06fc
2 changed files with 34 additions and 48 deletions
77
Command.cs
77
Command.cs
|
@ -1,55 +1,42 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Dungeoneer {
|
namespace Dungeoneer {
|
||||||
|
|
||||||
public sealed class Command {
|
public sealed class Command {
|
||||||
|
|
||||||
public static void Roll(IList<string> args) {
|
public static void Roll(IList<string> args) {
|
||||||
var argc = args.Count;
|
if(args.Count == 0)
|
||||||
if(argc == 1) {
|
return;
|
||||||
if(args[0].Contains("d")) {
|
var expression = "";
|
||||||
var parts = args[0].Split('d');
|
|
||||||
Roll(new List<string> { parts[1], parts[0] });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var arg = args[0];
|
|
||||||
int sides;
|
|
||||||
try { sides = int.Parse(arg); }
|
|
||||||
catch(Exception _) {
|
|
||||||
Console.WriteLine($"'{arg}' is not a number");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var result = Dungeoneer.Util.Roll(sides);
|
|
||||||
Console.WriteLine($" -> {result}");
|
|
||||||
} else if(argc == 2) {
|
|
||||||
var result = 0;
|
|
||||||
var first = args[0];
|
|
||||||
int sides;
|
|
||||||
try { sides = int.Parse(first); }
|
|
||||||
catch(Exception _) {
|
|
||||||
Console.WriteLine($"'{first}' is not a number");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var second = args[1];
|
|
||||||
int count;
|
|
||||||
try { count = int.Parse(second); }
|
|
||||||
catch(Exception _) {
|
|
||||||
Console.WriteLine($"'{second}' is not a number");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(count == 1) {
|
|
||||||
Roll(new List<string> { first });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var work = "";
|
foreach(string arg in args) {
|
||||||
for(int _ = 0; _ < count; _++) {
|
string part;
|
||||||
var roll = Dungeoneer.Util.Roll(sides);
|
if(arg.Contains('d')) {
|
||||||
result += roll;
|
var parts = arg.Split('d');
|
||||||
work += $"{roll} ";
|
var first = parts[0];
|
||||||
}
|
var second = parts[1];
|
||||||
Console.WriteLine($"{work}\n -> {result}");
|
|
||||||
} else
|
int count;
|
||||||
Console.WriteLine($"'roll' accepts 1 or 2 arguments ({argc} given)");
|
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}");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,8 @@ namespace Dungeoneer {
|
||||||
Scope.roll = (Func<int, int>)Dungeoneer.Util.Roll;
|
Scope.roll = (Func<int, int>)Dungeoneer.Util.Roll;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Run(string file) {
|
public static void Run(string file) { Engine.ExecuteFile(file, Scope); }
|
||||||
Engine.ExecuteFile(file, Scope);
|
public static dynamic Expr(string expression) { return Engine.Execute(expression, Scope); }
|
||||||
}
|
|
||||||
|
|
||||||
public static string Input(string prompt) {
|
public static string Input(string prompt) {
|
||||||
Console.Write(prompt);
|
Console.Write(prompt);
|
||||||
|
|
Reference in a new issue