115 lines
2.9 KiB
Rust
115 lines
2.9 KiB
Rust
use std::collections::HashMap;
|
|
use std::time::Duration;
|
|
use std::thread;
|
|
mod strings;
|
|
use crate::
|
|
{
|
|
// Internal code
|
|
character,
|
|
api,
|
|
|
|
// Libraries
|
|
mpsc::Receiver,
|
|
Arc,
|
|
Mutex,
|
|
info,
|
|
warn,
|
|
debug,
|
|
};
|
|
|
|
|
|
// Parse the tokens in a file
|
|
// Returns success or an error string
|
|
pub async fn token_parse(
|
|
tokens: &Vec<&str>,
|
|
mut characters: &mut HashMap::<String, character::Character>,
|
|
data_to_send: Arc<Mutex<api::DataToSend>>,
|
|
rx: &Receiver<u8>,
|
|
) -> Result<(),String>
|
|
{
|
|
let mut index: usize = 0;
|
|
// Run an infinite loop
|
|
loop
|
|
{
|
|
// If the client hasn't responded then continue (after a short pause)
|
|
if rx.try_recv().is_err()
|
|
{
|
|
thread::sleep(Duration::from_millis(300));
|
|
continue
|
|
}
|
|
// Get the next token
|
|
let token = tokens
|
|
.get(index)
|
|
.ok_or_else(|| "File reached termination point".to_string())?;
|
|
|
|
// The instructions are related to characters
|
|
if token.starts_with('@')
|
|
{
|
|
let character_name: String = token.chars().skip(1).collect();
|
|
// If the character doesn't exist, then create it
|
|
if ! characters.contains_key(&character_name)
|
|
{
|
|
let new_character = character::Character::new(character_name.clone());
|
|
characters.insert(character_name.clone(),new_character);
|
|
}
|
|
println!("Doing something with a character: {}", character_name);
|
|
// The index is incremented to after the character's instructions
|
|
index += match character_parse(index+1, &tokens, character_name, &mut characters, &data_to_send, &rx).await
|
|
{
|
|
Ok(increment) => increment,
|
|
Err(error) => return Err(error),
|
|
}
|
|
}
|
|
// Miscelleneous instructions
|
|
if token.to_lowercase() == "end"
|
|
{
|
|
return Ok(()) // quit
|
|
}
|
|
index += 1;
|
|
}
|
|
}
|
|
// Parsing character related instructions
|
|
async fn character_parse
|
|
(
|
|
index: usize,
|
|
tokens: &Vec<&str>,
|
|
character_name: String,
|
|
characters: &mut HashMap::<String, character::Character>,
|
|
data_to_send: &Arc<Mutex<api::DataToSend>>,
|
|
rx: &Receiver<u8>,
|
|
) -> Result<usize,String>
|
|
{
|
|
let mut sum_index: usize = index;
|
|
loop
|
|
{
|
|
// 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())?;
|
|
match token.to_lowercase().as_str()
|
|
{
|
|
// The character is saying something, so grab the text and pass it
|
|
// to the client
|
|
"says" =>
|
|
{
|
|
match strings::extract_quoted(&tokens[sum_index+1..]) // TODO increment to after the string
|
|
{
|
|
Some(output_string) =>
|
|
{
|
|
debug!("{}", output_string);
|
|
let mut data = data_to_send.lock().unwrap();
|
|
data.action_type = String::from("output");
|
|
data.content = output_string;
|
|
data.character = character_name.clone();
|
|
},
|
|
None => return Err(String::from("Unable to read string")),
|
|
}
|
|
},
|
|
// Catch all condition, if the instruction is unrecognised as a
|
|
// character command
|
|
_ => return Err(String::from(format!("Invalid command: {}", token))),
|
|
}
|
|
sum_index += 1;
|
|
}
|
|
}
|