6d012dbe6b
of Character
92 lines
3.0 KiB
Rust
92 lines
3.0 KiB
Rust
use super::strings;
|
|
use crate::
|
|
{
|
|
// Internal code
|
|
character,
|
|
api,
|
|
UnwrapOrExit,
|
|
//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;
|
|
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)),
|
|
}
|
|
},
|
|
// 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().unwrap_or_exit("Character Mutex was poisoned",3);
|
|
if let Some(character) = characters.get_mut(&character_name)
|
|
&& character.set_field(feature, &output_string)
|
|
.is_err() { warn!("Feature {feature} does not exist") }
|
|
drop(characters);
|
|
api::modify_data(data_to_send, "change".to_string(), String::new(), character_name, vec![]);
|
|
},
|
|
// These two are mainly just actions performed by the frontend client, so just tell the client to move/animate
|
|
// the character and not much other processing needed on the serverside
|
|
"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
|
|
_ => return Err((format!("Invalid command: {token}"),sum_index)),
|
|
}
|
|
sum_index += 1;
|
|
Ok(sum_index)
|
|
}
|
|
|