diff --git a/Cargo.toml b/Cargo.toml index 87328ba..7e28bfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ authors = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "3.2.17", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index e7a11a9..8bedb7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +mod story_manager; +use story_manager::{introduction, tutorial}; +mod user_command; + fn main() { - println!("Hello, world!"); + introduction(); + tutorial(); } diff --git a/src/story_manager/mod.rs b/src/story_manager/mod.rs new file mode 100644 index 0000000..9f25801 --- /dev/null +++ b/src/story_manager/mod.rs @@ -0,0 +1,22 @@ +use crate::user_command::{ + get_user_input, get_valid_action, get_valid_response, VALID_AFFIRMATIVE_RESPONSES, +}; + +pub fn introduction() { + println!("Welcome to Dust Plight!") +} + +pub fn tutorial() { + // General Tutorial Intro + println!("This tutorial will guide you through a basic scenario, like you might encounter while playing the game. + // \nIn each state of the game, you will be presented with an environment described to you based on your current position. It will be up to you to determine your next action based on this description. Are you interested in running the tutorial?"); + + + let response = get_user_input(&|input| get_valid_response(input.to_string())); + if VALID_AFFIRMATIVE_RESPONSES.contains(&response.as_str()) { + println!("Great! Let's get started with the tutorial.") + } + + println!("Excellent! Now that you're ready to go, let's practice some movement."); + get_user_input(&|input| get_valid_action(input.to_string())); +} diff --git a/src/user_command/mod.rs b/src/user_command/mod.rs new file mode 100644 index 0000000..128036e --- /dev/null +++ b/src/user_command/mod.rs @@ -0,0 +1,62 @@ +use std::io::{self, BufRead}; + +use clap::Parser; + +pub static VALID_AFFIRMATIVE_RESPONSES: [&str; 2] = ["yes", "y"]; +pub static VALID_NEGATIVE_RESPONSES: [&str; 2] = ["no", "n"]; +static VALID_ACTIONS: [&str; 1] = ["move"]; + +pub fn get_valid_response(input: String) -> (String, bool) { + let response_is_valid = is_valid(&input, &VALID_AFFIRMATIVE_RESPONSES) + || is_valid(&input, &VALID_NEGATIVE_RESPONSES); + if response_is_valid { + return (input, true); + } + return ( + format!( + "Please provide a valid response: {}, {}.", + VALID_AFFIRMATIVE_RESPONSES.join(", "), + VALID_NEGATIVE_RESPONSES.join(", ") + ), + false, + ); +} + +pub fn get_valid_action(input: String) -> (String, bool) { + let action_is_valid = is_valid(&input, &VALID_ACTIONS); + if action_is_valid { + return (input, true); + } + return ( + format!( + "Please provide a valid action: {}.", + VALID_ACTIONS.join(", ") + ), + false, + ); +} + +pub fn get_user_input(f: &dyn Fn(&String) -> (String, bool)) -> String { + let stdin = io::stdin(); + + let mut buffer = String::with_capacity(2048); + // Lock our standard input to eliminate synchronization overhead (unlocks when dropped) + let mut stdin = io::stdin().lock(); + + // Read our first line. + stdin.read_line(&mut buffer); + + let mut result = f(&buffer); + while !result.1 { + println!("{}", result.0); + stdin.read_line(&mut buffer); + result = f(&buffer); + buffer.clear(); + } + + return result.0; +} + +fn is_valid(command: &str, valid_commands: &[&str]) -> bool { + return valid_commands.contains(&command.trim()); +}