diff --git a/.~lock.report.odt# b/.~lock.report.odt# deleted file mode 100644 index 76dbfef..0000000 --- a/.~lock.report.odt# +++ /dev/null @@ -1 +0,0 @@ -,deadvey,linux-pc,03.05.2026 02:13,file:///home/deadvey/.config/libreoffice/4; \ No newline at end of file diff --git a/report.odt b/report.odt index dc19a13..eefef77 100644 Binary files a/report.odt and b/report.odt differ diff --git a/src/.api.rs.swp b/src/.api.rs.swp new file mode 100644 index 0000000..4314426 Binary files /dev/null and b/src/.api.rs.swp differ diff --git a/src/.parsing.rs.swp b/src/.parsing.rs.swp index 9ef50d4..3b978e8 100644 Binary files a/src/.parsing.rs.swp and b/src/.parsing.rs.swp differ diff --git a/src/api.rs b/src/api.rs index 5d2e1cf..a506f0f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -20,6 +20,7 @@ pub struct DataToSend { pub action_type: String, pub content: String, pub character: String, + pub choices: Vec, } // Async function that runs the api server in the background. @@ -29,27 +30,28 @@ pub async fn api_process ( data_to_send: Arc>, characters: Arc>>, - tx: Sender, + tx: Sender<(bool, usize)>, ) { // This data must be passed through to the api route in order to be used let data_filter = warp::any().map(move || Arc::clone(&data_to_send)); let characters_filter = warp::any().map(move || Arc::clone(&characters)); let tx_filter = warp::any().map(move || tx.clone()); - + let tx_filter2 = tx_filter.clone(); + info!("Running server"); - + // The server route is loaded at address:port/happening let main = warp::path("happening") .and(warp::get()) .and(data_filter) .and(tx_filter) // Perform this code on a GET request - .map(|state: Arc>, tx_handle: Sender| + .map(|state: Arc>, tx_handle: Sender<(bool,usize)>| { info!("GET: {:?}", state); let reply = state.as_ref(); - let _ = tx_handle.send(1); + let _ = tx_handle.send((true,0)); warp::reply::json(&reply) // Send the reply data (data_to_send formatted as JSON) }).boxed(); let characters = warp::path("character") @@ -60,10 +62,18 @@ pub async fn api_process { let map = characters.lock().unwrap(); // TODO remove unwrap let reply = map.get(&name).unwrap(); - debug!("GET: name: {}, data: {:?}", name, reply); + debug!("GET: name: {}", name); warp::reply::json(&reply) }).boxed(); - + let choice = warp::path("choice") + .and(warp::post()) + .and(warp::body::json()) + .and(tx_filter2) + .map(|index: usize, tx_handle: Sender<(bool,usize)>| { + debug!("Choice: {}", index); + let _ = tx_handle.send((true,index)); + }).boxed(); + let routes = main.or(characters); // Start the server warp::serve(routes) diff --git a/src/main.rs b/src/main.rs index 4cd9999..f0a5125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ async fn main() action_type: "begin".to_string(), content: "".to_string(), character: "".to_string(), + choices: vec![], })); // Setup the characters hashmap which will store each character in it as a Character struct let mut characters: Arc>> = Default::default(); @@ -73,6 +74,7 @@ async fn main() { warn!("No END statement, story may exit unexpectedly"); } + debug!("{:?}", tokens); let data_clone2 = Arc::clone(&data_to_send); let characters_clone2 = Arc::clone(&characters); diff --git a/src/parsing.rs b/src/parsing.rs index 21a84b1..aad0b35 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -22,7 +22,7 @@ pub async fn token_parse( tokens: &Vec<&str>, characters: Arc>>, data_to_send: Arc>, - rx: &Receiver, + rx: &Receiver<(bool,usize)>, ) -> Result<(),String> { let mut index: usize = 0; @@ -62,7 +62,7 @@ pub async fn token_parse( }, "choice" => { - index += match choice_parse(index+1, &tokens).await + index += match choice_parse(index+1, &tokens, &data_to_send).await { Ok(increment) => increment, Err(error) => return Err(error), @@ -82,17 +82,35 @@ async fn choice_parse ( index: usize, tokens: &Vec<&str>, -) -> Result + data_to_send: &Arc>, +) -> Result<(usize, Vec), String> { let mut sum_index: usize = index; let mut choices: Vec = Vec::new(); + let mut choice_indeces: Vec = Vec::new(); // Ensure the index is valid (the index is not beyond the vector) let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index..]) .ok_or_else(|| "No choice string".to_string())?; sum_index += counter; - choices.append(choice_string); - sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap(); //TODO eh - Ok(sum_index + 1) + choices.push(choice_string); + choice_indeces.push(sum_index+1); + sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap() + 1; //TODO eh + while tokens[sum_index].to_lowercase() == "or" + { + let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index+1..]) + .ok_or_else(|| "No choice string".to_string())?; + sum_index += counter; + choices.push(choice_string); + choice_indeces.push(sum_index+2); + sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap() + 1; //TODO eh + } + debug!("{:?}", choices); + let mut data = data_to_send.lock().unwrap(); + data.action_type = String::from("choice"); + data.content = "".to_string(); + data.character = "".to_string(); + data.choices = choices; //TODO + Ok((sum_index + 1, choice_indeces)) } // Parsing character related instructions @@ -127,6 +145,7 @@ async fn character_parse data.action_type = String::from("output"); data.content = output_string; data.character = character_name.clone(); + data.choices = vec![]; }, None => return Err(String::from("Unable to read string")), }