コード例 #1
0
        public TokenizedInput Tokenize(string input)
        {
            var result = new TokenizedInput();

            if (input != null)
            {
                input = input.ToLower();
                input = input.Replace(',', ' ');

                var tokens = input.Split(' ');

                foreach (string token in tokens)
                {
                    if (IgnoredWords.Contains(token))
                    {
                        continue;
                    }

                    if (ReplacedWords.Contains(token))
                    {
                        result.Add(ReplacedWords.ReplacementFor(token));
                    }
                    else
                    {
                        result.Add(token);
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        private CommandLineParserResult HandleExcept(CommandLineParserResult result, TokenizedInput tokens, string currentToken)
        {
            var index = tokens.IndexOf(currentToken) + 1;

            if (index >= tokens.Count)
            {
                return(null);
            }

            var except = new List <Object>();

            // process rest of the tokens as objects
            for (int i = index; i < tokens.Count; i++)
            {
                // TODO: handle multiple objects with same name
                var next = tokens[i];

                if (next.IsPreposition() && result.Preposition == null)
                {
                    result.Preposition = Prepositions.Get(next);
                    continue;
                }

                var obj = GetObject(result, next);

                if (obj == null)
                {
                    result.Error = Messages.CantSeeObject;
                    return(result);
                }
                else if (obj is MultipleObjectsFound)
                {
                    var input = GetInput(result.Verb);

                    if (input.Error.HasValue())
                    {
                        return(input);
                    }

                    if (input.Objects.Count > 0)
                    {
                        except.AddRange(input.Objects);
                    }
                }
                else
                {
                    except.Add(obj);
                }
            }

            result.Objects = result.Objects.Where(x => !except.Contains(x)).ToList();

            return(null);
        }
コード例 #3
0
        private CommandLineParserResult Parse(Verb verb, TokenizedInput tokens)
        {
            var result = new CommandLineParserResult
            {
                Verb = verb
            };

            var lastToken = "";

            foreach (string token in tokens)
            {
                var obj = GetObject(result, token);

                if (obj is Skip)
                {
                    continue;
                }

                if (result.Objects.Contains(obj))
                {
                    // will happen for something like "take the brass lamp"
                    continue;
                }

                if (obj != null)
                {
                    if (obj.InScope)
                    {
                        if (result.Preposition == null || !result.Objects.Any())
                        {
                            // handles commands like "put on coat", "put down book", etc.
                            result.Ordered.Add(obj);
                            result.Objects.Add(obj);
                        }
                        else
                        {
                            result.Ordered.Add(obj);
                            result.IndirectObject = obj;
                        }
                    }
                    else if (obj is MultipleObjectsFound)
                    {
                        return(ResolveMultipleObjects(verb, (MultipleObjectsFound)obj));
                    }
                    else
                    {
                        result.Error = Messages.CantSeeObject;
                        return(result);
                    }
                }

                // distinguish between prepositions and movement - "go south", "put bottle down", "close up grate"
                else if ((result.Verb is IDirectionProxy) && token.IsDirection() && !result.Objects.Any())
                {
                    var v = token.ToVerb();
                    result.Ordered.Add(v);
                    result.Verb = v;
                }

                else if (token.IsPreposition())
                {
                    var p = Prepositions.Get(token);
                    result.Ordered.Add(p);
                    result.Preposition = p;
                }

                else if (token == "all" && !result.Objects.Any())
                {
                    result.IsAll = true;

                    if (!verb.Multi && !verb.MultiHeld)
                    {
                        result.Error = Messages.MultiNotAllowed;
                        break;
                    }

                    var multi = new List <Object>();

                    if (verb.Multi)
                    {
                        var objectsInRoom =
                            from o in CurrentRoom.ObjectsInRoom()
                            where !o.Animate
                            select o;

                        multi.AddRange(objectsInRoom);
                    }

                    if (verb.MultiHeld)
                    {
                        multi.AddRange(Inventory.Items);
                    }

                    // if object count is only 1, we don't add it so it can be handled in the verb using implict
                    // messages e.g. (the small bottle)
                    if (multi.Count > 1)
                    {
                        result.Objects.AddRange(multi);
                    }
                }

                else if (token == "except" && (verb.Multi || verb.MultiHeld) && lastToken == "all")
                {
                    var except = HandleExcept(result, tokens, token);

                    if (except != null)
                    {
                        return(except);
                    }

                    break;
                }

                else
                {
                    obj = result.Objects.FirstOrDefault();

                    if (obj != null && !result.IsAll)
                    {
                        result.Error = Messages.PartialUnderstanding(verb, obj);
                    }
                    else
                    {
                        result.Error = Messages.CantSeeObject;
                    }

                    return(result);
                }

                lastToken = token;
            }

            return(result);
        }