diff --git a/README.md b/README.md index ab94fb2..8084518 100644 --- a/README.md +++ b/README.md @@ -165,9 +165,9 @@ GOTO label | TO | No | | ANIMATE | No | ### Other Features -| Feature | Implemented | -| ------- | ----------- | -| Variables | No | +| Feature | Implemented | +| --------- | ----------- | +| Variables | No | ## Error codes | | | | diff --git a/server/src/api.rs b/server/src/api.rs index b741011..cca0e7e 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -3,6 +3,7 @@ use crate:: { // internal code character, + UnwrapOrExit, // libraries json, HashMap, @@ -101,3 +102,22 @@ pub async fn api_process .run(([127, 0, 0, 1],config::API_PORT)) .await; } + +// On fail, quit safely +// If successful, return nothing +pub fn modify_data +( + data_to_send: &Arc>, + action_type: String, + content: String, + character_name: String, + choices: Vec, +) +{ + let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO eh? + data.action_type = action_type; + data.content = content; + data.character = character_name; + data.choices = choices; + drop(data); +} diff --git a/server/src/main.rs b/server/src/main.rs index 012db22..6b5edd6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -131,14 +131,9 @@ async fn main() // Exit with error or success Ok(()) => { - info!("Program exited successfully"); - let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO test - data.action_type = String::from("end"); - data.content = String::new(); - data.character = String::new(); - // Manually unlock the mutex - std::mem::drop(data); + api::modify_data(&data_to_send, "end".to_string(), String::new(), String::new(), vec![]); let _ = rx.recv(); // Wait for the client to respond + info!("Program exited successfully"); exit(0); }, Err(error) => diff --git a/server/src/parsing.rs b/server/src/parsing.rs index b585c8e..9b769d4 100644 --- a/server/src/parsing.rs +++ b/server/src/parsing.rs @@ -12,7 +12,6 @@ use crate:: info, debug, warn, - UnwrapOrExit, }; mod strings; @@ -153,12 +152,7 @@ fn choice_parse } debug!("{choices:?}"); // Send the choices to the Client via the API - let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); - data.action_type = String::from("choice"); - data.content = String::new(); - data.character = String::new(); - data.choices = choices; - drop(data); + api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices); // Return the choice indeces Ok((sum_index + 1, choice_indeces)) } diff --git a/server/src/parsing/character_parse.rs b/server/src/parsing/character_parse.rs index 68d5a94..14cb7f2 100644 --- a/server/src/parsing/character_parse.rs +++ b/server/src/parsing/character_parse.rs @@ -44,12 +44,7 @@ pub fn character_parse { debug!("{output_string}"); sum_index += counter; - let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned", 2); - data.action_type = String::from("output"); - data.content = output_string; - data.character = character_name; - data.choices = vec![]; - drop(data); + api::modify_data(data_to_send, "output".to_string(), output_string, character_name, vec![]); }, None => return Err(("Unable to read output string".to_string(), sum_index)), } @@ -74,11 +69,15 @@ pub fn character_parse && character.set_field(feature, &output_string) .is_err() { warn!("Feature {feature} does not exist") } drop(characters); - let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO eh? - data.action_type = String::from("change"); - data.content = String::new(); - data.character = character_name; - drop(data); + api::modify_data(data_to_send, "change".to_string(), String::new(), character_name, vec![]); + }, + "to"|"animate" => + { + sum_index += 1; + let content = tokens + .get(sum_index) + .ok_or_else(|| ("File unexpectedly reached termination point".to_string(), sum_index))?; + api::modify_data(data_to_send, token.to_lowercase(), content.to_string(), character_name, vec![]); }, // Catch all condition, if the instruction is unrecognised as a // character command diff --git a/stories/story.ha b/stories/story.ha index 84a6ac7..0e9ea1f 100644 --- a/stories/story.ha +++ b/stories/story.ha @@ -1,5 +1,6 @@ @tim says "hello world, it's a good day" @tim change name "Timothy Fineshooter" +@tim to fr choice "choice numero uno" { @tim says "super sad" }