CommandLineUserInput() public method

public CommandLineUserInput ( ) : CommandLineInput
return CommandLineInput
Ejemplo n.º 1
0
        Compiler.CommandLineInputResult ReadCommandLineInput(string userInput)
        {
            var inputParser = new InkParser(userInput);
            var inputResult = inputParser.CommandLineUserInput();
            var result      = new Compiler.CommandLineInputResult();

            // Choice
            if (inputResult.choiceInput != null)
            {
                result.choiceIdx = ((int)inputResult.choiceInput) - 1;
                return(result);
            }

            // Help
            if (inputResult.isHelp)
            {
                result.output = "Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')";
                return(result);
            }

            // Quit
            if (inputResult.isExit)
            {
                result.requestsExit = true;
                return(result);
            }

            // If the compiler is available give it a chance to handle
            // the input.
            if (_compiler != null)
            {
                var compilerResult = _compiler.HandleInput(inputResult);
                if (compilerResult != null)
                {
                    return(compilerResult);
                }
            }

            result.output = "Unexpected input. Type 'help' or a choice number.";
            return(result);
        }
        public CommandLineInputResult ReadCommandLineInput(string userInput)
        {
            var inputParser = new InkParser(userInput);
            var inputResult = inputParser.CommandLineUserInput();

            var result = new CommandLineInputResult();

            // Choice
            if (inputResult.choiceInput != null)
            {
                result.choiceIdx = ((int)inputResult.choiceInput) - 1;
            }

            // Help
            else if (inputResult.isHelp)
            {
                result.output = "Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')";
            }

            // Quit
            else if (inputResult.isExit)
            {
                result.requestsExit = true;
            }

            // Request for debug source line number
            else if (inputResult.debugSource != null)
            {
                var offset = (int)inputResult.debugSource;
                var dm     = DebugMetadataForContentAtOffset(offset);
                if (dm != null)
                {
                    result.output = "DebugSource: " + dm.ToString();
                }
                else
                {
                    result.output = "DebugSource: Unknown source";
                }
            }

            // Request for runtime path lookup (to line number)
            else if (inputResult.debugPathLookup != null)
            {
                var pathStr       = inputResult.debugPathLookup;
                var contentResult = _runtimeStory.ContentAtPath(new Runtime.Path(pathStr));
                var dm            = contentResult.obj.debugMetadata;
                if (dm != null)
                {
                    result.output = "DebugSource: " + dm.ToString();
                }
                else
                {
                    result.output = "DebugSource: Unknown source";
                }
            }

            // User entered some ink
            else if (inputResult.userImmediateModeStatement != null)
            {
                var parsedObj = inputResult.userImmediateModeStatement as Parsed.Object;

                // Variable assignment: create in Parsed.Story as well as the Runtime.Story
                // so that we don't get an error message during reference resolution
                if (parsedObj is Parsed.VariableAssignment)
                {
                    var varAssign = (Parsed.VariableAssignment)parsedObj;
                    if (varAssign.isNewTemporaryDeclaration)
                    {
                        _parsedStory.TryAddNewVariableDeclaration(varAssign);
                    }
                }

                parsedObj.parent = _parsedStory;
                var runtimeObj = parsedObj.runtimeObject;

                parsedObj.ResolveReferences(_parsedStory);

                if (!_parsedStory.hadError)
                {
                    // Divert
                    if (parsedObj is Parsed.Divert)
                    {
                        var parsedDivert = parsedObj as Parsed.Divert;
                        result.divertedPath = parsedDivert.runtimeDivert.targetPath.ToString();
                    }

                    // Expression or variable assignment
                    else if (parsedObj is Parsed.Expression || parsedObj is Parsed.VariableAssignment)
                    {
                        var evalResult = _runtimeStory.EvaluateExpression((Runtime.Container)runtimeObj);
                        if (evalResult != null)
                        {
                            result.output = evalResult.ToString();
                        }
                    }
                }
                else
                {
                    _parsedStory.ResetError();
                }
            }
            else
            {
                result.output = "Unexpected input. Type 'help' or a choice number.";
            }

            return(result);
        }
