コード例 #1
0
        public async System.Threading.Tasks.Task GetDepositDataTestAsync()
        {
            var sectorGeneratorMock = new Mock <ISectorGenerator>();

            sectorGeneratorMock.Setup(x => x.GenerateAsync(It.IsAny <ISectorNode>()))
            .Returns <ISectorNode>(scheme =>
            {
                return(Task.Run(() =>
                {
                    var sectorMock = new Mock <ISector>();
                    var sector = sectorMock.Object;
                    return sector;
                }));
            });

            var sectorGenerator = sectorGeneratorMock.Object;
            var locationSchemes = CreateBiomSchemes();

            var schemeRoundCounter = 0;
            var rollerMock         = new Mock <IBiomeSchemeRoller>();

            rollerMock.Setup(x => x.Roll()).Returns(() =>
            {
                schemeRoundCounter++;
                if (schemeRoundCounter >= locationSchemes.Length)
                {
                    schemeRoundCounter = 0;
                }

                var scheme = locationSchemes[schemeRoundCounter];

                return(scheme);
            });
            var roller = rollerMock.Object;

            var introScheme = locationSchemes.Single(x => x.Sid == "intro");

            var biomService = new BiomeInitializer(sectorGenerator, roller);

            var dice = new LinearDice();
            var resourceMaterializationMap = new ResourceMaterializationMap(dice);

            var resultStringBuilder = new StringBuilder();

            resultStringBuilder.AppendLine("graph TD;");

            // ACT

            var biom = await biomService.InitBiomeAsync(introScheme).ConfigureAwait(false);

            var         introNode       = biom.Sectors.Single(x => x.State == SectorNodeState.SectorMaterialized);
            var         currentResource = resourceMaterializationMap.GetDepositData(introNode);
            ISectorNode currentNode     = introNode;

            var       iteration     = 0;
            const int ITERATION_MAX = 100;

            var openList   = new List <NodeInfo>();
            var nextNodes1 = currentNode.Biome.GetNext(currentNode)
                             .OfType <SectorNode>()
                             .Where(x => x.State != SectorNodeState.SectorMaterialized)
                             .Select(x => new NodeInfo {
                Current = x, Parent = introNode, ParentResource = currentResource
            });

            openList.AddRange(nextNodes1);

            while (iteration < ITERATION_MAX)
            {
                var nextNode = openList[0];
                openList.RemoveAt(0);

                await biomService.MaterializeLevelAsync(nextNode.Current).ConfigureAwait(false);

                var nextResource = resourceMaterializationMap.GetDepositData(nextNode.Current);

                resultStringBuilder.AppendLine(GetVisualString(nextNode.Parent, nextNode.Current, nextNode.ParentResource, nextResource));

                var nextNodes2 = nextNode.Current.Biome.GetNext(nextNode.Current)
                                 .OfType <SectorNode>()
                                 .Where(x => x.State != SectorNodeState.SectorMaterialized)
                                 .Select(x => new NodeInfo {
                    Current = x, Parent = nextNode.Current, ParentResource = nextResource
                });
                openList.AddRange(nextNodes2);

                iteration++;
            }

            Console.Write(resultStringBuilder.ToString());
        }
コード例 #2
0
        public async Task BuildLinearBiomGraph()
        {
            // ARRANGE

            var sectorGeneratorMock = new Mock <ISectorGenerator>();

            sectorGeneratorMock.Setup(x => x.GenerateAsync(It.IsAny <ISectorNode>()))
            .Returns <ISectorNode>(scheme =>
            {
                return(Task.Run(() =>
                {
                    var sectorMock = new Mock <ISector>();
                    var sector = sectorMock.Object;
                    return sector;
                }));
            });

            var sectorGenerator = sectorGeneratorMock.Object;
            var locationSchemes = CreateBiomSchemes();

            var schemeRoundCounter = 0;
            var rollerMock         = new Mock <IBiomeSchemeRoller>();

            rollerMock.Setup(x => x.Roll()).Returns(() =>
            {
                schemeRoundCounter++;
                if (schemeRoundCounter >= locationSchemes.Length)
                {
                    schemeRoundCounter = 0;
                }

                var scheme = locationSchemes[schemeRoundCounter];

                return(scheme);
            });
            var roller = rollerMock.Object;

            var introScheme = locationSchemes.Single(x => x.Sid == "intro");

            var biomService = new BiomeInitializer(sectorGenerator, roller);

            // ACT

            var biom = await biomService.InitBiomeAsync(introScheme).ConfigureAwait(false);

            var introNode   = biom.Sectors.Single(x => x.State == SectorNodeState.SectorMaterialized);
            var currentNode = introNode;

            var       iteration     = 0;
            const int ITERATION_MAX = 100;

            while (iteration < ITERATION_MAX)
            {
                var nextNode = currentNode.Biome.GetNext(currentNode)
                               .OfType <SectorNode>()
                               .First(x => x.State != SectorNodeState.SectorMaterialized);

                await biomService.MaterializeLevelAsync(nextNode).ConfigureAwait(false);

                currentNode = nextNode;

                iteration++;
            }

            // ASSERT
            var materializedSectorNodes = new List <SectorNode>();
            var scanNode = introNode;

            materializedSectorNodes.Add(scanNode);
            while (true)
            {
                var nextNodes = scanNode.Biome.GetNext(scanNode).OfType <SectorNode>();
                scanNode = nextNodes.SingleOrDefault(x => x.State == SectorNodeState.SectorMaterialized &&
                                                     !materializedSectorNodes.Contains(x));

                if (scanNode is null)
                {
                    break;
                }

                materializedSectorNodes.Add(scanNode);
            }

            var expectedMaterializedSectors = ITERATION_MAX + 1; // +1 потому что интровый не учитывается при итерациях.

            materializedSectorNodes.Should().HaveCount(expectedMaterializedSectors);
        }