Exemple #1
0
        public static IAppBuilder UseRequestTracer(this IAppBuilder builder)
        {
            var         builderProperties = new BuilderProperties(builder.Properties);
            TraceSource traceSource       = builderProperties.Get <TraceSource>("host.TraceSource");

            return(builder.UseType <RequestTracer>(traceSource));
        }
Exemple #2
0
        static bool CheckRequiredStartupData(IDictionary <string, object> properties, IList <string> warnings)
        {
            var builderProperties = new BuilderProperties(properties);

            if (builderProperties.Version == null)
            {
                warnings.Add("builder.Properties should contain \"owin.Version\"");
            }
            else if (builderProperties.Version != "1.0")
            {
                warnings.Add("builder.Properties[\"owin.Version\"] should be \"1.0\"");
            }

            if (builderProperties.TraceOutput == null)
            {
                warnings.Add("builder.Properties[\"host.TraceOutput\"] should be a TextWriter");
            }

            // TODO: is this still true?
            //if (!properties.ContainsKey("server.Name"))
            //{
            //    warnings.Add("builder.Properties should contain \"server.Name\"");
            //}
            //else if (properties.Get<string>("server.Name") == null)
            //{
            //    warnings.Add("builder.Properties[\"server.Name\"] should be a string");
            //}

            return(true);
        }
Exemple #3
0
        private string GetBoardBuildingInfo(BuilderProperties props, int shipSize)
        {
            string boardBuildingInfo = "";

            boardBuildingInfo += DataUtils.Default + $"   Current ship: {props.CurrentShipName} \n";
            boardBuildingInfo += $"   {Separator} \n";
            boardBuildingInfo += "   Press R to rotate your ship \n";
            boardBuildingInfo += $"   {Separator}" + Color.Reset + "\n";
            return(boardBuildingInfo);
        }
Exemple #4
0
        public void RenderBoard(BuilderProperties props, int row, int col)
        {
            string outputString = "";

            outputString += GetLevelDescription(props.Mode, props.PlayerName);
            outputString += GetFieldCoordinatesTemplate(props.Mode);
            outputString += DrawDefaultField(props, row, col);
            outputString += GetFieldFooter(props.Mode);
            outputString += GetBoardBuildingInfo(props, props.CursorLength);
            outputString += GetBattleMenu(props.MenuOptions, row, props.SelectableRowCount);

            RenderScreen(outputString);
        }
Exemple #5
0
        public void EventListener(BuilderProperties props, int row, int col)
        {
            bool shipPlaced = false;

            while (!shipPlaced)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                switch (key.Key)
                {
                case ConsoleKey.RightArrow when col + 1 < props.PlayerField.GetLength(1):
                    props.Renderer.RenderBoard(props, row, ++col);
                    break;

                case ConsoleKey.LeftArrow when col - 1 >= 0:
                    props.Renderer.RenderBoard(props, row, --col);
                    break;

                case ConsoleKey.UpArrow when row - 1 >= 0:
                    props.Renderer.RenderBoard(props, --row, col);
                    break;

                case ConsoleKey.DownArrow when row + 1 < props.SelectableRowCount + props.MenuOptions.Count:
                    props.Renderer.RenderBoard(props, ++row, col);
                    break;

                case ConsoleKey.Enter when row >= props.SelectableRowCount:
                    MenuEnterEvent(props.SelectableRowCount, row);
                    props.Renderer.RenderBoard(props, row, col);
                    break;

                case ConsoleKey.R:
                    props.Direction = props.Direction == 0 ? 1 : 0;
                    props.Renderer.RenderBoard(props, row, col);
                    break;

                case ConsoleKey.Enter:
                    if (props.Builder.ShipPlacement(props, row, col))
                    {
                        shipPlaced = true;
                    }
                    break;

                case ConsoleKey.M:
                    row = props.SelectableRowCount;
                    props.Renderer.RenderBoard(props, row, 0);
                    break;
                }
            }
        }
        public static IAppBuilder UsePassiveValidator(this IAppBuilder builder)
        {
            IList<string> warnings = new List<string>();
            if (!PassiveValidator.TryValidateProperties(builder.Properties, warnings))
            {
                throw new InvalidOperationException(warnings.Aggregate("builder.Properties are invalid", (a, b) => a + "\r\n" + b));
            }

            if (warnings.Count != 0)
            {
                var builderProperties = new BuilderProperties(builder.Properties);
                var output = builderProperties.TraceOutput ?? Console.Out;
                output.WriteLine(warnings.Aggregate("builder.Properties are invalid", (a, b) => a + "\r\n" + b));
            }

            return builder.UseType<PassiveValidator>();
        }