Ejemplo n.º 3
0
        public void Begin()
        {
            EvaluateStory();

            var rand = new Random();

            while (story.currentChoices.Count > 0)
            {
                var choices = story.currentChoices;

                var          choiceIdx        = 0;
                bool         choiceIsValid    = false;
                Runtime.Path userDivertedPath = null;

                // autoPlay: Pick random choice
                if (autoPlay)
                {
                    choiceIdx = rand.Next() % choices.Count;
                }

                // Normal: Ask user for choice number
                else
                {
                    Console.ForegroundColor = ConsoleColor.Blue;

                    int i = 1;
                    foreach (Choice choice in choices)
                    {
                        Console.WriteLine("{0}: {1}", i, choice.text);
                        i++;
                    }


                    do
                    {
                        string userInput = Console.ReadLine();

                        var    inputParser    = new InkParser(userInput);
                        object evaluatedInput = inputParser.CommandLineUserInput();

                        // Choice
                        if (evaluatedInput is int?)
                        {
                            choiceIdx = ((int)evaluatedInput) - 1;

                            if (choiceIdx < 0 || choiceIdx >= choices.Count)
                            {
                                Console.WriteLine("Choice out of range");
                            }
                            else
                            {
                                choiceIsValid = true;
                            }
                        }

                        // Help
                        else if (evaluatedInput is string && (string)evaluatedInput == "help")
                        {
                            Console.WriteLine("Type a choice number, a divert (e.g. '==> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')");
                        }

                        // User entered some ink
                        else if (evaluatedInput is Parsed.Object)
                        {
                            // Variable assignment: create in Parsed.Story as well as the Runtime.Story
                            // so that we don't get an error message during reference resolution
                            if (evaluatedInput is Parsed.VariableAssignment)
                            {
                                var varAssign = (Parsed.VariableAssignment)evaluatedInput;
                                if (varAssign.isNewTemporaryDeclaration)
                                {
                                    parsedStory.TryAddNewVariableDeclaration(varAssign);
                                }
                            }

                            var parsedObj = (Parsed.Object)evaluatedInput;
                            parsedObj.parent = parsedStory;
                            parsedObj.GenerateRuntimeObject();
                            parsedObj.ResolveReferences(parsedStory);
                            var runtimeObj = parsedObj.runtimeObject;

                            if (!parsedStory.hadError)
                            {
                                // Divert
                                if (evaluatedInput is Parsed.Divert)
                                {
                                    userDivertedPath = ((Parsed.Divert)evaluatedInput).runtimeDivert.targetPath;
                                }

                                // Expression or variable assignment
                                else if (evaluatedInput is Parsed.Expression || evaluatedInput is Parsed.VariableAssignment)
                                {
                                    var result = story.EvaluateExpression((Container)runtimeObj);
                                    if (result != null)
                                    {
                                        Console.WriteLine(result);
                                    }
                                }
                            }
                            else
                            {
                                parsedStory.ResetError();
                            }
                        }

                        else
                        {
                            Console.WriteLine("Unexpected input. Type 'help' or a choice number.");
                        }
                    } while(!choiceIsValid && userDivertedPath == null);
                }

                Console.ResetColor();

                if (choiceIsValid)
                {
                    story.ChooseChoiceIndex(choiceIdx);
                }
                else if (userDivertedPath != null)
                {
                    story.ChoosePath(userDivertedPath);
                    userDivertedPath = null;
                }

                EvaluateStory();
            }
        }
