Beispiel #1
0
 public static IValueNode CreateDefaultValue(
     ITypeCompletionContext context,
     ArgumentDefinition argumentDefinition,
     IInputType argumentType,
     FieldCoordinate argumentCoordinate)
 {
     try
     {
         return(argumentDefinition.NativeDefaultValue != null
             ? argumentType.ParseValue(argumentDefinition.NativeDefaultValue)
             : argumentDefinition.DefaultValue);
     }
     catch (Exception ex)
     {
         context.ReportError(SchemaErrorBuilder.New()
                             .SetMessage(
                                 TypeResources.FieldInitHelper_InvalidDefaultValue,
                                 argumentCoordinate)
                             .SetCode(ErrorCodes.Schema.MissingType)
                             .SetTypeSystemObject(context.Type)
                             .AddSyntaxNode(argumentDefinition.SyntaxNode)
                             .SetException(ex)
                             .Build());
         return(NullValueNode.Default);
     }
 }
Beispiel #2
0
 internal static IValueNode?CompleteDefaultValue(
     ITypeCompletionContext context,
     ArgumentDefinition argumentDefinition,
     IInputType argumentType,
     FieldCoordinate argumentCoordinate)
 {
     try
     {
         return(argumentDefinition.RuntimeDefaultValue != null
             ? context.DescriptorContext.InputFormatter.FormatValue(
                    argumentDefinition.RuntimeDefaultValue,
                    argumentType,
                    Path.Root)
             : argumentDefinition.DefaultValue);
     }
     catch (Exception ex)
     {
         context.ReportError(SchemaErrorBuilder.New()
                             .SetMessage(
                                 TypeResources.FieldInitHelper_InvalidDefaultValue,
                                 argumentCoordinate)
                             .SetCode(ErrorCodes.Schema.MissingType)
                             .SetTypeSystemObject(context.Type)
                             .AddSyntaxNode(argumentDefinition.SyntaxNode)
                             .SetException(ex)
                             .Build());
         return(NullValueNode.Default);
     }
 }