Exemple #7
0
 // Go through all ships from the settings and arrange them
 public void Start(Settings settings, BuilderProperties props)
 {
     props.PlayerFlotilla = new Flotilla();
     for (int i = 0; i < settings.ShipCount.Length; i++)            // ship count
     {
         for (int j = 0; j < settings.ShipCount[i]; j++)            // ship size
         {
             props.CurrentShipName = props.ShipNames[i];            // ship name
             props.CursorLength    = settings.ShipSettings[i];      // set ship size
             props.Direction       = _lastDirection;                // set direction
             props.Renderer.RenderBoard(props, _lastRow, _lastCol); // render
             props.EventListener.EventListener(props, _lastRow, _lastCol);
         }
     }
     _manager.SetPlayerField(props.PlayerField);       // set field
     _manager.SetPlayerFlotilla(props.PlayerFlotilla); // set ship
 }
Exemple #8
0
        public static IAppBuilder UsePassiveValidator(this IAppBuilder builder)
        {
            IList <string> warnings = new List <string>();

            if (!PassiveValidator.TryValidateProperties(builder.Properties, warnings))
            {
                throw new InvalidOperationException(warnings.Aggregate("builder.Properties are invalid", (a, b) => a + "\r\n" + b));
            }

            if (warnings.Count != 0)
            {
                var builderProperties = new BuilderProperties(builder.Properties);
                var output            = builderProperties.TraceOutput ?? Console.Out;
                output.WriteLine(warnings.Aggregate("builder.Properties are invalid", (a, b) => a + "\r\n" + b));
            }

            return(builder.UseType <PassiveValidator>());
        }
Exemple #9
0
        private void LoadBoardBuilder(Settings settings, GameProperties gameProps, string playerName)
        {
            BuilderProperties builderProps = new BuilderProperties {
                Mode           = "Board Building",
                PlayerFlotilla = playerName == gameProps.Player1Name ? gameProps.Player1Flotilla : gameProps.Player2Flotilla,
                PlayerField    = new string[settings.BattlefieldSize[0], settings.BattlefieldSize[1]],
                ShipNames      = settings.ShipNames,
                MenuOptions    = new List <string> {
                    "Main Menu", "Quit"
                },
                PlayerName         = playerName,
                ShipArrangement    = settings.ShipArrangement,
                SelectableRowCount = settings.BattlefieldSize[0],
                Builder            = new BoardBuilder(this),
                Renderer           = new BuilderRenderer(settings.BattlefieldSize[1]),
                EventListener      = new BuilderEventListener()
            };

            builderProps.Builder.Start(settings, builderProps);
        }
Exemple #10
0
        private string DrawDefaultField(BuilderProperties props, int row, int col)
        {
            string fieldString = "";

            string[,] fieldTemplate = GetFieldTemplate(props, row, col, props.CursorLength, props.Direction);
            for (int i = 0; i < props.PlayerField.GetLength(0); i++)
            {
                fieldString += DataUtils.Default + (i < 9 ? $" {i + 1} " : $"{i + 1} ") + Color.Reset;
                for (int j = 0; j < props.PlayerField.GetLength(1); j++)
                {
                    switch (fieldTemplate[i, j])
                    {
                    case null:
                        fieldString += DataUtils.FieldCell;
                        break;

                    case "st true":
                        fieldString += DataUtils.ValidTemplateCell;
                        break;

                    case "st false":
                        fieldString += DataUtils.InvalidTemplateCell;
                        break;

                    case "border":
                        fieldString += DataUtils.BorderCell;
                        break;

                    case "ship":
                        fieldString += DataUtils.ShipCell;
                        break;
                    }
                }
                fieldString += "\n";
            }

            TemporaryField = fieldTemplate;
            return(fieldString);
        }
