Ejemplo n.º 1
0
        public void TestMethodTestINRangeNeg2()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "No:aNo:bNo:c",
                lcdMod.ExecuteHandlebarScript(pf, new
            {
                I = new[] {
                    new Item()
                    {
                        Id = 1, Name = "a"
                    },
                    new Item()
                    {
                        Id = 2, Name = "b"
                    },
                    new Item()
                    {
                        Id = 3, Name = "c"
                    },
                }
            }, "{{#each I}}{{test Id in '-1--2'}}Yes:{{Name}}{{else}}No:{{Name}}{{/test}}{{/each}}")
                );
        }
Ejemplo n.º 2
0
        public void TestMethodTestLEQReal()
        {
            var S = Substitute.For <IStructureData>();

            S.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 10, Name = "a"
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b"
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c"
                },
            });

            var E = Substitute.For <IEntityData>();

            E.S.Returns(S);

            var lcdData = Substitute.For <IScriptRootData>();

            lcdData.E.Returns(E);

            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "Yes:aYes:bNo:c",
                lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{#each E.S.Items}}{{test Id leq 2}}Yes:{{Name}}{{else}}No:{{Name}}{{/test}}{{/each}}")
                );
        }
Ejemplo n.º 3
0
        public void TestMethodSortedEachEntities()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            var e1 = Substitute.For <IEntity>();

            e1.Name.ReturnsForAnyArgs("a");
            e1.Position.ReturnsForAnyArgs(new UnityEngine.Vector3(0, 0, 0));
            e1.Type.ReturnsForAnyArgs(EntityType.BA);

            var e2 = Substitute.For <IEntity>();

            e2.Name.ReturnsForAnyArgs("b");
            e2.Position.ReturnsForAnyArgs(new UnityEngine.Vector3(10, 10, 10));
            e2.Type.ReturnsForAnyArgs(EntityType.BA);

            pf.AllEntities = new[] { e1, e2 };

            var data = new ScriptSaveGameRootData(pf, pf.AllEntities, pf.AllEntities, null, e1, null, null);

            Assert.AreEqual(
                "a:0 # b:17,32051 # ",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#entitiesbyname '*'}}{{#sortedeach . 'Distance'}}{{./Name}}:{{./Distance}} # {{/sortedeach}}{{/entitiesbyname}}")
                );
            Assert.AreEqual(
                "b:17,32051 # a:0 # ",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#entitiesbyname '*'}}{{#sortedeach . 'Distance' true}}{{./Name}}:{{./Distance}} # {{/sortedeach}}{{/entitiesbyname}}")
                );
        }
