Implemented choices successfully
This commit is contained in:
+54
-36
@@ -26,67 +26,83 @@ pub async fn token_parse(
|
||||
) -> Result<(),String>
|
||||
{
|
||||
let mut index: usize = 0;
|
||||
if rx.recv().is_err()
|
||||
{
|
||||
warn!("Some issue with api");
|
||||
// TOD eh?
|
||||
};
|
||||
// Run an infinite loop
|
||||
loop
|
||||
'parse_loop: loop
|
||||
{
|
||||
// If the client hasn't responded then continue (after a short pause)
|
||||
if rx.recv().is_err()
|
||||
{
|
||||
continue
|
||||
}
|
||||
// Get the next token
|
||||
let token = tokens
|
||||
.get(index)
|
||||
.ok_or_else(|| "File reached termination point".to_string())?;
|
||||
|
||||
.ok_or_else(|| "File unexpectedly reached termination point".to_string())?;
|
||||
debug!("{}: {}",index, token);
|
||||
// The instructions are related to characters
|
||||
if token.starts_with('@')
|
||||
{
|
||||
let character_name: String = token.chars().skip(1).collect();
|
||||
info!("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, &characters, &data_to_send).await
|
||||
index = match character_parse(index+1, tokens, character_name, &characters, &data_to_send).await
|
||||
{
|
||||
Ok(increment) => increment,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
continue
|
||||
}
|
||||
// Miscelleneous instructions
|
||||
match token.to_lowercase().as_str()
|
||||
else
|
||||
{
|
||||
"end" =>
|
||||
match token.to_lowercase().as_str()
|
||||
{
|
||||
info!("END command, exiting");
|
||||
return Ok(()) // quit
|
||||
},
|
||||
"choice" =>
|
||||
{
|
||||
let (_,jump_points) = match choice_parse(index+1, &tokens, &data_to_send).await
|
||||
"end" =>
|
||||
{
|
||||
Ok((increment,jump_point)) => (increment,jump_point),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if rx.recv().is_err() { warn!("Error sending choices to client"); };
|
||||
let (_, choice) = rx.recv().unwrap(); // TODO eh
|
||||
index = jump_points[choice];
|
||||
debug!("{:?} {} {}",jump_points, choice, index);
|
||||
continue
|
||||
}
|
||||
_ =>
|
||||
{
|
||||
warn!("Invalid command: {}", token);
|
||||
}
|
||||
info!("END command, exiting");
|
||||
return Ok(()) // quit
|
||||
},
|
||||
"choice" =>
|
||||
{
|
||||
let (_,jump_points) = match choice_parse(index+1, tokens, &data_to_send).await
|
||||
{
|
||||
Ok((increment,jump_point)) => (increment,jump_point),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if rx.recv().is_err() { warn!("Error sending choices to client"); };
|
||||
let (_, choice) = rx.recv().unwrap(); // TODO eh
|
||||
index = jump_points[choice];
|
||||
debug!("{:?} {} {}",jump_points, choice, index);
|
||||
continue 'parse_loop
|
||||
},
|
||||
"or" =>
|
||||
{
|
||||
index += strings::closing_char(&tokens[index..], '{','}').unwrap(); // TODO eh
|
||||
continue
|
||||
},
|
||||
"}" =>
|
||||
{
|
||||
index += 1;
|
||||
continue
|
||||
},
|
||||
_ =>
|
||||
{
|
||||
warn!("Invalid command: {}", token);
|
||||
}
|
||||
};
|
||||
}
|
||||
if rx.recv().is_err()
|
||||
{
|
||||
warn!("Some issue with api");
|
||||
continue
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO underdev
|
||||
// Parse the options in a choice clause and returns the idexes of the code blocks
|
||||
async fn choice_parse
|
||||
(
|
||||
index: usize,
|
||||
tokens: &Vec<&str>,
|
||||
tokens: &[&str],
|
||||
data_to_send: &Arc<Mutex<api::DataToSend>>,
|
||||
) -> Result<(usize, Vec<usize>), String>
|
||||
{
|
||||
@@ -119,7 +135,8 @@ async fn choice_parse
|
||||
}
|
||||
|
||||
// Parsing character related instructions
|
||||
async fn character_parse
|
||||
// TODO only send relevant tokens
|
||||
#[allow(unused_variables)] async fn character_parse
|
||||
(
|
||||
index: usize,
|
||||
tokens: &Vec<&str>,
|
||||
@@ -146,6 +163,7 @@ async fn character_parse
|
||||
{
|
||||
debug!("{}", output_string);
|
||||
sum_index += counter;
|
||||
debug!("{}",sum_index);
|
||||
let mut data = data_to_send.lock().unwrap();
|
||||
data.action_type = String::from("output");
|
||||
data.content = output_string;
|
||||
@@ -157,7 +175,7 @@ async fn character_parse
|
||||
},
|
||||
// Catch all condition, if the instruction is unrecognised as a
|
||||
// character command
|
||||
_ => return Err(String::from(format!("Invalid command: {}", token))),
|
||||
_ => return Err(format!("Invalid command: {}", token)),
|
||||
}
|
||||
sum_index += 1;
|
||||
Ok(sum_index)
|
||||
|
||||
Reference in New Issue
Block a user