diff --git a/Command.cs b/Command.cs new file mode 100644 index 0000000..6117ccc --- /dev/null +++ b/Command.cs @@ -0,0 +1,58 @@ + +namespace Dungeoneer { + + public sealed class Command { + + public static void Roll(IList args) { + var argc = args.Count; + if(argc == 1) { + if(args[0].Contains("d")) { + var parts = args[0].Split('d'); + Roll(new List { 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 { first }); + return; + } + + var work = ""; + for(int _ = 0; _ < count; _++) { + var roll = Dungeoneer.Util.Roll(sides); + result += roll; + work += $"{roll} "; + } + Console.WriteLine($"{work}\n -> {result}"); + } else + Console.WriteLine($"'roll' accepts 1 or 2 arguments ({argc} given)"); + } + + } + +} + diff --git a/Object.cs b/Object.cs new file mode 100644 index 0000000..6db54e5 --- /dev/null +++ b/Object.cs @@ -0,0 +1,11 @@ +using System.Dynamic; + +namespace Dungeoneer.Objects { + + public class Character { + public string Name; + + } + +} + diff --git a/Program.cs b/Program.cs index 5ec3385..ea72dfa 100644 --- a/Program.cs +++ b/Program.cs @@ -4,10 +4,15 @@ namespace Dungeoneer { public sealed class Program { + public delegate void Command(IList args); + public static Dictionary ReplCommands = new Dictionary { + { "roll", (Command)Dungeoneer.Command.Roll } + }; + public static void Main() { Console.WriteLine("Starting"); - Dungeoneer.Scripting.Run("test.py"); + Scripting.Run("repl.py"); } } diff --git a/Scripting.cs b/Scripting.cs index 361afda..a6f6561 100644 --- a/Scripting.cs +++ b/Scripting.cs @@ -6,7 +6,7 @@ namespace Dungeoneer { public static class Scripting { private static ScriptEngine Engine; - private static dynamic Scope; + public static dynamic Scope; static Scripting() { // set up python engine @@ -14,17 +14,29 @@ namespace Dungeoneer { Scope = Engine.CreateScope(); dynamic builtin = Engine.GetBuiltinModule(); + var paths = Engine.GetSearchPaths(); + paths.Add("/usr/lib/python3.12/"); + Engine.SetSearchPaths(paths); + // set up python environment - builtin.RemoveVariable("compile"); - builtin.SetVariable("exec", (Action)Run); + builtin.SetVariable("input", (Func)Input); builtin.RemoveVariable("open"); + Scope.repl = Program.ReplCommands; + + // helper functions Scope.roll = (Func)Dungeoneer.Util.Roll; } public static void Run(string file) { Engine.ExecuteFile(file, Scope); } + + public static string Input(string prompt) { + Console.Write(prompt); + return Console.ReadLine(); + } + } } diff --git a/Util.cs b/Util.cs index bbd239f..eebb985 100644 --- a/Util.cs +++ b/Util.cs @@ -5,7 +5,7 @@ namespace Dungeoneer { public static class Util { public static int Roll(int sides) { - return RandomNumberGenerator.GetInt32(sides); + return RandomNumberGenerator.GetInt32(sides) + 1; } } diff --git a/repl.py b/repl.py new file mode 100644 index 0000000..b78b06f --- /dev/null +++ b/repl.py @@ -0,0 +1,14 @@ + +def repl_loop(): + while True: + raw = input("> ") + if raw == 'exit': + break + parts = raw.split(" ") + command = parts[0] + del parts[0] + if command in repl: + repl[command](parts) + +repl_loop() +