Ejemplo n.º 4
0
		public void Begin()
		{
            EvaluateStory ();

			var rand = new Random ();

            while (story.currentChoices.Count > 0 || this.keepOpenAfterStoryFinish) {
				var choices = story.currentChoices;
				
                var choiceIdx = 0;
                bool choiceIsValid = false;
                Runtime.Path userDivertedPath = null;

				// autoPlay: Pick random choice
				if (autoPlay) {
					choiceIdx = rand.Next () % choices.Count;
				}

				// Normal: Ask user for choice number
				else {

                    Console.ForegroundColor = ConsoleColor.Blue;

                    // Add extra newline to ensure that the choice is
                    // on a separate line.
                    Console.WriteLine ();

					int i = 1;
					foreach (Choice choice in choices) {
						Console.WriteLine ("{0}: {1}", i, choice.text);
						i++;
					}


                    do {
                        // Prompt
                        Console.Write("?> ");
                        string userInput = Console.ReadLine ();

                        var inputParser = new InkParser (userInput);
                        var input = inputParser.CommandLineUserInput();

                        // Choice
                        if (input.choiceInput != null) {

                            choiceIdx = ((int)input.choiceInput) - 1;

                            if (choiceIdx < 0 || choiceIdx >= choices.Count) {
                                Console.WriteLine ("Choice out of range");
                            } else {
                                choiceIsValid = true;
                            }
                        }

                        // Help
                        else if (input.isHelp) {
                            Console.WriteLine ("Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')");
                        }

                        // Quit
                        else if (input.isExit) {
                            return;
                        }

                        // Request for debug source line number
                        else if (input.debugSource != null) {
                            var offset = (int)input.debugSource;
                            var dm = DebugMetadataForContentAtOffset (offset);
                            if (dm != null)
                                Console.WriteLine ("DebugSource: "+dm.ToString ());
                            else
                                Console.WriteLine ("DebugSource: Unknown source");
                        }

                        // User entered some ink
                        else if (input.userImmediateModeStatement != null ) {

                            var parsedObj = input.userImmediateModeStatement as Parsed.Object;

                            // Variable assignment: create in Parsed.Story as well as the Runtime.Story
                            // so that we don't get an error message during reference resolution
                            if( parsedObj is Parsed.VariableAssignment ) {
                                var varAssign = (Parsed.VariableAssignment) parsedObj;
                                if( varAssign.isNewTemporaryDeclaration ) {
                                    parsedStory.TryAddNewVariableDeclaration(varAssign);
                                }
                            }

                            parsedObj.parent = parsedStory;
                            var runtimeObj = parsedObj.runtimeObject;

                            parsedObj.ResolveReferences(parsedStory);

                            if( !parsedStory.hadError ) {

                                // Divert
                                if( parsedObj is Parsed.Divert ) {
                                    var parsedDivert = parsedObj as Parsed.Divert;
                                    userDivertedPath = parsedDivert.runtimeDivert.targetPath;
                                }

                                // Expression or variable assignment
                                else if( parsedObj is Parsed.Expression || parsedObj is Parsed.VariableAssignment ) {
                                    var result = story.EvaluateExpression((Container)runtimeObj);
                                    if( result != null ) {
                                        Console.WriteLine(result.ToString());
                                    }
                                }
                            } else {
                                parsedStory.ResetError();
                            }

                        }

                        else {
                            Console.WriteLine ("Unexpected input. Type 'help' or a choice number.");
                        }

                    } while(!choiceIsValid && userDivertedPath == null);

				}

                Console.ResetColor ();

                if (choiceIsValid) {
                    story.ChooseChoiceIndex (choiceIdx);
                } else if (userDivertedPath != null) {
                    story.ChoosePath (userDivertedPath);
                    userDivertedPath = null;
                }
                    
                EvaluateStory ();
			}
		}
