Esempio n. 1
0
        private ConsoleCommandVariant MatchVariant(ICharacter byCharacter, string[] arguments)
        {
            this.InitializeIfRequired();

            var bestMatchRating = 0;
            ConsoleCommandVariant bestMatchMethod = null;

            foreach (var method in this.Variants)
            {
                var rating = method.GetMatchRating(byCharacter, arguments);
                if (bestMatchRating < rating)
                {
                    bestMatchRating = rating;
                    bestMatchMethod = method;
                }
                else if (bestMatchMethod is null)
                //&& method.RequiredParametersCount == 0)
                {
                    // method without parameters
                    bestMatchMethod = method;
                }
            }

            return(bestMatchMethod);
        }
Esempio n. 2
0
 public static IReadOnlyList <string> ServerSuggestAutocomplete(
     string text,
     ICharacter byCharacter,
     ushort textPosition,
     out ConsoleCommandVariant consoleCommandVariant)
 {
     return(SharedSuggestAutocomplete(text, byCharacter, textPosition, out consoleCommandVariant));
 }
Esempio n. 3
0
 public string[] GetParameterSuggestions(ICharacter byCharacter, out ConsoleCommandVariant consoleCommandVariant)
 {
     return(this.ConsoleCommand.GetParameterSuggestions(
                byCharacter,
                this.arguments,
                this.argumentIndexForSuggestion,
                out consoleCommandVariant));
 }
Esempio n. 4
0
        public string[] GetParameterSuggestions(
            [NotNull] ICharacter byCharacter,
            string[] arguments,
            byte argumentIndex,
            out ConsoleCommandVariant consoleCommandVariant)
        {
            this.Validate(byCharacter);

            var bestMatchedVariant = this.MatchVariant(byCharacter, arguments);

            if (bestMatchedVariant is null)
            {
                consoleCommandVariant = null;
                return(null);
            }

            var startsWith = argumentIndex < arguments.Length
                                 ? arguments[argumentIndex]
                                 : string.Empty;

            consoleCommandVariant = bestMatchedVariant;
            return(bestMatchedVariant.GetParameterSuggestions(startsWith, argumentIndex));
        }
Esempio n. 5
0
        private static IReadOnlyList <string> SharedSuggestAutocomplete(
            string text,
            ICharacter byCharacter,
            ushort textPosition,
            out ConsoleCommandVariant consoleCommandVariant)
        {
            if (string.IsNullOrEmpty(text))
            {
                consoleCommandVariant = null;
                return(EmptySuggestionsArray);
            }

            var commandData = SharedParseCommand(text, out var parsedCommandName, textPosition);
            IReadOnlyList <string> suggestions;

            var isOperator  = ServerIsOperatorOrSystemConsole(byCharacter);
            var isModerator = ServerIsModeratorOrSystemConsole(byCharacter);

            if (commandData is null) // don't have parsed command - suggest commands
            {
                if (text.Contains(' ') &&
                    text.TrimEnd(' ').Contains(' '))
                {
                    // cannot suggest commands as user entered a space char in between which is supposed to be a command with args
                    consoleCommandVariant = null;
                    return(EmptySuggestionsArray);
                }

                var suggestedCommands = SharedGetCommandNamesSuggestions(parsedCommandName ?? string.Empty);
                SharedFilterAvailableCommands(suggestedCommands, isOperator, isModerator);

                suggestions           = suggestedCommands.Select(sc => sc.GetNameOrAlias(parsedCommandName)).ToList();
                consoleCommandVariant = suggestedCommands.FirstOrDefault()?.Variants.FirstOrDefault();
            }
            else // parsed command - suggest parameters for this command
            {
                var consoleCommandKind = commandData.ConsoleCommand.Kind;
                if (!isOperator &&
                    consoleCommandKind.HasFlag(ConsoleCommandKinds.ServerOperator) &&
                    (IsServer ||
                     !consoleCommandKind.HasFlag(ConsoleCommandKinds.Client)))
                {
                    // cannot suggest anything to the non-operator player for an operator-only command
                    consoleCommandVariant = null;
                    return(EmptySuggestionsArray);
                }

                if (!isModerator &&
                    consoleCommandKind.HasFlag(ConsoleCommandKinds.ServerModerator) &&
                    (IsServer ||
                     !consoleCommandKind.HasFlag(ConsoleCommandKinds.Client)))
                {
                    // cannot suggest anything to the non-moderator player for a moderator-only command
                    consoleCommandVariant = null;
                    return(EmptySuggestionsArray);
                }

                suggestions = commandData.GetParameterSuggestions(byCharacter, out consoleCommandVariant);
            }

            if (suggestions?.Count >= MaxSuggestions)
            {
                suggestions = suggestions.Take(MaxSuggestions).ToList();
            }

            return(suggestions ?? EmptySuggestionsArray);
        }