moved character parsing to it's own file and made it return data to the API
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
use super::strings;
|
||||
use crate::
|
||||
{
|
||||
// Internal code
|
||||
character,
|
||||
api,
|
||||
//Libs
|
||||
Mutex,
|
||||
Arc,
|
||||
HashMap,
|
||||
info,
|
||||
warn,
|
||||
debug,
|
||||
};
|
||||
|
||||
// Parsing character related instructions
|
||||
// TODO only send relevant tokens
|
||||
#[allow(unused_variables)]
|
||||
pub fn character_parse
|
||||
(
|
||||
index: usize,
|
||||
tokens: &Vec<&str>,
|
||||
character_name: String,
|
||||
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
||||
data_to_send: &Arc<Mutex<api::DataToSend>>,
|
||||
) -> Result<usize,(String,usize)>
|
||||
{
|
||||
let mut sum_index: usize = index;
|
||||
// Ensure the index is valid (the index is not beyond the vector)
|
||||
let token = tokens
|
||||
.get(sum_index)
|
||||
.ok_or_else(|| ("File unexpectedly reached termination point".to_string(), sum_index))?;
|
||||
match token.to_lowercase().as_str()
|
||||
{
|
||||
// The character is saying something, so grab the text and pass it
|
||||
// to the client
|
||||
"says" =>
|
||||
{
|
||||
info!("SAYS command with character {character_name}");
|
||||
match strings::extract_quoted(&tokens[sum_index+1..])
|
||||
{
|
||||
Some((output_string, counter)) =>
|
||||
{
|
||||
debug!("{output_string}");
|
||||
sum_index += counter;
|
||||
let mut data = data_to_send.lock().unwrap();
|
||||
data.action_type = String::from("output");
|
||||
data.content = output_string;
|
||||
data.character = character_name;
|
||||
data.choices = vec![];
|
||||
},
|
||||
None => return Err(("Unable to read output string".to_string(), sum_index)),
|
||||
}
|
||||
},
|
||||
// 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(), sum_index))?;
|
||||
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(),sum_index)),
|
||||
};
|
||||
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(); // TODO eh?
|
||||
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}"),sum_index)),
|
||||
}
|
||||
sum_index += 1;
|
||||
Ok(sum_index)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user