Ejemplo n.º 5
0
        public void Begin()
        {
            EvaluateStory();

            var rand = new Random();

            while (story.currentChoices.Count > 0 || this.keepOpenAfterStoryFinish)
            {
                var choices = story.currentChoices;

                var          choiceIdx        = 0;
                bool         choiceIsValid    = false;
                Runtime.Path userDivertedPath = null;

                // autoPlay: Pick random choice
                if (autoPlay)
                {
                    choiceIdx = rand.Next() % choices.Count;
                }

                // Normal: Ask user for choice number
                else
                {
                    Console.ForegroundColor = ConsoleColor.Blue;

                    // Add extra newline to ensure that the choice is
                    // on a separate line.
                    Console.WriteLine();

                    int i = 1;
                    foreach (Choice choice in choices)
                    {
                        Console.WriteLine("{0}: {1}", i, choice.text);
                        i++;
                    }


                    do
                    {
                        // Prompt
                        Console.Write("?> ");
                        string userInput = Console.ReadLine();

                        // If we have null user input, it means that we're
                        // "at the end of the stream", or in other words, the input
                        // stream has closed, so there's nothing more we can do.
                        // We return immediately, since otherwise we get into a busy
                        // loop waiting for user input.
                        if (userInput == null)
                        {
                            Console.WriteLine("<User input stream closed.>");
                            return;
                        }

                        var inputParser = new InkParser(userInput);
                        var input       = inputParser.CommandLineUserInput();

                        // Choice
                        if (input.choiceInput != null)
                        {
                            choiceIdx = ((int)input.choiceInput) - 1;

                            if (choiceIdx < 0 || choiceIdx >= choices.Count)
                            {
                                Console.WriteLine("Choice out of range");
                            }
                            else
                            {
                                choiceIsValid = true;
                            }
                        }

                        // Help
                        else if (input.isHelp)
                        {
                            Console.WriteLine("Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')");
                        }

                        // Quit
                        else if (input.isExit)
                        {
                            return;
                        }

                        // Request for debug source line number
                        else if (input.debugSource != null)
                        {
                            var offset = (int)input.debugSource;
                            var dm     = DebugMetadataForContentAtOffset(offset);
                            if (dm != null)
                            {
                                Console.WriteLine("DebugSource: " + dm.ToString());
                            }
                            else
                            {
                                Console.WriteLine("DebugSource: Unknown source");
                            }
                        }

                        // User entered some ink
                        else if (input.userImmediateModeStatement != null)
                        {
                            var parsedObj = input.userImmediateModeStatement as Parsed.Object;

                            // Variable assignment: create in Parsed.Story as well as the Runtime.Story
                            // so that we don't get an error message during reference resolution
                            if (parsedObj is Parsed.VariableAssignment)
                            {
                                var varAssign = (Parsed.VariableAssignment)parsedObj;
                                if (varAssign.isNewTemporaryDeclaration)
                                {
                                    parsedStory.TryAddNewVariableDeclaration(varAssign);
                                }
                            }

                            parsedObj.parent = parsedStory;
                            var runtimeObj = parsedObj.runtimeObject;

                            parsedObj.ResolveReferences(parsedStory);

                            if (!parsedStory.hadError)
                            {
                                // Divert
                                if (parsedObj is Parsed.Divert)
                                {
                                    var parsedDivert = parsedObj as Parsed.Divert;
                                    userDivertedPath = parsedDivert.runtimeDivert.targetPath;
                                }

                                // Expression or variable assignment
                                else if (parsedObj is Parsed.Expression || parsedObj is Parsed.VariableAssignment)
                                {
                                    var result = story.EvaluateExpression((Container)runtimeObj);
                                    if (result != null)
                                    {
                                        Console.WriteLine(result.ToString());
                                    }
                                }
                            }
                            else
                            {
                                parsedStory.ResetError();
                            }
                        }

                        else
                        {
                            Console.WriteLine("Unexpected input. Type 'help' or a choice number.");
                        }
                    } while(!choiceIsValid && userDivertedPath == null);
                }

                Console.ResetColor();

                if (choiceIsValid)
                {
                    story.ChooseChoiceIndex(choiceIdx);
                }
                else if (userDivertedPath != null)
                {
                    story.ChoosePath(userDivertedPath);
                    userDivertedPath = null;
                }

                EvaluateStory();
            }
        }