Ejemplo n.º 4
0
        public static void HandleMoveLostItems(PlayfieldScriptData root)
        {
            while (root.MoveLostItems.TryDequeue(out var restore))
            {
                try
                {
                    var count = restore.SourceE.S.GetCurrent().GetDevice <IContainer>(restore.Source)?.AddItems(restore.Id, restore.Count);
                    if (count > 0)
                    {
                        root.MoveLostItems.Enqueue(new ItemMoveInfo()
                        {
                            Id      = restore.Id,
                            Count   = count.Value,
                            SourceE = restore.SourceE,
                            Source  = restore.Source,
                        });
                        Log($"HandleMoveLostItems(partial restored): {restore.Source} {restore.Id} #{restore.Count} -> {count}", restore.Count == count ? LogLevel.Debug : LogLevel.Message);
                    }
                    else
                    {
                        Log($"HandleMoveLostItems(restored): {restore.Source} {restore.Id} #{restore.Count}", LogLevel.Message);
                    }
                }
                catch (Exception error)
                {
                    Log($"HandleMoveLostItems(error): {restore.Source} {restore.Id} #{restore.Count} -> {EmpyrionScripting.ErrorFilter(error)}", LogLevel.Debug);
                    root.MoveLostItems.Enqueue(restore);
                }

                if (root.ScriptExecQueue.TimeLimitSyncReached())
                {
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        public void TestMethodScroll2()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            var data = Substitute.For <IScriptRootData>();

            data.CycleCounter.Returns(0);
            Assert.AreEqual(
                "1\n2\n3\n",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#scroll 3 1 2}}{{#split '1-2-3-4-5-6-7-8-9' '-'}}{{#each .}}{{.}}\n{{/each}}{{/split}}{{/scroll}}")
                );

            data.CycleCounter.Returns(1);
            Assert.AreEqual(
                "3\n4\n5\n",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#scroll 3 1 2}}{{#split '1-2-3-4-5-6-7-8-9' '-'}}{{#each .}}{{.}}\n{{/each}}{{/split}}{{/scroll}}")
                );

            data.CycleCounter.Returns(4);
            Assert.AreEqual(
                "9\n1\n2\n",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#scroll 3 1 2}}{{#split '1-2-3-4-5-6-7-8-9' '-'}}{{#each .}}{{.}}\n{{/each}}{{/split}}{{/scroll}}")
                );

            data.CycleCounter.Returns(8);
            Assert.AreEqual(
                "8\n9\n1\n",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{#scroll 3 1 2}}{{#split '1-2-3-4-5-6-7-8-9' '-'}}{{#each .}}{{.}}\n{{/each}}{{/split}}{{/scroll}}")
                );
        }
Ejemplo n.º 6
0
        public void TestMethodTestLEQ()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "Yes:aYes:bNo:c",
                lcdMod.ExecuteHandlebarScript(pf, new
            {
                S = new {
                    I = new[] {
                        new ItemsData()
                        {
                            Id = 1, Name = "a"
                        },
                        new ItemsData()
                        {
                            Id = 2, Name = "b"
                        },
                        new ItemsData()
                        {
                            Id = 3, Name = "c"
                        },
                    }
                }
            }, "{{#each S.I}}{{test Id leq 2}}Yes:{{Name}}{{else}}No:{{Name}}{{/test}}{{/each}}")
                );
        }
Ejemplo n.º 7
0
        public void TestMethodColor()
        {
            var lcdData = Substitute.For <IScriptRootData>();
            var lcdMod  = new EmpyrionScripting();
            var pf      = new PlayfieldScriptData(lcdMod);

            lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{color 'ff00ff'}}");
            Assert.AreEqual(new UnityEngine.Color(255, 0, 255), lcdData.Color);
        }
Ejemplo n.º 8
0
 public ScriptSaveGameRootData(
     PlayfieldScriptData playfieldScriptData,
     IEntity[] allEntities,
     IEntity[] currentEntities,
     IPlayfield playfield,
     IEntity entity,
     ConcurrentDictionary <string, object> persistendData,
     EventStore eventStore) : base(playfieldScriptData, allEntities, currentEntities, playfield, entity, persistendData, eventStore)
 {
 }
Ejemplo n.º 9
0
        public void TestMethodSortedEachObjectReverse()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "321",
                lcdMod.ExecuteHandlebarScript(pf, "", "{{#split '3,2,1' ','}}{{#sortedeach . '' true}}{{.}}{{/sortedeach}}{{/split}}")
                );
        }
Ejemplo n.º 10
0
        public void TestMethodUnicodeEscape()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "äöü äöü",
                lcdMod.ExecuteHandlebarScript(pf, "äöü", "äöü {{this}}")
                );
        }
Ejemplo n.º 11
0
        public void TestMethodTestDateTimeMESZFormat()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                DateTime.Now.ToString("dd MMM HH:mm:ss"),
                lcdMod.ExecuteHandlebarScript(pf, "", "{{datetime 'dd MMM HH:mm:ss'}}")
                );
        }
Ejemplo n.º 12
0
        public void TestMethodFormatPercentNeg()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "-50,0%",
                lcdMod.ExecuteHandlebarScript(pf, -0.5, "{{format . '{0:P1}'}}")
                );
        }
Ejemplo n.º 13
0
        public void TestMethodTestDateTime()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                DateTime.Now.ToString(),
                lcdMod.ExecuteHandlebarScript(pf, "", "{{datetime}}")
                );
        }
Ejemplo n.º 14
0
        public void TestMethodTestScroll()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);
            var data   = Substitute.For <IScriptRootData>();

            data.CycleCounter.Returns(0);

            var outtext = lcdMod.ExecuteHandlebarScript(pf, data, "{{#scroll 5 1}}\nLine 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\nLine 11\nLine 12\nLine 13\n{{/scroll}}");

            Assert.AreEqual("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n", outtext);
        }
