Пример #1
0
        /*
         * public JsonResult methods
         *
         */

        public JsonResult ContinueStory(Guid sessionGuid, string playId, int?choiceIndex)
        {
            // if we have a playId, this is a permaplay story, and we load it from permaplays instead of inkJsons.
            string inkJsonPath = string.IsNullOrEmpty(playId)
                ? _rootPath + _inkJsonsDirectory + sessionGuid + ".json"
                : _rootPath + _permaplaysDirectory + playId + ".json";
            string gameStatePath = _rootPath + _gameStatesDirectory + sessionGuid + ".json";

            try
            {
                // if no choices at all, this means we're starting a new story.
                if (!choiceIndex.HasValue)
                {
                    return(StartNewStory(inkJsonPath, gameStatePath));
                }

                // there was a choiceIndex selected, which means we're continuing a saved story.
                var story = InkMethods.RestoreStory(inkJsonPath, gameStatePath);

                // much happens in the Ink runtime here.
                story.ChooseChoiceIndex(choiceIndex.Value);

                List <InkOutputMessage> outputs = InkMethods.GetStoryOutputMessages(story);

                InkMethods.SaveStory(gameStatePath, story);

                return(base.Json(outputs));
            }
            catch (Exception x)
            {
                string message;

                if (x is StoryException)
                {
                    // we don't need to log story exceptions. we do want to parse the exception message a little.
                    message = GetStoryExceptionMessage((StoryException)x);
                }
                else
                {
                    _logger.LogError(Environment.NewLine + "HANDLED EXCEPTION IN Home/ContinueStory" + Environment.NewLine + x.ToString(), null);

                    message = "Some especially weird error occurred. If you get this message repeatedly, please file an issue at https://github.com/MattConrad/Quill/issues "
                              + $"with a copy of this error text (/Home/ContinueStory {DateTime.Now})";
                }

                CateError[] errors = new CateError[] { new CateError {
                                                           Message = message, LineNumber = -1
                                                       } };
                return(base.Json(new { errors }));
            }
        }
Пример #2
0
        public JsonResult PlayInk(string inktext, Guid sessionGuid)
        {
            try
            {
                string newInkPath  = _rootPath + _rawInksDirectory + sessionGuid + ".ink";
                string newJsonPath = _rootPath + _inkJsonsDirectory + sessionGuid + ".json";

                System.IO.File.WriteAllText(newInkPath, inktext);

                var processStartInfo = new ProcessStartInfo()
                {
                    Arguments = _rootPath + "/lib/inklecate.dll" + " -o " + newJsonPath + " " + newInkPath,
                    FileName  = "dnx",
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false
                };

                Process p = new Process();
                p.StartInfo = processStartInfo;
                p.Start();

                //let's hope any syntax errs are found w/in a second
                System.Threading.Thread.Sleep(1000);
                string errorMessage  = p.StandardError.ReadToEnd();
                string outputMessage = p.StandardOutput.ReadToEnd();
                p.WaitForExit(2000);

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    throw new InvalidOperationException(FixInkMessages(newInkPath, errorMessage));
                }
                if (!string.IsNullOrEmpty(outputMessage))
                {
                    throw new InvalidOperationException(FixInkMessages(newInkPath, outputMessage));
                }
                if (p.ExitCode != 0)
                {
                    throw new InvalidOperationException("Ink processing crashed. No details are available.");
                }

                return(Json(new { errors = new string[] {} }));
            }
            catch (Exception x)
            {
                try
                {
                    var errors = GetInklecateErrors(x.Message);
                    return(Json(new { errors = errors }));
                }
                //MWCTODO: this means GetInklecateErrors() threw a new exception, should also write to the internal log (figure out the RC2 way of doing this)
                catch
                {
                    var error = new CateError()
                    {
                        Message = x.Message, LineNumber = 0
                    };
                    return(Json(new { errors = new List <CateError>()
                                      {
                                          error
                                      } }));
                }
            }
        }