private void ProcessSimulationStateMessage(Message message)
        {
            if (message is ITAlert.Photon.Messages.Simulation.States.InitializedMessage initializedMessage)
            {
                LogProxy.Info("InitializingState: Received InitializedMessage");

                if (string.IsNullOrEmpty(initializedMessage.PlayerConfiguration) ||
                    string.IsNullOrEmpty(initializedMessage.ScenarioName) ||
                    _scenarioLoader.TryGetScenario(initializedMessage.ScenarioName, out var scenario) == false)
                {
                    throw new InvalidOperationException("Received invalid InitializedMessage.");
                }

                try
                {
                    scenario.Configuration.PlayerConfiguration = ConfigurationSerializer.Deserialize <List <PlayerConfig> >(initializedMessage.PlayerConfiguration);
                    // TODO: extract simulation initialization to somewhere else
                    var simulationRoot = SimulationInstaller.CreateSimulationRoot(scenario);
                    if (_director.Initialize(simulationRoot, _photonClient.CurrentRoom.Player.PhotonId, _photonClient.CurrentRoom.Players))
                    {
                        _photonClient.CurrentRoom.Messenger.SendMessage(new InitializedMessage
                        {
                            PlayerPhotonId = _photonClient.CurrentRoom.Player.PhotonId
                        });
                    }
                }
                catch (Exception ex)
                {
                    //TODO: transition back to lobby, or show error message

                    throw new SimulationException("Error creating simulation root", ex);
                }
            }
            else
            {
                throw new Exception("Unhandled Simulation State Message: " + message);
            }
        }
Exemple #2
0
        public void TestMemoryResourceTransmission()
        {
            var nodeConfigs = new List <NodeConfig> {
                new NodeConfig()
                {
                    X         = 0,
                    Y         = 0,
                    Name      = "Node 0",
                    Archetype = SubsystemNode.Archetype,
                }
            };

            ConfigurationHelper.ProcessNodeConfigs(nodeConfigs);

            var systems = new List <SystemConfiguration>()
            {
                new SystemConfiguration <ItemStorageSystem>(),
                new SystemConfiguration <ResourcesSystem>()
                {
                    ExtensionConfiguration = new SystemExtensionConfiguration[]
                    {
                        new SystemExtensionConfiguration <ISubsystemResourceEffect>()
                        {
                            Implementations = new SystemExtensionImplementation[]
                            {
                                new SystemExtensionConfiguration <ISubsystemResourceEffect> .SystemExtensionImplementation <ResetMemoryEachTick>(),
                                new SystemExtensionConfiguration <ISubsystemResourceEffect> .SystemExtensionImplementation <ItemStorageConsumesMemoryEffect>(),
                            }
                        }
                    }
                },
            };

            var archetypes = new List <Archetype>()
            {
                SubsystemNode.Archetype,
                ScannerTool.Archetype,
            };

            var lifecycleConfig = new LifeCycleConfiguration();
            var configuration   = new SimulationConfiguration(nodeConfigs, null, null, archetypes, systems, lifecycleConfig);
            var scenarioA       = new SimulationScenario()
            {
                Configuration = configuration
            };
            var rootA = SimulationInstaller.CreateSimulationRoot(scenarioA);
            var ecsA  = rootA.ECS;

            MemoryResource memoryResourceA;

            TestEntityState(ecsA, SimulationConstants.SubsystemInitialMemory, out memoryResourceA);

            ecsA.Tick();

            var scenarioB = new SimulationScenario()
            {
                Configuration = configuration
            };
            var rootB = SimulationInstaller.CreateSimulationRoot(scenarioB);
            var ecsB  = rootB.ECS;

            MemoryResource memoryResourceB;

            TestEntityState(ecsB, SimulationConstants.SubsystemInitialMemory, out memoryResourceB);

            Assert.That(memoryResourceB, Is.Not.EqualTo(memoryResourceA));

            var json = rootA.GetEntityState();

            rootB.UpdateEntityState(json);

            MemoryResource memoryResourceC;

            TestEntityState(ecsB, SimulationConstants.ItemMemoryConsumption, out memoryResourceC);

            Assert.That(memoryResourceC, Is.EqualTo(memoryResourceB), "Memory Resource was recreated");
        }
Exemple #3
0
        public void TestItemOwnerTransmission()
        {
            var testSystem = new Archetype("TestSystem")
                             .HasComponent(new ComponentBinding <Subsystem>())
                             .HasComponent(new ComponentBinding <Coordinate2DProperty>())
                             .HasComponent(new ComponentBinding <Name>())
                             .HasComponent(new ComponentBinding <ItemStorage>()
            {
                ComponentTemplate = new ItemStorage()
                {
                    Items = new ItemContainer[1],
                }
            });

            var testItem = new Archetype("TestItem")
                           .HasComponent(new ComponentBinding <Scanner>())
                           .HasComponent(new ComponentBinding <Owner>());

            var nodeConfigs = new List <NodeConfig> {
                new NodeConfig()
                {
                    X         = 0,
                    Y         = 0,
                    Name      = "Node 0",
                    Archetype = testSystem.Name
                },
            };

            ConfigurationHelper.ProcessNodeConfigs(nodeConfigs);

            var systems = new List <SystemConfiguration>()
            {
                new SystemConfiguration <ItemStorageSystem>(),
            };

            var archetypes = new List <Archetype>()
            {
                testSystem,
                testItem,
            };

            var lifecycleConfig = new LifeCycleConfiguration();
            var configuration   = new SimulationConfiguration(nodeConfigs, null, null, archetypes, systems, lifecycleConfig);
            var scenario        = new SimulationScenario()
            {
                Configuration = configuration
            };

            var rootA = SimulationInstaller.CreateSimulationRoot(scenario);
            var ecsA  = rootA.ECS;

            Owner ownerA;

            TestEntityState(ecsA, null, out ownerA);

            ecsA.Tick();

            var rootB = SimulationInstaller.CreateSimulationRoot(scenario);
            var ecsB  = rootB.ECS;

            Owner ownerB;

            TestEntityState(ecsB, null, out ownerB);

            Assert.That(ownerB, Is.Not.EqualTo(ownerA));

            const int owner = 3;

            ownerA.Value = 3;

            var json = rootA.GetEntityState();

            rootB.UpdateEntityState(json);

            Owner ownerC;

            TestEntityState(ecsB, owner, out ownerC);

            Assert.That(ownerC, Is.EqualTo(ownerB), "Owner was recreated");

            ownerA.Value = null;
            json         = rootA.GetEntityState();
            rootB.UpdateEntityState(json);

            Owner ownerD;

            TestEntityState(ecsB, null, out ownerD);

            Assert.That(ownerD, Is.EqualTo(ownerB), "Owner was recreated");
        }