Changed the data_to_send to be a stack so many lines of code can
be pre-processed before the user interacts. When the /happening api is called it just dequeues the front item
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
use crate::
|
||||
{
|
||||
tokenise,
|
||||
character,
|
||||
api,
|
||||
|
||||
HashMap,
|
||||
Arc,
|
||||
Mutex,
|
||||
VecDeque,
|
||||
warn,
|
||||
debug,
|
||||
info,
|
||||
mpsc::Receiver,
|
||||
};
|
||||
|
||||
pub fn keyword_parse(
|
||||
tokens: &[tokenise::Token],
|
||||
token: String,
|
||||
mut index: usize,
|
||||
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
||||
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
||||
labels: &HashMap<String, usize>,
|
||||
rx: &Receiver<(usize,String)>,
|
||||
)
|
||||
-> Result<usize, String>
|
||||
{
|
||||
|
||||
match token.to_lowercase().as_str()
|
||||
{
|
||||
"choice" =>
|
||||
{
|
||||
let choice_indeces = choice_parse(tokens, index, happening_queue)?;
|
||||
debug!("{choice_indeces:?}");
|
||||
let choice = match rx.recv()
|
||||
{
|
||||
Ok((choice,_)) => choice_indeces[choice],
|
||||
Err(err) =>
|
||||
{
|
||||
warn!("Error receiving choice from client, defaulting to choice 0 {err}");
|
||||
0
|
||||
}
|
||||
};
|
||||
index = choice;
|
||||
},
|
||||
"or" =>
|
||||
{
|
||||
info!("OR command, jumping over");
|
||||
index += 2;
|
||||
let new_index = tokenise::get_closing_index(tokens, index)?;
|
||||
index = new_index;
|
||||
},
|
||||
// Jump to a particular index based on a label eg GOTO character_check
|
||||
"goto" =>
|
||||
{
|
||||
index += 1;
|
||||
let label = tokenise::get_keyword_token(tokens, index)?;
|
||||
index = if let Some(label_index) = labels.get(&label) { *label_index }
|
||||
else
|
||||
{
|
||||
warn!("Label {label} does not exist");
|
||||
index + 1
|
||||
};
|
||||
debug!("Jumping to {index}");
|
||||
}
|
||||
_ =>
|
||||
{
|
||||
warn!("Invalid command: {token}");
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
fn choice_parse(tokens: &[tokenise::Token], mut index: usize, happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,)
|
||||
-> Result<Vec<usize>, String>
|
||||
{
|
||||
let mut next_token: String = "or".to_string();
|
||||
let mut choices: Vec<String> = Vec::new();
|
||||
let mut choice_indeces: Vec<usize> = Vec::new();
|
||||
while next_token == "or"
|
||||
{
|
||||
index += 1;
|
||||
choices.push
|
||||
(
|
||||
tokenise::get_string_token(tokens, index)?
|
||||
);
|
||||
index += 1;
|
||||
choice_indeces.push(index+1);
|
||||
index = match tokenise::get_closing_index(tokens,index)
|
||||
{
|
||||
Ok(new_index) => new_index + 1,
|
||||
Err(_) => break,
|
||||
};
|
||||
next_token = match tokenise::get_keyword_token(tokens, index)
|
||||
{
|
||||
Ok(string) => string,
|
||||
Err(_) => break,
|
||||
}
|
||||
};
|
||||
api::modify_data(happening_queue, "choice".to_string(), String::new(), String::new(), choices);
|
||||
Ok(choice_indeces)
|
||||
}
|
||||
Reference in New Issue
Block a user