Ejemplo n.º 15
0
        public void TestMethodSaveGamesScriptsRoot()
        {
            var lcdData = Substitute.For <ScriptSaveGameRootData>();

            lcdData.MainScriptPath = "abc";
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "abc",
                lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{MainScriptPath}}")
                );
        }
Ejemplo n.º 16
0
        public void TestMethodSortedEachTypeValue()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "[2, a][3, b][1, c]",
                lcdMod.ExecuteHandlebarScript(pf, new[] {
                new KeyValuePair <int, string>(1, "c"),
                new KeyValuePair <int, string>(2, "a"),
                new KeyValuePair <int, string>(3, "b"),
            }, "{{#sortedeach . 'Value'}}{{.}}{{/sortedeach}}")
                );
        }
Ejemplo n.º 17
0
        public void TestMethodSaveGamesScripts()
        {
            EmpyrionScripting.SaveGameModPath = @"C:\steamcmd\empyrion\PlayfieldServer/../Saves/Games/Test\Mods\EmpyrionScripting";
            var lcdMod = new EmpyrionScripting();

            lcdMod.SaveGamesScripts = new SaveGamesScripts(null)
            {
                SaveGameModPath = EmpyrionScripting.SaveGameModPath
            };
            lcdMod.SaveGamesScripts.ReadSaveGamesScripts();

            var pf = new PlayfieldScriptData(lcdMod);

            lcdMod.ExecFoundSaveGameScripts(pf, new ScriptSaveGameRootData(pf, null, null, null, null, null, null), Path.Combine(lcdMod.SaveGamesScripts.MainScriptPath, "CV"));
        }
Ejemplo n.º 18
0
        public void TestMethodItemsPartial()
        {
            var S = Substitute.For <IStructureData>();

            S.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            S.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10
                                                                      }, new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxX", Id = 1, Count = 1
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });

            var E = Substitute.For <IEntityData>();

            E.S.Returns(S);

            var lcdData = Substitute.For <IScriptRootData>();

            lcdData.E.Returns(E);

            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual(
                "Yes:1 #10",
                lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{#items E.S 'BoxA'}}{{test Id leq 2}}Yes:{{Name}} #{{Count}}{{else}}No:{{Name}}{{/test}}{{/items}}")
                );
        }
Ejemplo n.º 19
0
        public void TestMethodCsSimpleTextReturn()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data = Substitute.For <IScriptRootData>();

            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual("3", lcdMod.ExecuteCsScript(pf, data, "return \"3\";"));
        }
Ejemplo n.º 20
0
        public void TestMethodCsAssemlbyReturn()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data = Substitute.For <IScriptRootData>();

            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual("42", lcdMod.ExecuteCsScript(pf, data, "public class ModMain { public static int Main(IScriptModData root) { return 42; }}"));
        }
Ejemplo n.º 21
0
        public void TestMethodCsReturnTask()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data = Substitute.For <IScriptRootData>();

            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual("", lcdMod.ExecuteCsScript(pf, data, "return new Task(async () => {});"));
        }
Ejemplo n.º 22
0
        public void TestMethodCsScriptWithException()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data = Substitute.For <IScriptRootData>();

            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual("Exception: Test\n\nScript output up to exception:\n", lcdMod.ExecuteCsScript(pf, data, "throw new Exception(\"Test\");"));
        }
Ejemplo n.º 23
0
        public void TestMethodLookUp()
        {
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data = Substitute.For <IScriptRootData>();

            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual(
                "3",
                lcdMod.ExecuteHandlebarScript(pf, data, "{{set 'index' 2}}{{#split '1-2-3-4-5-6-7-8-9' '-'}}{{lookup . @root.Data.index}}{{/split}}")
                );
        }