Exemple #11
0
        private string[,] GetFieldTemplate(BuilderProperties props, int row, int col, int cursorLength, int direction)
        {
            string[,] currentField = new string[props.PlayerField.GetLength(0), props.PlayerField.GetLength(1)];
            Array.Copy(props.PlayerField, currentField, props.PlayerField.Length);
            int height = props.PlayerField.GetLength(0);
            int weight = props.PlayerField.GetLength(1);

            if (row >= height)
            {
                return(currentField);
            }

            string        shipTemplateName;
            List <string> availableDir = FieldManager.GetAvailableDirections(props.ShipArrangement, currentField, row, col, cursorLength);

            if (direction == 0 && availableDir.Contains("down") || direction == 1 && availableDir.Contains("right"))
            {
                shipTemplateName = "st true";
            }
            else
            {
                shipTemplateName = "st false";
            }

            for (int a = 0; a < cursorLength; a++)
            {
                if (direction == 0 && col + a < weight && row < height)
                {
                    currentField[row, col + a] = shipTemplateName;
                }
                else if (direction == 1 && row + a < height && col < weight)
                {
                    currentField[row + a, col] = shipTemplateName;
                }
            }

            return(currentField);
        }
        private void UpdateLeader()
        {
            if (BuilderProperties == null)
            {
                GroupLeaderView = null;
            }

            GroupLeaderView =
                (ILeaderBuilderPropertyView)BuilderPropertiesItemsControl.BuilderPropertyViews.Value.FirstOrDefault(
                    x =>
                    x.Value.PropertyPosition.IsGroupLeader &&
                    x.Value.PropertyPosition.BuilderCategory == BuilderCategory &&
                    x.Value.PropertyPosition.BuilderGroup == BuilderGroup).Value;

            var builderViewModel =
                BuilderProperties?.FirstOrDefault(x => x.BuilderProperty.GetType() == GroupLeaderView?.BuilderProperty);

            if (builderViewModel != null)
            {
                GroupLeaderProperty = builderViewModel.BuilderProperty;
                builderViewModel.BuilderPropertyView = GroupLeaderView;
            }
        }
Exemple #13
0
        // Does ship been placed or not
        public bool ShipPlacement(BuilderProperties props, int row, int col)
        {
            List <string> availableDirections = FieldManager.GetAvailableDirections(props.ShipArrangement,
                                                                                    props.Renderer.TemporaryField, row, col, props.CursorLength);
            string directionString = props.Direction == 0 ? "down" : "right";

            if (!availableDirections.Contains(directionString))
            {
                return(false);
            }

            Ship ship;

            // Put ship if you can
            (props.PlayerField, ship) = FieldManager.PutShip(props.ShipArrangement, props.PlayerField,
                                                             directionString, row, col, props.CursorLength);
            ship.Name = props.CurrentShipName;
            props.PlayerFlotilla.AddShip(ship);
            // Remember last coordinates
            _lastRow       = row;
            _lastCol       = col;
            _lastDirection = props.Direction;
            return(true);
        }
        static bool CheckRequiredStartupData(IDictionary<string, object> properties, IList<string> warnings)
        {
            var builderProperties = new BuilderProperties(properties);
            if (builderProperties.Version == null)
            {
                warnings.Add("builder.Properties should contain \"owin.Version\"");
            }
            else if (builderProperties.Version != "1.0")
            {
                warnings.Add("builder.Properties[\"owin.Version\"] should be \"1.0\"");
            }

            if (builderProperties.TraceOutput == null)
            {
                warnings.Add("builder.Properties[\"host.TraceOutput\"] should be a TextWriter");
            }

            // TODO: is this still true?
            //if (!properties.ContainsKey("server.Name"))
            //{
            //    warnings.Add("builder.Properties should contain \"server.Name\"");
            //}
            //else if (properties.Get<string>("server.Name") == null)
            //{
            //    warnings.Add("builder.Properties[\"server.Name\"] should be a string");
            //}

            return true;
        }
 public static IAppBuilder UseRequestTracer(this IAppBuilder builder)
 {
     var builderProperties = new BuilderProperties(builder.Properties);
     TraceSource traceSource = builderProperties.Get<TraceSource>("host.TraceSource");
     return builder.UseType<RequestTracer>(traceSource);
 }