Example #1
0
        /// <summary>
        /// Check if the Question is in valid format.
        /// </summary>
        public bool IsValid(out string errMsg)
        {
            errMsg = "";
            if (string.IsNullOrEmpty(QuestionText))
            {
                errMsg = "Question text cannot be empty!";
                return(false);
            }

            if (Choices.Length != NUM_OF_ANS)
            {
                errMsg = "There are no " + NUM_OF_ANS + " answers!";
                return(false);
            }

            if (string.IsNullOrEmpty(CorrectAnswer))
            {
                errMsg = "Correct answer cannot be empty!";
                return(false);
            }

            if (!Choices.Contains(CorrectAnswer))
            {
                errMsg = "Answers doesn't match correct answer.";
                return(false);
            }

            return(true);
        }
Example #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // If it doesn't match any of the choices => error
            if (!string.IsNullOrEmpty(value?.ToString()) && !Choices.Contains(value))
            {
                // This is a programmer error, no need to localize it
                string concatenatedChoices = string.Join(", ", Choices.Select(e => e.ToString()));
                return(new ValidationResult($"Only the following values are allowed: {concatenatedChoices}"));
            }

            // All is good
            return(ValidationResult.Success);
        }
Example #3
0
        public void Pick(IEntity Choice)
        {
            if (ChoiceType != ChoiceType.GENERAL)
            {
                throw new ChoiceException("Attempting to select a card with Pick for a mulligan or unknown choice card");
            }

            if (!Choices.Contains(Choice))
            {
                throw new ChoiceException("Attempting to select an unavailable card with Pick");
            }

            Keeping = new List <IEntity>()
            {
                Choice
            };
            Controller.Game.Action(Controller, Actions.Choose(Controller));
        }
Example #4
0
 public override bool IsValid(object value)
 {
     return(Choices.Contains(value.ToString()));
 }
Example #5
0
        /// <summary>
        /// Shows the prompt and requests input from the user.
        /// </summary>
        /// <param name="console">The console to show the prompt in.</param>
        /// <returns>The user input converted to the expected type.</returns>
        /// <inheritdoc/>
        public T Show(IAnsiConsole console)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            var promptStyle = PromptStyle ?? Style.Plain;

            WritePrompt(console);

            while (true)
            {
                var input = console.ReadLine(promptStyle, IsSecret);

                // Nothing entered?
                if (string.IsNullOrWhiteSpace(input))
                {
                    if (DefaultValue != null)
                    {
                        console.Write(TextPrompt <T> .GetTypeConverter().ConvertToInvariantString(DefaultValue.Value), promptStyle);
                        console.WriteLine();
                        return(DefaultValue.Value);
                    }

                    if (!AllowEmpty)
                    {
                        continue;
                    }
                }

                console.WriteLine();

                // Try convert the value to the expected type.
                if (!TextPrompt <T> .TryConvert(input, out var result) || result == null)
                {
                    console.MarkupLine(ValidationErrorMessage);
                    WritePrompt(console);
                    continue;
                }

                if (Choices.Count > 0)
                {
                    if (Choices.Contains(result))
                    {
                        return(result);
                    }
                    else
                    {
                        console.MarkupLine(InvalidChoiceMessage);
                        WritePrompt(console);
                        continue;
                    }
                }

                // Run all validators
                if (!ValidateResult(result, out var validationMessage))
                {
                    console.MarkupLine(validationMessage);
                    WritePrompt(console);
                    continue;
                }

                return(result);
            }
        }