Ejemplo n.º 24
0
        public void TestMethodCsExtMethods()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data      = Substitute.For <IScriptRootData>();
            var playfield = Substitute.For <IPlayfield>();

            playfield.Name.Returns("PlayfieldTestName");
            data.P = new PlayfieldData(playfield);
            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;

            data.CycleCounter.Returns(0);
            Assert.AreEqual("a-a1", lcdMod.ExecuteCsScript(pf, data, "return string.Join(\"-\", new[]{\"a\", \"b\", \"a1\"}.GetUniqueNames(\"a*\"));"));
        }
Ejemplo n.º 25
0
        public void TestMethodCsPlayfieldName()
        {
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data      = Substitute.For <IScriptRootData>();
            var playfield = Substitute.For <IPlayfield>();

            playfield.Name.Returns("PlayfieldTestName");
            data.P = new PlayfieldData(playfield);
            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;
            data.Console.Returns(new CsHelper.ConsoleMock(data));

            data.CycleCounter.Returns(0);
            Assert.AreEqual("PlayfieldTestName", lcdMod.ExecuteCsScript(pf, data, "Console.Write(P.Name);"));
        }
Ejemplo n.º 26
0
        public ScriptRootData(
            PlayfieldScriptData playfieldScriptData,
            IEntity[] allEntities,
            IEntity[] currentEntities,
            IPlayfield playfield,
            IEntity entity,
            ConcurrentDictionary <string, object> persistendData,
            EventStore eventStore) : this()
        {
            _PlayfieldScriptData = playfieldScriptData;
            _PersistendData      = persistendData;
            this.currentEntities = currentEntities;
            this.allEntities     = allEntities;
            this.playfield       = playfield;
            this.entity          = entity;
            SignalEventStore     = eventStore;
            var testGroup = entity?.Faction.Group == FactionGroup.Faction || entity?.Faction.Group == FactionGroup.Player ? entity?.Faction.Id.ToString() : entity?.Faction.Group.ToString();

            IsElevatedScript = this is ScriptSaveGameRootData || EmpyrionScripting.Configuration.Current.ElevatedGroups.Any(f => f == testGroup);

            try  { CultureInfo = playfieldScriptData?.EntityCultureInfo?.GetOrAdd(entity.Id, e => GetCultureInfoForEntity(entity)); }
            catch { CultureInfo = new EntityCultureInfo(); }
        }
