Changed how the data is tokenised
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use super::strings;
|
||||
use crate::
|
||||
{
|
||||
// Internal code
|
||||
character,
|
||||
api,
|
||||
tokenise,
|
||||
UnwrapOrExit,
|
||||
//Libs
|
||||
Mutex,
|
||||
@@ -20,7 +20,7 @@ use crate::
|
||||
pub fn character_parse
|
||||
(
|
||||
index: usize,
|
||||
tokens: &Vec<&str>,
|
||||
tokens: &Vec<tokenise::Token>,
|
||||
character_name: String,
|
||||
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
||||
data_to_send: &Arc<Mutex<api::DataToSend>>,
|
||||
@@ -28,9 +28,11 @@ pub fn character_parse
|
||||
{
|
||||
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))?;
|
||||
let token = match tokens.get(sum_index) {
|
||||
Some(tokenise::Token::String(s)) => s.clone(),
|
||||
Some(_) => return Err(("Unexpected token".to_string(), sum_index + 1)),
|
||||
None => return Err(("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
|
||||
@@ -38,35 +40,26 @@ pub fn character_parse
|
||||
"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)),
|
||||
}
|
||||
sum_index += 1;
|
||||
let output = tokenise::get_string_token(tokens, sum_index)
|
||||
.map_err(|err| (err, index))?;
|
||||
debug!("Saying {output}");
|
||||
api::modify_data(data_to_send, "output".to_string(), output.to_string(), character_name, vec![]);
|
||||
},
|
||||
// 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)),
|
||||
};
|
||||
let feature = tokenise::get_string_token(tokens, sum_index)
|
||||
.map_err(|err| (err, index))?;
|
||||
sum_index += 1;
|
||||
let string = tokenise::get_string_token(tokens, sum_index)
|
||||
.map_err(|err| (err, 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)
|
||||
&& character.set_field(&feature, &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![]);
|
||||
@@ -76,9 +69,8 @@ pub fn character_parse
|
||||
"to"|"animate" =>
|
||||
{
|
||||
sum_index += 1;
|
||||
let content = tokens
|
||||
.get(sum_index)
|
||||
.ok_or_else(|| ("File unexpectedly reached termination point".to_string(), sum_index))?;
|
||||
let content = tokenise::get_string_token(tokens, sum_index)
|
||||
.map_err(|err| (err, 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
|
||||
|
||||
Reference in New Issue
Block a user