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); } }
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); } }
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)); }
private static bool IsValidWallPosition(FieldCoordinate coord) { return(coord.XCoord >= XField.A && coord.XCoord <= XField.H && coord.YCoord >= YField.Nine && coord.YCoord <= YField.Two); }
private static bool IsValidFigurePosition(FieldCoordinate coord) { return(coord.XCoord >= XField.A && coord.XCoord <= XField.I && coord.YCoord >= YField.Nine && coord.YCoord <= YField.One); }
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();
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 !;
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); }
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; }
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); }
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; }
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; }
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(); } }
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 } &&
// 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)); }
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); } }
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); } } }
public static bool ExistsBottom(this FieldCoordinate coord) => coord.YCoord != YField.One;
public static bool ExistsTop(this FieldCoordinate coord) => coord.YCoord != YField.Nine;
public static bool ExistsRight(this FieldCoordinate coord) => coord.XCoord != XField.I;
public static bool ExistsLeft(this FieldCoordinate coord) => coord.XCoord != XField.A;
public static FieldCoordinate GetBottom(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord, coord.YCoord + 1);
public static FieldCoordinate GetTop(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord, coord.YCoord - 1);
public static FieldCoordinate GetRight(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord + 1, coord.YCoord);
public static FieldCoordinate GetLeft(this FieldCoordinate coord) => new FieldCoordinate(coord.XCoord - 1, coord.YCoord);
public Wall(FieldCoordinate topLeft, WallOrientation orientation) { Orientation = orientation; TopLeft = topLeft; }
public static PlayerState MovePlayer(this PlayerState playerState, FieldCoordinate newPosition) { return(new PlayerState(playerState.Player, newPosition, playerState.WallsToPlace)); }