private static bool IsValidResponse(WebSocketActionResponse response) { var content = response?.Payload?.Data?.SubscribeContent; if (content == null) { Console.Write("\nPress any key to continue..."); Console.ReadKey(); return(false); } if (content.Error != null) { Console.WriteLine($"En error occurred: {content.Error.Message}"); Console.Write("Press any key to continue..."); Console.ReadKey(); return(false); } return(true); }
private static async Task ProcessAdventureAsync(uint adventureId, List <History> historyList) { ActionType action; string text; uint lastActionId = 0; // Seems that in a custom adventure the last 2 history will always be empty, so we need to filter that. historyList.RemoveAll(x => string.IsNullOrEmpty(x.Text)); // This should prevent any errors if (historyList.Count == 0) { Console.WriteLine("The adventure is empty, strange..."); Console.Write("Press any key to continue..."); Console.ReadKey(); return; } string lastHistory = historyList[historyList.Count - 1].Text; Console.Clear(); if (historyList.Count > 1) { string previousHistory = ""; historyList.RemoveAt(historyList.Count - 1); foreach (var history in historyList) { previousHistory += history.Text; } Console.Write(previousHistory); await Task.Delay(5000); } TypeLine(lastHistory.Trim() + "\n"); TypeLine("Quick help: Use \"Do\", \"Say\" or \"Story\" at the start of your input to do an action, say something, or make progress on the story."); TypeLine("Other commands:\n"); TypeLine("\"Undo\": Undo the last action."); TypeLine("\"Redo\": Redo the last action."); TypeLine("\"Alter\": Alter the last action."); TypeLine("\"Remember\": Edit the memory context."); TypeLine("\"Retry\": Retry the last action and generate a new response.\n"); TypeLine("Undo, Redo and Retry do not require any input.\n"); TypeLine("See the help in the menu for more info."); TypeLine("Enter \"quit\" or \"exit\" to exit."); while (true) { Console.Write("\n> "); text = Console.ReadLine()?.Trim(); if (string.IsNullOrEmpty(text)) { action = ActionType.Continue; } else { if (text.ToLowerInvariant() == "quit" || text.ToLowerInvariant() == "exit") { break; } // Split the text in two, the first part should be the input type, and the second part, the text. string[] splitText = text.Split(' ', 2); if (Enum.TryParse(splitText[0], true, out action)) { // if only the command is passed if (splitText.Length == 1) { // if the command requires an input if (action == ActionType.Remember || action == ActionType.Alter) { Console.WriteLine("You must enter a text when using Remember / Alter commands."); continue; } } else { // else, actionType will be one of the valid input types // and text will be the second part (the rest) of the split text. text = splitText[1]; } } else { // if the parse fails, keep the text and set the input type to Do (the default). action = ActionType.Do; } } //TypeLine("Generating story..."); if (action == ActionType.Alter) { lastActionId = uint.Parse(historyList[historyList.Count - 1].Id, CultureInfo.InvariantCulture); } WebSocketActionResponse response = null; try { response = await API.RunWebSocketActionAsync(adventureId, action, text, lastActionId); } catch (IOException e) { Console.WriteLine($"An error occurred: {e.Message}"); } catch (JsonSerializationException e) { Console.WriteLine($"An error occurred: {e.Message}"); } catch (TimeoutException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(response)) { continue; //break; } historyList = response.Payload.Data.SubscribeContent.HistoryList; string textToShow = historyList[historyList.Count - 1].Text; if (action != ActionType.Continue && action != ActionType.Undo && action != ActionType.Redo && action != ActionType.Alter) { textToShow = historyList[historyList.Count - 2].Text + textToShow; } TypeLine(textToShow); } }
private static async Task CreateNewGameAsync() { Console.Write("\nLoading..."); ScenarioResponse modeList = null; try { modeList = await API.GetScenarioAsync(AIDungeon.AllScenarios); } catch (HttpRequestException e) { Console.WriteLine($"\nAn error occurred: {e.Message}"); } if (!IsValidResponse(modeList)) { return; } SortedList <string, uint> modes = new SortedList <string, uint>(); foreach (var mode in modeList.Data.Content.Options) { modes.Add(mode.Title, uint.Parse(mode.Id.Substring(9), CultureInfo.InvariantCulture)); // "scenario:xxxxxx" } var tempModes = new List <string>(modes.Keys) { "Exit to menu" }; int modeOption = OptionSelection("Select a setting...", tempModes); if (modeOption == tempModes.Count) { // Exit return; } modeOption--; List <History> historyList; uint id; if (modes.Keys[modeOption] != "custom") { ScenarioResponse characterList = null; try { characterList = await API.GetScenarioAsync(modes.Values[modeOption]); } catch (HttpRequestException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(characterList)) { return; } SortedList <string, uint> characters = new SortedList <string, uint>(); foreach (var character in characterList.Data.Content.Options) { characters.Add(character.Title, uint.Parse(character.Id.Substring(9), CultureInfo.InvariantCulture)); // "scenario:xxxxxx" } var tempCharacters = new List <string>(characters.Keys) { "Exit to menu" }; int characterOption = OptionSelection("Select a character...", tempCharacters); if (characterOption == tempCharacters.Count) { // Exit return; } characterOption--; string name; while (true) { Console.Write("\n\nEnter the character name: "); name = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(name)) { break; } Console.WriteLine("You must specify a name."); } Console.WriteLine($"Creating a new adventure with the mode: {modes.Keys[modeOption]}, and the character: {characters.Keys[characterOption]}..."); ScenarioResponse scenario = null; try { scenario = await API.GetScenarioAsync(characters.Values[characterOption]); } catch (HttpRequestException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(scenario)) { return; } CreationResponse response = null; try { response = await API.CreateAdventureAsync(characters.Values[characterOption], scenario.Data.Content.Prompt.Replace("${character.name}", name)); } catch (HttpRequestException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(response)) { return; } if (response.Data.AdventureInfo == null) { Console.Write("Seems that the access token is invalid.\nPlease edit the token in the menu/Edit config, or try logging out and logging in.\n"); Console.Write("\nPress any key to continue..."); Console.ReadKey(); return; } id = uint.Parse(response.Data.AdventureInfo.ContentId, CultureInfo.InvariantCulture); // "adventure:xxxxxxx" historyList = response.Data.AdventureInfo.HistoryList; // result.Data.AdventureInfo.HistoryList.Count - 1 } else { string customText; Console.WriteLine($"\n{CustomPrompt}\n"); while (true) { Console.Write("Prompt: "); customText = Console.ReadLine(); if (string.IsNullOrWhiteSpace(customText)) { Console.WriteLine("You must enter a text."); } else if (customText.Length > 140) { Console.WriteLine($"Text length must be lower than 140. (Current: {customText.Length})"); } else { break; } } Console.WriteLine("Creating a new adventure with the custom prompt..."); CreationResponse adventure = null; try { adventure = await API.CreateAdventureAsync(modes.Values[modeOption]); } catch (HttpRequestException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(adventure)) { return; } WebSocketActionResponse response = null; try { response = await API.RunWebSocketActionAsync(uint.Parse(adventure.Data.AdventureInfo.ContentId, CultureInfo.InvariantCulture), ActionType.Story, customText); } catch (IOException e) { Console.WriteLine($"An error occurred: {e.Message}"); } catch (JsonSerializationException e) { Console.WriteLine($"An error occurred: {e.Message}"); } catch (TimeoutException e) { Console.WriteLine($"An error occurred: {e.Message}"); } if (!IsValidResponse(response)) { return; } var content = response.Payload.Data.SubscribeContent; id = uint.Parse(content.Id.Substring(10), CultureInfo.InvariantCulture); // "adventure:xxxxxxx" historyList = content.HistoryList; } await ProcessAdventureAsync(id, historyList); }