Пример #1
0
        public void AsInTitle()
        {
            var src = @"
an assist 
displayAs 
tunafish rep 
conditions    
player x is ‘mr vol’
dialogs
:player1        
muffin ducker
outcomes
set x to y";

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "an assist",
                    DisplayAs  = "tunafish rep",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Op    = "=",
                            Left  = "player.x",
                            Right = "'mr vol'"
                        }
                    },
                    Dialog = new DialogRule.DialogPart[]
                    {
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "'player1'",
                            Content      = "muffin ducker",
                            ContentParts = new string[]
                            {
                                "'muffin ducker'",
                            }
                        }
                    },
                    Outcomes = new DialogRule.DialogOutcome[]
                    {
                        new DialogRule.DialogOutcome()
                        {
                            Command   = "set",
                            Target    = "x",
                            Arguments = new Dictionary <string, string>()
                            {
                                { "", "y" }
                            }
                        }
                    }
                }
            };

            var bundle = new DialogBundle()
            {
                Name          = "test",
                Rules         = rules,
                ConditionSets = new DialogConditionSet[]
                {
                }
            };

            var expected = JsonConvert.SerializeObject(bundle, Formatting.None, new JsonSerializerSettings()
            {
                StringEscapeHandling = StringEscapeHandling.Default
            });

            var program = new WordLangResults(src);

            Assert.AreEqual(false, program.ParserErrors.AnyErrors);

            var v      = new ProgramToJson();
            var output = v.Visit(program.ProgramContext);

            Assert.IsNotNull(output);

            var backwards = JsonConvert.DeserializeObject <DialogBundle>(output);

            Assert.AreEqual(backwards, bundle);
            Assert.AreEqual(expected, output);
        }
Пример #2
0
        public void SimpleJSON()
        {
            var src = @"
tunafish 
conditions
a is b

a nifty rule 
     displayAs 
   tunafish rep 
conditions    
player health + 50 > 100
player x is ‘okay’
use tunafish
dialogs
:player1        
speaking {player health + 12} line text
:{player2.name}
something <b>meaningful</b>
:player3
this is <color='red'> red text </color>
 outcomes
    set x to y
run actions.fart with power as 100 * player.fart
with target as enemy";

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "a nifty rule",
                    DisplayAs  = "tunafish rep",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Op    = ">",
                            Left  = "(+ player.health 50)",
                            Right = "100"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Op    = "=",
                            Left  = "player.x",
                            Right = "'okay'"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Op    = "=",
                            Left  = "__.conditions.tunafish",
                            Right = "true"
                        }
                    },
                    Dialog = new DialogRule.DialogPart[]
                    {
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "'player1'",
                            Content      = "speaking {player health + 12} line text",
                            ContentParts = new string[]
                            {
                                "'speaking '",
                                "(+ player.health 12)",
                                "' line text'"
                            }
                        },
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "player2.name",
                            Content      = "something <b>meaningful</b>",
                            ContentParts = new string[]
                            {
                                "'something <b>meaningful</b>'"
                            }
                        },
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "'player3'",
                            Content      = "this is <color='red'> red text </color>",
                            ContentParts = new string[]
                            {
                                "'this is <color=\\'red\\'> red text </color>'"
                            }
                        }
                    },
                    Outcomes = new DialogRule.DialogOutcome[]
                    {
                        new DialogRule.DialogOutcome()
                        {
                            Command   = "set",
                            Target    = "x",
                            Arguments = new Dictionary <string, string>()
                            {
                                { "", "y" }
                            }
                        },
                        new DialogRule.DialogOutcome()
                        {
                            Command   = "run",
                            Target    = "actions.fart",
                            Arguments = new Dictionary <string, string>()
                            {
                                { "power", "(* 100 player.fart)" },
                                { "target", "enemy" }
                            }
                        }
                    }
                }
            };

            var bundle = new DialogBundle()
            {
                Name          = "test",
                Rules         = rules,
                ConditionSets = new DialogConditionSet[]
                {
                    new DialogConditionSet()
                    {
                        Name       = "tunafish",
                        Conditions = new DialogConditionSet.DialogCondition[]
                        {
                            new DialogConditionSet.DialogCondition()
                            {
                                Left  = "a",
                                Op    = "=",
                                Right = "b"
                            }
                        }
                    }
                }
            };

            var expected = JsonConvert.SerializeObject(bundle, Formatting.None, new JsonSerializerSettings()
            {
                StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
            });
            //var expected = "[" +
            //    "{" +
            //        "\"title\":\"a nifty rule\"," +
            //        "\"displayAs\":\"tunafish rep\"," +
            //        "\"conditions\":[" +
            //            "{" +
            //                "\"op\":\"=!\"," +
            //                "\"left\":\"yaday first\"," +
            //                "\"right\":\"blahblah some more\"" +
            //            "}," +
            //            "{" +
            //                "\"op\":\"=\"," +
            //                "\"left\":\"another line of fun\"," +
            //                "\"right\":\"twice the pain\"" +
            //            "}" +
            //        "]," +
            //        "\"dialogs\":[" +
            //            "{" +
            //                "\"speaker\":\"player1\"," +
            //                "\"line\":\"speaking line text\r\nmulti line\"" +
            //            "}," +
            //            "{" +
            //                "\"speaker\":\"player2\"," +
            //                "\"line\":\"something meaningful\"" +
            //            "}" +
            //        "]," +
            //        "\"outcomes\":[" +
            //            "{" +
            //                "\"action\":\"doit\"" +
            //            "}," +
            //            "{" +
            //                "\"action\":\"doit again\"" +
            //            "}" +
            //        "]" +
            //    "}" +
            //    "" +
            //    "]";
            // if thething is theotherthing:
            // muffin is true

            var program = new WordLangResults(src);

            Assert.AreEqual(false, program.ParserErrors.AnyErrors);

            var v      = new ProgramToJson();
            var output = v.Visit(program.ProgramContext);

            Assert.IsNotNull(output);

            var backwards = JsonConvert.DeserializeObject <DialogBundle>(output);

            Assert.AreEqual(backwards, bundle);
            Assert.AreEqual(expected, output);
        }