implemented basic repl and roll function
This commit is contained in:
parent
3a80cb18a9
commit
94c401e85c
6 changed files with 105 additions and 5 deletions
58
Command.cs
Normal file
58
Command.cs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
namespace Dungeoneer {
|
||||||
|
|
||||||
|
public sealed class Command {
|
||||||
|
|
||||||
|
public static void Roll(IList<string> args) {
|
||||||
|
var argc = args.Count;
|
||||||
|
if(argc == 1) {
|
||||||
|
if(args[0].Contains("d")) {
|
||||||
|
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 = "";
|
||||||
|
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)");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
11
Object.cs
Normal file
11
Object.cs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
using System.Dynamic;
|
||||||
|
|
||||||
|
namespace Dungeoneer.Objects {
|
||||||
|
|
||||||
|
public class Character {
|
||||||
|
public string Name;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -4,10 +4,15 @@ namespace Dungeoneer {
|
||||||
|
|
||||||
public sealed class Program {
|
public sealed class Program {
|
||||||
|
|
||||||
|
public delegate void Command(IList<string> args);
|
||||||
|
public static Dictionary<string, Command> ReplCommands = new Dictionary<string, Command> {
|
||||||
|
{ "roll", (Command)Dungeoneer.Command.Roll }
|
||||||
|
};
|
||||||
|
|
||||||
public static void Main() {
|
public static void Main() {
|
||||||
Console.WriteLine("Starting");
|
Console.WriteLine("Starting");
|
||||||
|
|
||||||
Dungeoneer.Scripting.Run("test.py");
|
Scripting.Run("repl.py");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
18
Scripting.cs
18
Scripting.cs
|
@ -6,7 +6,7 @@ namespace Dungeoneer {
|
||||||
|
|
||||||
public static class Scripting {
|
public static class Scripting {
|
||||||
private static ScriptEngine Engine;
|
private static ScriptEngine Engine;
|
||||||
private static dynamic Scope;
|
public static dynamic Scope;
|
||||||
|
|
||||||
static Scripting() {
|
static Scripting() {
|
||||||
// set up python engine
|
// set up python engine
|
||||||
|
@ -14,17 +14,29 @@ namespace Dungeoneer {
|
||||||
Scope = Engine.CreateScope();
|
Scope = Engine.CreateScope();
|
||||||
dynamic builtin = Engine.GetBuiltinModule();
|
dynamic builtin = Engine.GetBuiltinModule();
|
||||||
|
|
||||||
|
var paths = Engine.GetSearchPaths();
|
||||||
|
paths.Add("/usr/lib/python3.12/");
|
||||||
|
Engine.SetSearchPaths(paths);
|
||||||
|
|
||||||
// set up python environment
|
// set up python environment
|
||||||
builtin.RemoveVariable("compile");
|
builtin.SetVariable("input", (Func<string, string>)Input);
|
||||||
builtin.SetVariable("exec", (Action<string>)Run);
|
|
||||||
builtin.RemoveVariable("open");
|
builtin.RemoveVariable("open");
|
||||||
|
|
||||||
|
Scope.repl = Program.ReplCommands;
|
||||||
|
|
||||||
|
// helper functions
|
||||||
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 string Input(string prompt) {
|
||||||
|
Console.Write(prompt);
|
||||||
|
return Console.ReadLine();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
Util.cs
2
Util.cs
|
@ -5,7 +5,7 @@ namespace Dungeoneer {
|
||||||
public static class Util {
|
public static class Util {
|
||||||
|
|
||||||
public static int Roll(int sides) {
|
public static int Roll(int sides) {
|
||||||
return RandomNumberGenerator.GetInt32(sides);
|
return RandomNumberGenerator.GetInt32(sides) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
14
repl.py
Normal file
14
repl.py
Normal file
|
@ -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()
|
||||||
|
|
Reference in a new issue