Пример #1
0
        public void TestResolverRecursedCustomFunctions()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>
            {
                { "test", new Script("test", null, false, "The letter is {OneOf(\"a\", F(\"func\"), \"{c}\")}.") },
                { "func", new Script("func", null, false, "b") }
            };
            ScriptResolver resolver = new ScriptResolver(scripts);
            var            dict     = new Dictionary <string, Cottle.Value> {
                ["c"] = "c"
            };

            List <string> results = new List <string>();

            for (int i = 0; i < 1000; i++)
            {
                results.Add(resolver.resolveFromName("test", dict, true));
            }
            Assert.IsTrue(results.Contains(@"The letter is a."));
            results.RemoveAll(result => result == @"The letter is a.");
            Assert.IsTrue(results.Contains(@"The letter is b."));
            results.RemoveAll(result => result == @"The letter is b.");
            Assert.IsTrue(results.Contains(@"The letter is c."));
            results.RemoveAll(result => result == @"The letter is c.");
            Assert.IsTrue(results.Count == 0);
        }
Пример #2
0
        private void InitializeResolvers()
        {
            if (_replicationDocument?.ResolveByCollection == null)
            {
                if (ScriptConflictResolversCache.Count > 0)
                {
                    ScriptConflictResolversCache = new Dictionary <string, ScriptResolver>();
                }
                return;
            }
            var copy = new Dictionary <string, ScriptResolver>();

            foreach (var kvp in _replicationDocument.ResolveByCollection)
            {
                var collection = kvp.Key;
                var script     = kvp.Value.Script;
                if (string.IsNullOrEmpty(script.Trim()))
                {
                    continue;
                }
                copy[collection] = new ScriptResolver
                {
                    Script = script
                };
            }
            ScriptConflictResolversCache = copy;
        }
Пример #3
0
        public void TestResolverNativeSetCustomFunction()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>
            {
                { "test", new Script("test", null, false, "{set x to \"Hello\"} {OneOf(\"{x} world\")}") }
            };
            ScriptResolver resolver = new ScriptResolver(scripts);
            var            dict     = new Dictionary <string, Cottle.Value>();
            string         result   = resolver.resolveFromName("test", dict, true);

            Assert.AreEqual("Hello world", result);
        }
        public void TestResolverSimple()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>();

            scripts.Add("test", new Script("test", null, false, "Hello {name}"));
            ScriptResolver resolver = new ScriptResolver(scripts);
            Dictionary <string, Cottle.Value> dict = new Dictionary <string, Cottle.Value>();

            dict["name"] = "world";
            string result = resolver.resolve("test", dict);

            Assert.AreEqual("Hello world", result);
        }
Пример #5
0
        public void TestResolverSimple()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>
            {
                { "test", new Script("test", null, false, "Hello {name}") }
            };
            ScriptResolver resolver = new ScriptResolver(scripts);
            Dictionary <string, Cottle.Value> dict = new Dictionary <string, Cottle.Value> {
                ["name"] = "world"
            };
            string result = resolver.resolveFromName("test", dict, true);

            Assert.AreEqual("Hello world", result);
        }
Пример #6
0
        public void LoadScripts()
        {
            string[] files = CSharpCompiler.GetScripts("*.cs");
            Console.WriteLine("Found {0} Scripts.", files.Length);
            CSharpCompiler.Compile(files);
            // So look for our new assembly
            string   dll = Path.Combine(ServerGlobals.BaseDirectory, "Scripts\\Scripts.dll");
            Assembly a   = Assembly.LoadFrom(dll);

            if (a != null)
            {
                ScriptResolver.LoadScriptObjects(a);
            }
        }
Пример #7
0
        public void TestResolverFunctions()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>
            {
                { "func", new Script("func", null, false, "Hello {name}") },
                { "test", new Script("test", null, false, "Well {F(\"func\")}") }
            };
            ScriptResolver resolver = new ScriptResolver(scripts);
            var            dict     = new Dictionary <string, Cottle.Value> {
                ["name"] = "world"
            };
            string result = resolver.resolveFromName("test", dict, true);

            Assert.AreEqual("Well Hello world", result);
        }
