diff --git a/server/report.odt b/server/report.odt deleted file mode 100644 index eefef77..0000000 Binary files a/server/report.odt and /dev/null differ diff --git a/server/src/api.rs b/server/src/api.rs index f38b73a..5bee681 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -4,6 +4,7 @@ use crate:: // internal code character, // libraries + json, HashMap, Arc, Mutex, @@ -11,6 +12,7 @@ use crate:: mpsc::Sender, info, debug, + warn, Serialize, Deserialize, }; @@ -60,8 +62,21 @@ pub async fn api_process .and(characters_filter) .map(|name: String, characters: Arc>>| { - let map = characters.lock().unwrap(); // TODO eh - let reply = map.get(&name).unwrap(); // TODO eh + let characters = match characters.lock() + { + Ok(data) => data, + Err(poisoned) => + { + warn!("Character Mutex is poisoned"); + poisoned.into_inner() + }, + }; + let Some(reply) = characters.get(&name) + else + { + warn!("Client requested character that does not exist"); + return warp::reply::json(&json!({"reply": "invalid character"})); + }; debug!("GET: name: {name}"); warp::reply::json(&reply) }).boxed(); diff --git a/server/src/character.rs b/server/src/character.rs index 2fd8e8e..2b7d601 100644 --- a/server/src/character.rs +++ b/server/src/character.rs @@ -31,6 +31,34 @@ pub struct Character bottom_clothing: String, shoes: String, } +impl Character { + pub fn set_field(&mut self, field: &str, value: &str) -> Result<(), ()> { + match field { + "name" => self.name = value.to_string(), + "gender" => self.gender = value.to_string(), + "eye_color" => self.eye_color = value.to_string(), + "pronoun_subject" => self.pronoun_subject = value.to_string(), + "pronoun_object" => self.pronoun_object = value.to_string(), + "pronoun_deppos" => self.pronoun_deppos = value.to_string(), + "pronoun_indpos" => self.pronoun_indpos = value.to_string(), + "pronoun_reflex" => self.pronoun_reflex = value.to_string(), + "animation" => self.animation = value.to_string(), + "head" => self.head = value.to_string(), + "hair" => self.hair = value.to_string(), + "torso" => self.torso = value.to_string(), + "arm" => self.arm = value.to_string(), + "leg" => self.leg = value.to_string(), + "hair_color" => self.hair_color = value.to_string(), + "top_clothing" => self.top_clothing = value.to_string(), + "bottom_clothing" => self.bottom_clothing = value.to_string(), + "shoes" => self.shoes = value.to_string(), + + _ => return Err(()), + } + + Ok(()) + } +} pub fn character_parse(archive: &mut ZipArchive) -> Result>>,String> diff --git a/server/src/main.rs b/server/src/main.rs index cad2003..e1a6658 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -19,6 +19,7 @@ use serde:: Deserialize, Serialize }; +use serde_json::json; use zip:: { ZipArchive, diff --git a/server/src/parsing.rs b/server/src/parsing.rs index d8888c7..8670c3e 100644 --- a/server/src/parsing.rs +++ b/server/src/parsing.rs @@ -165,7 +165,7 @@ fn character_parse // Ensure the index is valid (the index is not beyond the vector) let token = tokens .get(sum_index) - .ok_or_else(|| "File reached termination point".to_string())?; + .ok_or_else(|| "File unexpectedly reached termination point".to_string())?; match token.to_lowercase().as_str() { // The character is saying something, so grab the text and pass it @@ -173,7 +173,7 @@ fn character_parse "says" => { info!("SAYS command with character {character_name}"); - match strings::extract_quoted(&tokens[sum_index+1..]) // TODO increment to after the string + match strings::extract_quoted(&tokens[sum_index+1..]) { Some((output_string, counter)) => { @@ -185,9 +185,35 @@ fn character_parse data.character = character_name; data.choices = vec![]; }, - None => return Err(String::from("Unable to read string")), + None => return Err("Unable to read output string".to_string()), } }, + // Change the property of the selected character eg @tim CHANGE name "Bill Buffins" + // will change the character with ID tim to "Bill Buffins"; a character's ID cannot change + "change" => + { + sum_index += 1; + let feature = tokens + .get(sum_index) + .ok_or_else(|| "File unexpectedly reached termination point".to_string())?; + let output_string: String; + (output_string, sum_index) = match strings::extract_quoted(&tokens[sum_index+1..]) + { + Some((string,counter)) => (string,sum_index+counter), + None => return Err("Unable to parse property to change character".to_string()), + }; + info!("CHANGE command with character {character_name} feature {feature}"); + let mut characters = characters.lock().expect("Data cannot be unlocked"); + if let Some(character) = characters.get_mut(&character_name) + { + if character.set_field(feature, &output_string) + .is_err() { warn!("Feature {feature} does not exist") }; + } + let mut data = data_to_send.lock().unwrap(); + data.action_type = String::from("change"); + data.content = String::new(); + data.character = character_name; + }, // Catch all condition, if the instruction is unrecognised as a // character command _ => return Err(format!("Invalid command: {token}")), diff --git a/stories/characters.json b/stories/characters.json index a5ec8d4..c120f30 100644 --- a/stories/characters.json +++ b/stories/characters.json @@ -1,14 +1,14 @@ { "tim": { "name": "Timothy Sharpshooter", - "gender": "", + "gender": "Male", "skin_color": "", "eye_color": "", - "pronoun_subject": "", - "pronoun_object": "", - "pronoun_deppos": "", - "pronoun_indpos": "", - "pronoun_reflex": "", + "pronoun_subject": "He", + "pronoun_object": "Him", + "pronoun_deppos": "His", + "pronoun_indpos": "His", + "pronoun_reflex": "Himself", "animation": "", "head": "", "hair": "", diff --git a/stories/no-characters.zip b/stories/no-characters.zip deleted file mode 100644 index dd9ff21..0000000 Binary files a/stories/no-characters.zip and /dev/null differ diff --git a/stories/no-story.zip b/stories/no-story.zip deleted file mode 100644 index 131cadc..0000000 Binary files a/stories/no-story.zip and /dev/null differ diff --git a/stories/nothing.zip b/stories/nothing.zip deleted file mode 100644 index 6703614..0000000 Binary files a/stories/nothing.zip and /dev/null differ diff --git a/stories/story.ha b/stories/story.ha index a8799fb..84a6ac7 100644 --- a/stories/story.ha +++ b/stories/story.ha @@ -1,4 +1,5 @@ @tim says "hello world, it's a good day" +@tim change name "Timothy Fineshooter" choice "choice numero uno" { @tim says "super sad" } diff --git a/stories/test.zip b/stories/test.zip index 3575183..2fed764 100644 Binary files a/stories/test.zip and b/stories/test.zip differ