Ejemplo n.º 27
0
        public void TestMethodItemsMoveToOtherEntity()
        {
            ConveyorHelpers.CreateDeviceLock = (R, P, S, V) => new MockDeviceLock();

            // SV ==================================================================
            var SVboxA = Substitute.For <IContainer>();

            SVboxA.RemoveItems(1, int.MaxValue).Returns(10);
            SVboxA.AddItems(1, 1).Returns(0);
            SVboxA.AddItems(1, 10).Returns(C => throw new Exception("in the same box"));

            var SVboxB = Substitute.For <IContainer>();

            SVboxB.AddItems(1, 10).Returns(1);

            var esSV = Substitute.For <IStructure>();

            esSV.GetDevice <IContainer>("BoxA").Returns(SVboxA);
            esSV.GetDevice <IContainer>("BoxB").Returns(SVboxB);

            var eeSV = Substitute.For <IEntity>();

            eeSV.Structure.Returns(esSV);

            var eSV = Substitute.For <IEntityData>();

            eSV.Name.Returns("SVa");
            eSV.GetCurrent().Returns(eeSV);

            var sSV = Substitute.For <IStructureData>();

            sSV.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            sSV.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10, Container = SVboxA, E = eSV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20, Container = SVboxB, E = eSV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });
            sSV.ContainerSource.Returns(new System.Collections.Concurrent.ConcurrentDictionary <string, IContainerSource>()
            {
                ["BoxA"] = new ContainerSource()
                {
                    Container = SVboxA, CustomName = "BoxA"
                },
                ["BoxB"] = new ContainerSource()
                {
                    Container = SVboxB, CustomName = "BoxB"
                },
            });
            sSV.GetCurrent().Returns(esSV);
            sSV.E.Returns(eSV);
            eSV.S.Returns(sSV);

            // CV ==================================================================
            var CVboxA = Substitute.For <IContainer>();

            CVboxA.RemoveItems(1, int.MaxValue).Returns(10);
            CVboxA.AddItems(1, 1).Returns(0);
            CVboxA.AddItems(1, 10).Returns(C => throw new Exception("in the same box"));

            var CVboxB = Substitute.For <IContainer>();

            CVboxB.AddItems(1, 10).Returns(1);

            var esCV = Substitute.For <IStructure>();

            esCV.GetDevice <IContainer>("BoxA").Returns(CVboxA);
            esCV.GetDevice <IContainer>("BoxB").Returns(CVboxB);

            var eeCV = Substitute.For <IEntity>();

            eeCV.Structure.Returns(esCV);

            var eCV = Substitute.For <IEntityData>();

            eCV.Name.Returns("CVb");
            eCV.GetCurrent().Returns(eeCV);

            var sCV = Substitute.For <IStructureData>();

            sCV.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            sCV.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10, Container = CVboxA, E = eCV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20, Container = CVboxB, E = eCV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });
            sCV.ContainerSource.Returns(new System.Collections.Concurrent.ConcurrentDictionary <string, IContainerSource>()
            {
                ["BoxA"] = new ContainerSource()
                {
                    Container = CVboxA, CustomName = "BoxA"
                },
                ["BoxB"] = new ContainerSource()
                {
                    Container = CVboxB, CustomName = "BoxB"
                },
            });
            sCV.GetCurrent().Returns(esCV);
            sCV.E.Returns(eCV);
            eCV.S.Returns(sCV);

            sCV.DockedE.Returns(new[] { eSV });

            // MOVE ==================================================================

            var lcdData = Substitute.For <IScriptRootData>();

            lcdData.DeviceLockAllowed.Returns(true);
            lcdData.CsRoot.Returns(new CsHelper.CsScriptFunctions(lcdData));
            lcdData.Console.Returns(new CsHelper.ConsoleMock(lcdData));

            lcdData.E.Returns(eCV);
            lcdData.E.S.Returns(sCV);
            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var s  = new StringBuilder();
            var pf = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual("SVa:1#9 SVa:BoxA->CVb:BoxB", lcdMod.ExecuteCsScript(pf, lcdData, @"
                E.S.DockedE.ForEach(e => { 
                    Console.Write(e.Name + "":"");
                    CsRoot.Items(e.S, ""BoxA"")
                        .ForEach(i => {
                            CsRoot.Move(i, E.S, ""Box*"").ForEach(mi => {
                                Console.Write($""{mi.Id}#{mi.Count} {mi.SourceE.Name}:{mi.Source}->{mi.DestinationE.Name}:{mi.Destination}"");
                            });
                        });
                });
            "));
        }
Ejemplo n.º 28
0
        public void TestMethodFillFuel()
        {
            ConveyorHelpers.CreateDeviceLock = (R, P, S, V) => new MockDeviceLock();

            var lcdMod = new EmpyrionScripting();

            lcdMod.CsCompiler = new CsCompiler.CsCompiler(".");
            lcdMod.CsCompiler.Configuration.Current.WithinLearnMode = true;
            var pf = new PlayfieldScriptData(lcdMod);

            var dict = new ConcurrentDictionary <string, object>();

            var data      = Substitute.For <IScriptRootData>();
            var playfield = Substitute.For <IPlayfield>();

            playfield.Name.Returns("PlayfieldTestName");
            data.CsRoot.Returns(new CsHelper.CsScriptFunctions(data));

            var e           = Substitute.For <IEntityData>();
            var s           = Substitute.For <IStructureData>();
            var egsFuelTank = Substitute.For <IStructureTank>();

            int fuelTankContent = 5000;

            egsFuelTank.Capacity.Returns(10000);
            egsFuelTank.Content.ReturnsForAnyArgs(ci => fuelTankContent);
            egsFuelTank.UsesIntegerAmounts.Returns(true);
            egsFuelTank.When(x => x.AddContent(4500)).Do(x => fuelTankContent += 4500);

            var fuelTank = new StructureTank(egsFuelTank, StructureTankType.Fuel);

            s.FuelTank.Returns(fuelTank);

            var c = Substitute.For <IContainer>();

            IItemsSource boxItemContent = new ItemsSource()
            {
                E          = e,
                Id         = 2373,
                Count      = 500,
                CustomName = "Box1",
                Container  = c,
            };
            var itemSource = new List <IItemsSource> {
                boxItemContent
            };

            c.RemoveItems(2373, 16).Returns(ci => { boxItemContent.Count -= 15; return(1); });

            var items = new[] { new ItemsData()
                                {
                                    Count  = 50,
                                    Id     = 2373,
                                    Key    = "Fuelcell",
                                    Name   = "Energie",
                                    Source = itemSource,
                                } };

            s.Items.Returns(items);

            e.S.Returns(s);
            data.E.Returns(e);
            data.P = new PlayfieldData(playfield);
            data.GetPersistendData().ReturnsForAnyArgs(dict);
            data.Data = dict;
            data.DeviceLockAllowed.Returns(true);

            data.CycleCounter.Returns(0);
            Assert.AreEqual("Id:2373 Count:15 Source:Box1", lcdMod.ExecuteCsScript(pf, data, @"
                var mi = CsRoot.Fill(E.S.Items.First(I => I.Id == 2373), E.S, StructureTankType.Fuel)[0];
                return $""Id:{mi.Id} Count:{mi.Count} Source:{mi.Source}"";
            "));

            Assert.AreEqual(9500, fuelTankContent, "FuelTankContext");
            Assert.AreEqual(485, boxItemContent.Count, "BoxItemContent");
        }
Ejemplo n.º 29
0
        public void TestMethodItemsMoveWithSameBox()
        {
            ConveyorHelpers.CreateDeviceLock = (R, P, S, V) => new MockDeviceLock();

            var e = Substitute.For <IEntityData>();

            e.Name.Returns("CVa");

            var boxA = Substitute.For <IContainer>();

            boxA.RemoveItems(1, int.MaxValue).Returns(10);
            boxA.AddItems(1, 1).Returns(0);
            boxA.AddItems(1, 10).Returns(C => throw new Exception("in the same box"));

            var boxB = Substitute.For <IContainer>();

            boxB.AddItems(1, 10).Returns(1);

            var es = Substitute.For <IStructure>();

            es.GetDevice <IContainer>("BoxA").Returns(boxA);
            es.GetDevice <IContainer>("BoxB").Returns(boxB);

            var s = Substitute.For <IStructureData>();

            s.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            s.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10, Container = boxA, E = e
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20, Container = boxB, E = e
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });
            s.ContainerSource.Returns(new System.Collections.Concurrent.ConcurrentDictionary <string, IContainerSource>()
            {
                ["BoxA"] = new ContainerSource()
                {
                    Container = boxA, CustomName = "BoxA"
                },
                ["BoxB"] = new ContainerSource()
                {
                    Container = boxB, CustomName = "BoxB"
                },
            });
            s.GetCurrent().Returns(es);
            s.E.Returns(e);

            var lcdData = Substitute.For <IScriptRootData>();

            lcdData.DeviceLockAllowed.Returns(true);

            lcdData.E.Returns(e);
            lcdData.E.S.Returns(s);
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual("1#9 CVa:BoxA->CVa:BoxB", lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{#items E.S 'BoxA'}}{{move this ../E.S 'Box*'}}{{Id}}#{{Count}} {{SourceE.Name}}:{{Source}}->{{DestinationE.Name}}:{{Destination}}{{/move}}{{/items}}"));
        }
Ejemplo n.º 30
0
        public void TestMethodItemsMoveToOtherEntity()
        {
            ConveyorHelpers.CreateDeviceLock = (R, P, S, V) => new MockDeviceLock();

            // SV ==================================================================
            var SVboxA = Substitute.For <IContainer>();

            SVboxA.RemoveItems(1, int.MaxValue).Returns(10);
            SVboxA.AddItems(1, 1).Returns(0);
            SVboxA.AddItems(1, 10).Returns(C => throw new Exception("in the same box"));

            var SVboxB = Substitute.For <IContainer>();

            SVboxB.AddItems(1, 10).Returns(1);

            var esSV = Substitute.For <IStructure>();

            esSV.GetDevice <IContainer>("BoxA").Returns(SVboxA);
            esSV.GetDevice <IContainer>("BoxB").Returns(SVboxB);

            var eeSV = Substitute.For <IEntity>();

            eeSV.Structure.Returns(esSV);

            var eSV = Substitute.For <IEntityData>();

            eSV.Name.Returns("SVa");
            eSV.GetCurrent().Returns(eeSV);

            var sSV = Substitute.For <IStructureData>();

            sSV.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            sSV.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10, Container = SVboxA, E = eSV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20, Container = SVboxB, E = eSV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });
            sSV.ContainerSource.Returns(new System.Collections.Concurrent.ConcurrentDictionary <string, IContainerSource>()
            {
                ["BoxA"] = new ContainerSource()
                {
                    Container = SVboxA, CustomName = "BoxA"
                },
                ["BoxB"] = new ContainerSource()
                {
                    Container = SVboxB, CustomName = "BoxB"
                },
            });
            sSV.GetCurrent().Returns(esSV);
            sSV.E.Returns(eSV);
            eSV.S.Returns(sSV);

            // CV ==================================================================
            var CVboxA = Substitute.For <IContainer>();

            CVboxA.RemoveItems(1, int.MaxValue).Returns(10);
            CVboxA.AddItems(1, 1).Returns(0);
            CVboxA.AddItems(1, 10).Returns(C => throw new Exception("in the same box"));

            var CVboxB = Substitute.For <IContainer>();

            CVboxB.AddItems(1, 10).Returns(1);

            var esCV = Substitute.For <IStructure>();

            esCV.GetDevice <IContainer>("BoxA").Returns(CVboxA);
            esCV.GetDevice <IContainer>("BoxB").Returns(CVboxB);

            var eeCV = Substitute.For <IEntity>();

            eeCV.Structure.Returns(esCV);

            var eCV = Substitute.For <IEntityData>();

            eCV.Name.Returns("CVb");
            eCV.GetCurrent().Returns(eeCV);

            var sCV = Substitute.For <IStructureData>();

            sCV.AllCustomDeviceNames.Returns(new[] { "BoxA", "BoxB", "BoxC", "BoxX" });
            sCV.Items.Returns(new[] {
                new ItemsData()
                {
                    Id = 1, Count = 11, Name = "a", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxA", Id = 1, Count = 10, Container = CVboxA, E = eCV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 2, Count = 20, Name = "b", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxB", Id = 2, Count = 20, Container = CVboxB, E = eCV
                                                                      } }.ToList()
                },
                new ItemsData()
                {
                    Id = 3, Count = 30, Name = "c", Source = new [] { (IItemsSource) new ItemsSource()
                                                                      {
                                                                          CustomName = "BoxC", Id = 3, Count = 30
                                                                      } }.ToList()
                },
            });
            sCV.ContainerSource.Returns(new System.Collections.Concurrent.ConcurrentDictionary <string, IContainerSource>()
            {
                ["BoxA"] = new ContainerSource()
                {
                    Container = CVboxA, CustomName = "BoxA"
                },
                ["BoxB"] = new ContainerSource()
                {
                    Container = CVboxB, CustomName = "BoxB"
                },
            });
            sCV.GetCurrent().Returns(esCV);
            sCV.E.Returns(eCV);
            eCV.S.Returns(sCV);

            sCV.DockedE.Returns(new[] { eSV });

            // MOVE ==================================================================

            var lcdData = Substitute.For <IScriptRootData>();

            lcdData.DeviceLockAllowed.Returns(true);

            lcdData.E.Returns(eCV);
            lcdData.E.S.Returns(sCV);
            var lcdMod = new EmpyrionScripting();
            var pf     = new PlayfieldScriptData(lcdMod);

            Assert.AreEqual("SVa:1#9 SVa:BoxA->CVb:BoxB", lcdMod.ExecuteHandlebarScript(pf, lcdData, "{{#each E.S.DockedE}}{{Name}}:{{#items S 'BoxA'}}{{move this @root/E.S 'Box*'}}{{Id}}#{{Count}} {{SourceE.Name}}:{{Source}}->{{DestinationE.Name}}:{{Destination}}{{/move}}{{/items}}{{/each}}"));
        }