Пример #8
0
        public void TestResolverFunctions()
        {
            Dictionary <string, Script> scripts = new Dictionary <string, Script>();

            scripts.Add("func", new Script("func", null, false, "Hello {name}"));
            scripts.Add("test", new Script("test", null, false, "Well {F(\"func\")}"));
            ScriptResolver resolver = new ScriptResolver(scripts);
            Dictionary <string, Cottle.Value> dict = new Dictionary <string, Cottle.Value>();

            dict["name"] = "world";
            string result = resolver.resolveFromName("test", dict);

            Assert.AreEqual("Well Hello world", result);
            string result2 = resolver.resolveFromValue(scripts["test"].Value, dict);

            Assert.AreEqual("Well Hello world", result2);
        }
Пример #9
0
        public static string SpeechFromScript(string script)
        {
            if (script == null)
            {
                return(null);
            }

            // Variable replacement
            Ship ship = ((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")).GetCurrentShip();

            if (ship != null)
            {
                script = script.Replace("$=", ship.phoneticname);
            }

            string cmdrScript;

            if (string.IsNullOrEmpty(EDDI.Instance.Cmdr?.name))
            {
                cmdrScript = "EDDI.Instance.Cmdr";
            }
            else
            {
                cmdrScript = "EDDI.Instance.Cmdr " + EDDI.Instance.Cmdr.phoneticname;
            }
            script = script.Replace("$-", cmdrScript);

            // Multiple choice selection
            StringBuilder sb = new StringBuilder();

            // Step 1 - resolve any options in square brackets
            Match matchResult = Regex.Match(script, @"\[[^\]]*\]|[^\[\]]+");

            while (matchResult.Success)
            {
                if (matchResult.Value.StartsWith("["))
                {
                    // Remove the brackets and pick one of the options
                    string   result  = matchResult.Value.Substring(1, matchResult.Value.Length - 2);
                    string[] options = result.Split(';');
                    sb.Append(options[random.Next(0, options.Length)]);
                }
                else
                {
                    // Pass it right along
                    sb.Append(matchResult.Groups[0].Value);
                }
                matchResult = matchResult.NextMatch();
            }
            string res = sb.ToString();

            // Step 2 - resolve phrases separated by semicolons
            if (res.Contains(";"))
            {
                // Pick one of the options
                string[] options = res.Split(';');
                res = options[random.Next(0, options.Length)];
            }

            // Step 3 - pass it through the script resolver
            res = new ScriptResolver(null).resolveFromValue(res);

            return(res ?? "");
        }
Пример #10
0
        public void TestGenerateFunctionsHelp()
        {
            // Prepare our functions
            var functionsList = new List <ICustomFunction>();
            var resolver      = new ScriptResolver(null);
            var store         = new BuiltinStore();
            var assy          = Assembly.GetAssembly(typeof(ScriptResolver));

            foreach (var type in assy.GetTypes()
                     .Where(t => t.IsClass && t.GetInterface(nameof(ICustomFunction)) != null))
            {
                var function = (ICustomFunction)(type.GetConstructor(Type.EmptyTypes) != null
                    ? Activator.CreateInstance(type) :
                                                 Activator.CreateInstance(type, resolver, store));

                if (function != null)
                {
                    functionsList.Add(function);
                }
            }

            // Organize functions in alphabetical order (except exclude functions that we've flagged as hidden)
            functionsList = functionsList
                            .Where(f => f.Category != FunctionCategory.Hidden)
                            .OrderBy(f => f.name)
                            .ToList();

            // Prepare Help.md
            List <string> help = new List <string>();

            help.Add("");
            help.Add(EddiSpeechResponder.Properties.CustomFunctions_Untranslated.HelpHeader);
            help.Add("");

            foreach (var function in functionsList)
            {
                help.Add($"### {function.name}()");
                help.Add("");
                help.Add(function.description);
                help.Add("");
            }

            // Prepare Functions.md
            List <string> functions = new List <string>();

            functions.Add("");
            functions.Add(EddiSpeechResponder.Properties.CustomFunctions_Untranslated.FunctionsHeader);
            functions.Add("");

            functionsList = functionsList.OrderBy(f => f.name).ToList();
            foreach (var function in functionsList)
            {
                functions.Add($"* {function.name}()");
            }

            // Make sure that a Wiki directory exists
            Directory.CreateDirectory(@"Wiki\");

            // Write our results
            File.WriteAllLines(@"Help.md", help);
            File.WriteAllLines(@"Wiki\Help.md", help);
            File.WriteAllLines(@"Wiki\Functions.md", functions);
        }