Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewind #49

Open
wants to merge 7 commits into
base: other-texts-support-for-i18n
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions cli/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ fn run_command(command: &str, parameters: Vec<&str>, runtime: &mut Runtime) -> S
"skip" | "s" => skip(runtime),
"skip-all" => skip_all(runtime),
"reset" => reset_command(parameters, runtime),
"rewind" => rewind_command(parameters, runtime),
"rewind_to_choice" => rewind_to_choice_command(runtime),
"rewind_to" => rewind_to(parameters, runtime),
str => {
if str.starts_with("->") {
let substr: String = str.chars().skip(2).collect();
Expand Down Expand Up @@ -310,6 +313,59 @@ fn parameters_to_pattern(parameters: Vec<&str>) -> String {
pattern.trim().to_string()
}

fn rewind_command(parameters: Vec<&str>, runtime: &mut Runtime) -> String {
if parameters.len() > 1 {
return "Invalid parameters".to_string();
}

if !parameters.is_empty() {
if let Ok(rewind_count) = parameters[0].parse() {
for _i in 0..rewind_count {
runtime.rewind().unwrap();
}
} else {
return "Invalid parameters".to_string();
}
} else {
runtime.rewind().unwrap();
}

match runtime.current() {
Ok(current) => get_output_string(current, runtime),
Err(err) => get_runtime_error_string(err, runtime),
}
}

fn rewind_to_choice_command(runtime: &mut Runtime) -> String {
runtime.rewind_to_choice().unwrap();

match runtime.current() {
Ok(current) => get_output_string(current, runtime),
Err(err) => get_runtime_error_string(err, runtime),
}
}

fn rewind_to(parameters: Vec<&str>, runtime: &mut Runtime) -> String {
if parameters.len() > 1 {
return "Invalid parameters".to_string();
}

if !parameters.is_empty() {
if let Ok(index) = parameters[0].parse() {
runtime.rewind_to(index).unwrap();
} else {
return "Invalid parameters".to_string();
}
} else {
return "Missing parameter index".to_string();
}

match runtime.current() {
Ok(current) => get_output_string(current, runtime),
Err(err) => get_runtime_error_string(err, runtime),
}
}

fn reset_command(parameters: Vec<&str>, runtime: &mut Runtime) -> String {
if parameters.is_empty()
|| parameters.contains(&"all")
Expand Down Expand Up @@ -777,4 +833,57 @@ mod test {
let str_found = Console::process_line(Ok("next".to_string()), &mut rl, &mut runtime).unwrap();
assert_eq!(expected_str, &str_found);
}

#[test]
fn rewind_command() {
let mut runtime = Console::load_runtime("./fixtures/script");
runtime.database.config.keep_history = true;
runtime.next_block().unwrap();
runtime.next_block().unwrap();

let mut rl = DefaultEditor::new().unwrap();
let expected_str = "You've just arrived in the bustling city, full of excitement and anticipation for your new job.";

let str_found = Console::process_line(Ok("rewind".to_string()), &mut rl, &mut runtime).unwrap();
assert_eq!(expected_str, &str_found);

runtime.next_block().unwrap();
runtime.next_block().unwrap();

let str_found =
Console::process_line(Ok("rewind 2".to_string()), &mut rl, &mut runtime).unwrap();
assert_eq!(expected_str, &str_found);
}

#[test]
fn rewind_to_choice_command() {
let mut runtime = Console::load_runtime("./fixtures/script");
runtime.database.config.keep_history = true;
runtime.skip().unwrap();
runtime.pick_choice(0).unwrap();

let mut rl = DefaultEditor::new().unwrap();
let expected_str = "As you take your first steps in this urban jungle, you feel a mix of emotions, hoping to find your place in this new environment.\n (1)I take a walk through a nearby park to relax and acclimate to the city.\n (2)I visit a popular street market to experience the city's unique flavors and energy.\n";

let str_found =
Console::process_line(Ok("rewind_to_choice".to_string()), &mut rl, &mut runtime).unwrap();
assert_eq!(expected_str, &str_found);
}

#[test]
fn rewind_to_command() {
let mut runtime = Console::load_runtime("./fixtures/script");
runtime.database.config.keep_history = true;
runtime.next_block().unwrap();
runtime.next_block().unwrap();
runtime.next_block().unwrap();

let mut rl = DefaultEditor::new().unwrap();
let expected_str =
"The skyline reaches for the clouds, and the sounds of traffic and people surround you.";

let str_found =
Console::process_line(Ok("rewind_to 1".to_string()), &mut rl, &mut runtime).unwrap();
assert_eq!(expected_str, &str_found);
}
}
2 changes: 2 additions & 0 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Config {
pub other_texts: HashMap<String, String>,
#[serde(default)]
pub story_progress_style: StoryProgressStyle,
#[serde(default)]
pub keep_history: bool,
}

#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Clone, Eq)]
Expand Down
1 change: 1 addition & 0 deletions examples/cuentitos.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
locales = ['en']
default_locale = 'en'
story_progress_style = 'next'
keep_history = true

[variables]
energy = "integer"
Expand Down
22 changes: 22 additions & 0 deletions runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type VariableName = String;

#[derive(PartialEq, Eq)]
pub enum RuntimeError {
MissingLocale(String),
RewindWithNoHistory(),
RewindWithToInvalidIndex {
index: usize,
current_index: usize,
},
InvalidBlockId(BlockId),
WaitingForChoice(Vec<String>),
StoryFinished,
Expand Down Expand Up @@ -131,6 +137,22 @@ impl Display for RuntimeError {
RuntimeError::FrequencyOutOfBucket => {
write!(f, "Frequencies are only allowed inside of buckets.")
}
RuntimeError::RewindWithNoHistory() => {
write!(f, "Can't rewind when the history is empty.")
}
RuntimeError::MissingLocale(s) => {
write!(f, "Missing locale: {}", s)
}
RuntimeError::RewindWithToInvalidIndex {
index,
current_index,
} => {
write!(
f,
"Can't rewind to {} because the current index is {}.",
index, current_index
)
}
}
}
}
Loading
Loading