Beispiel #3
0
 public InterfaceField(
     InterfaceFieldDefinition definition,
     FieldCoordinate fieldCoordinate,
     bool sortArgumentsByName = false)
     : base(definition, fieldCoordinate, sortArgumentsByName)
 {
 }
    public static ISchemaBuilder AddResolver(
        this ISchemaBuilder builder,
        FieldCoordinate field,
        FieldResolverDelegate resolver,
        Type?resultType = null)
    {
        if (builder is null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (!field.HasValue)
        {
            throw new ArgumentException(
                      TypeResources.SchemaBuilderExtensions_AddResolver_EmptyCooridnates,
                      nameof(builder));
        }

        if (resolver is null)
        {
            throw new ArgumentNullException(nameof(resolver));
        }

        return(AddResolverConfigInternal(builder, field, resolver, resultType));
    }
Beispiel #5
0
 private static bool IsValidWallPosition(FieldCoordinate coord)
 {
     return(coord.XCoord >= XField.A &&
            coord.XCoord <= XField.H &&
            coord.YCoord >= YField.Nine &&
            coord.YCoord <= YField.Two);
 }
Beispiel #6
0
 private static bool IsValidFigurePosition(FieldCoordinate coord)
 {
     return(coord.XCoord >= XField.A &&
            coord.XCoord <= XField.I &&
            coord.YCoord >= YField.Nine &&
            coord.YCoord <= YField.One);
 }
Beispiel #7
0
 public static ISchemaError MiddlewareOrderInvalid(
     FieldCoordinate field,
     ITypeSystemObject type,
     ISyntaxNode?syntaxNode,
     string currentOrder)
 => SchemaErrorBuilder.New()
 .SetMessage(TypeResources.ErrorHelper_MiddlewareOrderInvalid, field, currentOrder)
 .SetCode(ErrorCodes.Schema.MiddlewareOrderInvalid)
 .SetTypeSystemObject(type)
 .AddSyntaxNode(syntaxNode)
 .SetExtension(nameof(field), field)
 .Build();
Beispiel #8
0
        protected FieldBase(TDefinition definition, FieldCoordinate fieldCoordinate)
        {
            _definition = definition ?? throw new ArgumentNullException(nameof(definition));
            _syntaxNode = definition.SyntaxNode;

            Name        = definition.Name.EnsureNotEmpty(nameof(definition.Name));
            Description = definition.Description;
            Coordinate  = fieldCoordinate.HasValue
                ? fieldCoordinate
                : FieldCoordinate.CreateWithoutType(definition.Name);

            DeclaringType = default !;
Beispiel #9
0
        private static Direction GetDirection(FieldCoordinate from, FieldCoordinate to)
        {
            if (from.XCoord == to.XCoord)
            {
                return(from.YCoord > to.YCoord
                                        ? Direction.Top
                                        : Direction.Bottom);
            }

            return(from.XCoord < to.XCoord
                                ? Direction.Right
                                : Direction.Left);
        }
Beispiel #10
0
 internal ObjectField(
     ObjectFieldDefinition definition,
     FieldCoordinate fieldCoordinate,
     bool sortArgumentsByName = false)
     : base(definition, fieldCoordinate, sortArgumentsByName)
 {
     Member               = definition.Member;
     Middleware           = _empty;
     Resolver             = definition.Resolver !;
     Expression           = definition.Expression;
     SubscribeResolver    = definition.SubscribeResolver;
     IsIntrospectionField = definition.IsIntrospectionField;
 }
Beispiel #11
0
        public static Move GetMove(string s)
        {
            var moveString = s.Trim();

            switch (moveString.Length)
            {
            case 2:
            {
                var xCoord = GetXCoord(moveString[0]);
                var yCoord = GetYCoord(moveString[1]);

                if (xCoord.HasValue && yCoord.HasValue)
                {
                    var figurePosition = new FieldCoordinate(xCoord.Value, yCoord.Value);
                    if (IsValidFigurePosition(figurePosition))
                    {
                        return(new FigureMove(figurePosition));
                    }
                }

                break;
            }

            case 3:
            {
                if (moveString.ToLower() == "cap")
                {
                    return(new Capitulation());
                }

                var xCoord      = GetXCoord(moveString[0]);
                var yCoord      = GetYCoord(moveString[1]);
                var orientation = GetOrientation(moveString[2]);

                if (xCoord.HasValue && yCoord.HasValue && orientation.HasValue)
                {
                    var wallPosition = new FieldCoordinate(xCoord.Value, yCoord.Value);

                    if (IsValidWallPosition(wallPosition) &&
                        IsValidWallOrientation(orientation.Value))
                    {
                        return(new WallMove(new Wall(wallPosition, orientation.Value)));
                    }
                }

                break;
            }
            }

            return(null);
        }
Beispiel #12
0
 internal OutputFieldBase(
     TDefinition definition,
     FieldCoordinate fieldCoordinate,
     bool sortArgumentsByName)
     : base(definition, fieldCoordinate)
 {
     SyntaxNode = definition.SyntaxNode;
     Arguments  = new FieldCollection <Argument>(
         definition.Arguments.Select(
             t => new Argument(t, fieldCoordinate.With(argumentName: t.Name))),
         sortArgumentsByName);
     IsDeprecated = !string.IsNullOrEmpty(
         definition.DeprecationReason);
     DeprecationReason = definition.DeprecationReason;
 }
Beispiel #13
0
        public static IDictionary <FieldCoordinate, Node> GetAllNodes()
        {
            var result = new Dictionary <FieldCoordinate, Node>(81);

            for (var x = XField.A; x <= XField.I; x++)
            {
                for (var y = YField.One; y >= YField.Nine; y--)
                {
                    var coord = new FieldCoordinate(x, y);
                    result.Add(coord, new Node(coord));
                }
            }

            return(result);
        }
        public FieldResolverConfig(
            FieldCoordinate field,
            FieldResolverDelegate?resolver = null,
            PureFieldDelegate?pureResolver = null,
            Type?resultType = null)
        {
            if (resolver is null && pureResolver is null)
            {
                throw new ArgumentNullException(nameof(resolver));
            }

            Field        = field;
            PureResolver = pureResolver;
            Resolver     = resolver ?? new FieldResolverDelegate(ctx => new(pureResolver !(ctx)));
            ResultType   = resultType ?? typeof(object);
            _isEmpty     = false;
        }
Beispiel #15
0
        public Graph(BoardState boardState = null)
        {
            Nodes = GraphConstruction.GetAllNodes();
            GraphConstruction.AddAllEdges(Nodes);

            if (boardState != null)
            {
                foreach (var wall in boardState.PlacedWalls)
                {
                    ApplyWall(wall);
                }

                bottomPlayerPosition = boardState.BottomPlayer.Position;
                topPlayerPosition    = boardState.TopPlayer.Position;

                AddSpecialEdges();
            }
        }
Beispiel #16
0
        public InputField(InputFieldDefinition definition, FieldCoordinate fieldCoordinate)
            : base(definition, fieldCoordinate)
        {
            SyntaxNode   = definition.SyntaxNode;
            DefaultValue = definition.DefaultValue;
            Property     = definition.Property;

            IReadOnlyList <IInputValueFormatter> formatters = definition.GetFormatters();

            Formatter = formatters.Count switch
            {
                0 => null,
                1 => formatters[0],
                _ => new AggregateInputValueFormatter(formatters)
            };

            Type?propertyType = definition.Property?.PropertyType;

            if (propertyType is { IsGenericType : true } &&
Beispiel #17
0
//		First version
//
//		private static IList<Wall> GeneratePhysicalPossibleWalls (BoardState boardState)
//		{
//			var resultList = new List<Wall>();
//
//			for (var xCoord = XField.A; xCoord < XField.I; xCoord++)
//			{
//				for (var yCoord = YField.Nine; yCoord < YField.One; yCoord++)
//				{
//					var coord = new FieldCoordinate(xCoord, yCoord);
//
//					var placedWalls = boardState.PlacedWalls;
//
//					if (placedWalls.Any(wall => wall.TopLeft == coord))
//						continue;
//
//					if (!placedWalls.Any(wall => wall.Orientation == WallOrientation.Horizontal &&
//					                             (wall.TopLeft == new FieldCoordinate(coord.XCoord - 1, coord.YCoord) ||
//					                              wall.TopLeft == new FieldCoordinate(coord.XCoord + 1, coord.YCoord))))
//					{
//						resultList.Add(new Wall(coord, WallOrientation.Horizontal));
//					}
//
//					if (!placedWalls.Any(wall => wall.Orientation == WallOrientation.Vertical &&
//												 (wall.TopLeft == new FieldCoordinate(coord.XCoord, coord.YCoord + 1) ||
//												  wall.TopLeft == new FieldCoordinate(coord.XCoord, coord.YCoord - 1))))
//					{
//						resultList.Add(new Wall(coord, WallOrientation.Vertical));
//					}
//				}
//			}
//
//			return resultList;
//		}

        private static IEnumerable <Wall> GeneratePhysicalPossibleWalls(BoardState boardState)
        {
            var allHorizontalWalls = new Dictionary <FieldCoordinate, Wall>();
            var allVerticalWalls   = new Dictionary <FieldCoordinate, Wall>();

            for (var xCoord = XField.A; xCoord < XField.I; xCoord++)
            {
                for (var yCoord = YField.Nine; yCoord < YField.One; yCoord++)
                {
                    var coord = new FieldCoordinate(xCoord, yCoord);

                    allHorizontalWalls.Add(coord, new Wall(coord, WallOrientation.Horizontal));
                    allVerticalWalls.Add(coord, new Wall(coord, WallOrientation.Vertical));
                }
            }

            var horizontalWalls = boardState.PlacedWalls.Where(wall => wall.Orientation == WallOrientation.Horizontal);
            var verticalWalls   = boardState.PlacedWalls.Where(wall => wall.Orientation == WallOrientation.Vertical);

            foreach (var verticalWall in verticalWalls)
            {
                var topLeft = verticalWall.TopLeft;

                allHorizontalWalls.Remove(topLeft);
                allVerticalWalls.Remove(topLeft);
                allVerticalWalls.Remove(new FieldCoordinate(topLeft.XCoord, topLeft.YCoord + 1));
                allVerticalWalls.Remove(new FieldCoordinate(topLeft.XCoord, topLeft.YCoord - 1));
            }

            foreach (var horizontalWall in horizontalWalls)
            {
                var topLeft = horizontalWall.TopLeft;

                allVerticalWalls.Remove(topLeft);
                allHorizontalWalls.Remove(topLeft);
                allHorizontalWalls.Remove(new FieldCoordinate(topLeft.XCoord - 1, topLeft.YCoord));
                allHorizontalWalls.Remove(new FieldCoordinate(topLeft.XCoord + 1, topLeft.YCoord));
            }

            return(allHorizontalWalls.Values.Concat(allVerticalWalls.Values));
        }
Beispiel #18
0
        public Argument(
            ArgumentDefinition definition,
            FieldCoordinate fieldCoordinate)
            : base(definition, fieldCoordinate)
        {
            SyntaxNode   = definition.SyntaxNode;
            DefaultValue = definition.DefaultValue;

            if (definition.Formatters.Count == 0)
            {
                Formatter = null;
            }
            else if (definition.Formatters.Count == 1)
            {
                Formatter = definition.Formatters[0];
            }
            else
            {
                Formatter = new AggregateInputValueFormatter(definition.Formatters);
            }
        }
Beispiel #19
0
        public InputField(InputFieldDefinition definition, FieldCoordinate fieldCoordinate)
            : base(definition, fieldCoordinate)
        {
            SyntaxNode   = definition.SyntaxNode;
            DefaultValue = definition.DefaultValue;
            Property     = definition.Property;

            if (definition.Formatters.Count == 0)
            {
                Formatter = null;
            }
            else if (definition.Formatters.Count == 1)
            {
                Formatter = definition.Formatters[0];
            }
            else
            {
                Formatter = new AggregateInputValueFormatter(definition.Formatters);
            }

            Type?propertyType = definition.Property?.PropertyType;

            if (propertyType is { IsGenericType : true } &&
        public override void OnBeforeCompleteType(
            ITypeCompletionContext completionContext,
            DefinitionBase?definition,
            IDictionary <string, object?> contextData)
        {
            if (definition is not ObjectTypeDefinition objTypeDef)
            {
                return;
            }

            var options = completionContext.Services
                          .GetRequiredService <IFairyBreadOptions>();
            var validatorRegistry = completionContext.Services
                                    .GetRequiredService <IValidatorRegistry>();

            foreach (var fieldDef in objTypeDef.Fields)
            {
                // Don't add validation middleware unless:
                // 1. we have args
                var needsValidationMiddleware = false;

                foreach (var argDef in fieldDef.Arguments)
                {
                    var argCoord = new FieldCoordinate(objTypeDef.Name, fieldDef.Name, argDef.Name);

                    // 2. the argument should be validated according to options func
                    if (!options.ShouldValidateArgument(objTypeDef, fieldDef, argDef))
                    {
                        continue;
                    }

                    // 3. there's validators for it
                    List <ValidatorDescriptor> validatorDescs;
                    try
                    {
                        validatorDescs = DetermineValidatorsForArg(validatorRegistry, argDef);
                        if (validatorDescs.Count < 1)
                        {
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(
                                  $"Problem getting runtime type for argument '{argDef.Name}' " +
                                  $"in field '{fieldDef.Name}' on object type '{objTypeDef.Name}'.",
                                  ex);
                    }

                    // Cleanup context now we're done with these
                    foreach (var key in argDef.ContextData.Keys)
                    {
                        if (key.StartsWith(WellKnownContextData.Prefix))
                        {
                            argDef.ContextData.Remove(key);
                        }
                    }

                    validatorDescs.TrimExcess();
                    needsValidationMiddleware = true;
                    argDef.ContextData[WellKnownContextData.ValidatorDescriptors] = validatorDescs.AsReadOnly();
                }

                if (needsValidationMiddleware)
                {
                    if (_validationFieldMiddlewareDef is null)
                    {
                        _validationFieldMiddlewareDef = new FieldMiddlewareDefinition(
                            FieldClassMiddlewareFactory.Create <ValidationMiddleware>());
                    }

                    fieldDef.MiddlewareDefinitions.Insert(0, _validationFieldMiddlewareDef);
                }
            }
        }
Beispiel #21
0
 public static bool ExistsBottom(this FieldCoordinate coord) => coord.YCoord != YField.One;
Beispiel #22
0
 public static bool ExistsTop(this FieldCoordinate coord) => coord.YCoord != YField.Nine;
Beispiel #23
0
 public static bool ExistsRight(this FieldCoordinate coord) => coord.XCoord != XField.I;
Beispiel #24
0
 public static bool ExistsLeft(this FieldCoordinate coord) => coord.XCoord != XField.A;
Beispiel #25
0
 public static FieldCoordinate GetBottom(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord, coord.YCoord + 1);
Beispiel #26
0
 public static FieldCoordinate GetTop(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord, coord.YCoord - 1);
Beispiel #27
0
 public static FieldCoordinate GetRight(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord + 1, coord.YCoord);
Beispiel #28
0
 public static FieldCoordinate GetLeft(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord - 1, coord.YCoord);
Beispiel #29
0
 public Wall(FieldCoordinate topLeft, WallOrientation orientation)
 {
     Orientation = orientation;
     TopLeft     = topLeft;
 }
Beispiel #30
0
 public static PlayerState MovePlayer(this PlayerState playerState, FieldCoordinate newPosition)
 {
     return(new PlayerState(playerState.Player,
                            newPosition,
                            playerState.WallsToPlace));
 }