private IOutputSpeech Do(AdventureEngine engine, AdventureCommandType command, XElement ssml = null) { ssml = ssml ?? new XElement("speak"); ProcessResponse(engine.Do(command)); return(new SsmlOutputSpeech { Ssml = ssml.ToString(SaveOptions.DisableFormatting) }); // local functions void ProcessResponse(AdventureResponse response) { switch (response) { case AdventureResponseSay say: ssml.Add(new XElement("p", new XText(say.Text))); break; case AdventureResponseDelay delay: ssml.Add(new XElement("break", new XAttribute("time", (int)delay.Delay.TotalMilliseconds + "ms"))); break; case AdventureResponsePlay play: ssml.Add(new XElement("audio", new XAttribute("src", _adventureSoundFilesPublicUrl + play.Name))); break; case AdventureResponseNotUnderstood _: ssml.Add(new XElement("p", new XText(PROMPT_MISUNDERSTOOD))); break; case AdventureResponseBye _: ssml.Add(new XElement("p", new XText(PROMPT_GOODBYE))); break; case AdventureResponseFinished _: break; case AdventureResponseMultiple multiple: foreach (var nestedResponse in multiple.Responses) { ProcessResponse(nestedResponse); } break; default: throw new AdventureException($"Unknown response type: {response?.GetType().FullName}"); } } }
private static void AdventureLoop(AdventureEngine engine) { // start the adventure loop ProcessResponse(engine.Do(AdventureCommandType.Restart)); try { while (true) { // prompt user input Console.Write("> "); var commandText = Console.ReadLine().Trim().ToLower(); if (!Enum.TryParse(commandText, true, out AdventureCommandType command)) { // TODO (2017-07-21, bjorg): need a way to invoke a 'command not understood' reaction // responses = new[] { new AdventureResponseNotUnderstood() }; continue; } // process user input ProcessResponse(engine.Do(command)); } } catch (Exception e) { Console.Error.WriteLine(e); } // local functions void ProcessResponse(AAdventureResponse response) { switch (response) { case AdventureResponseSay say: TypeLine(say.Text); break; case AdventureResponseDelay delay: System.Threading.Thread.Sleep(delay.Delay); break; case AdventureResponsePlay play: Console.WriteLine($"({play.Name})"); break; case AdventureResponseNotUnderstood _: TypeLine("Sorry, I don't know what that means."); break; case AdventureResponseBye _: TypeLine("Good bye."); System.Environment.Exit(0); break; case AdventureResponseFinished _: break; case AdventureResponseMultiple multiple: foreach (var nestedResponse in multiple.Responses) { ProcessResponse(nestedResponse); } break; default: throw new AdventureException($"Unknown response type: {response?.GetType().FullName}"); } } }