began implementing choice responses server side

This commit is contained in:
2026-05-07 23:34:37 +01:00
parent ffe1c75947
commit 1d2966a494
7 changed files with 44 additions and 14 deletions
-1
View File
@@ -1 +0,0 @@
,deadvey,linux-pc,03.05.2026 02:13,file:///home/deadvey/.config/libreoffice/4;
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
+17 -7
View File
@@ -20,6 +20,7 @@ pub struct DataToSend {
pub action_type: String, pub action_type: String,
pub content: String, pub content: String,
pub character: String, pub character: String,
pub choices: Vec<String>,
} }
// Async function that runs the api server in the background. // Async function that runs the api server in the background.
@@ -29,27 +30,28 @@ pub async fn api_process
( (
data_to_send: Arc<Mutex<DataToSend>>, data_to_send: Arc<Mutex<DataToSend>>,
characters: Arc<Mutex<HashMap::<String,character::Character>>>, characters: Arc<Mutex<HashMap::<String,character::Character>>>,
tx: Sender<u8>, tx: Sender<(bool, usize)>,
) )
{ {
// This data must be passed through to the api route in order to be used // 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 data_filter = warp::any().map(move || Arc::clone(&data_to_send));
let characters_filter = warp::any().map(move || Arc::clone(&characters)); let characters_filter = warp::any().map(move || Arc::clone(&characters));
let tx_filter = warp::any().map(move || tx.clone()); let tx_filter = warp::any().map(move || tx.clone());
let tx_filter2 = tx_filter.clone();
info!("Running server"); info!("Running server");
// The server route is loaded at address:port/happening // The server route is loaded at address:port/happening
let main = warp::path("happening") let main = warp::path("happening")
.and(warp::get()) .and(warp::get())
.and(data_filter) .and(data_filter)
.and(tx_filter) .and(tx_filter)
// Perform this code on a GET request // Perform this code on a GET request
.map(|state: Arc<Mutex<DataToSend>>, tx_handle: Sender<u8>| .map(|state: Arc<Mutex<DataToSend>>, tx_handle: Sender<(bool,usize)>|
{ {
info!("GET: {:?}", state); info!("GET: {:?}", state);
let reply = state.as_ref(); 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) warp::reply::json(&reply) // Send the reply data (data_to_send formatted as JSON)
}).boxed(); }).boxed();
let characters = warp::path("character") let characters = warp::path("character")
@@ -60,10 +62,18 @@ pub async fn api_process
{ {
let map = characters.lock().unwrap(); // TODO remove unwrap let map = characters.lock().unwrap(); // TODO remove unwrap
let reply = map.get(&name).unwrap(); let reply = map.get(&name).unwrap();
debug!("GET: name: {}, data: {:?}", name, reply); debug!("GET: name: {}", name);
warp::reply::json(&reply) warp::reply::json(&reply)
}).boxed(); }).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); let routes = main.or(characters);
// Start the server // Start the server
warp::serve(routes) warp::serve(routes)
+2
View File
@@ -31,6 +31,7 @@ async fn main()
action_type: "begin".to_string(), action_type: "begin".to_string(),
content: "".to_string(), content: "".to_string(),
character: "".to_string(), character: "".to_string(),
choices: vec![],
})); }));
// Setup the characters hashmap which will store each character in it as a Character struct // Setup the characters hashmap which will store each character in it as a Character struct
let mut characters: Arc<Mutex<HashMap::<String, character::Character>>> = Default::default(); let mut characters: Arc<Mutex<HashMap::<String, character::Character>>> = Default::default();
@@ -73,6 +74,7 @@ async fn main()
{ {
warn!("No END statement, story may exit unexpectedly"); warn!("No END statement, story may exit unexpectedly");
} }
debug!("{:?}", tokens);
let data_clone2 = Arc::clone(&data_to_send); let data_clone2 = Arc::clone(&data_to_send);
let characters_clone2 = Arc::clone(&characters); let characters_clone2 = Arc::clone(&characters);
+25 -6
View File
@@ -22,7 +22,7 @@ pub async fn token_parse(
tokens: &Vec<&str>, tokens: &Vec<&str>,
characters: Arc<Mutex<HashMap::<String, character::Character>>>, characters: Arc<Mutex<HashMap::<String, character::Character>>>,
data_to_send: Arc<Mutex<api::DataToSend>>, data_to_send: Arc<Mutex<api::DataToSend>>,
rx: &Receiver<u8>, rx: &Receiver<(bool,usize)>,
) -> Result<(),String> ) -> Result<(),String>
{ {
let mut index: usize = 0; let mut index: usize = 0;
@@ -62,7 +62,7 @@ pub async fn token_parse(
}, },
"choice" => "choice" =>
{ {
index += match choice_parse(index+1, &tokens).await index += match choice_parse(index+1, &tokens, &data_to_send).await
{ {
Ok(increment) => increment, Ok(increment) => increment,
Err(error) => return Err(error), Err(error) => return Err(error),
@@ -82,17 +82,35 @@ async fn choice_parse
( (
index: usize, index: usize,
tokens: &Vec<&str>, tokens: &Vec<&str>,
) -> Result<usize, String> data_to_send: &Arc<Mutex<api::DataToSend>>,
) -> Result<(usize, Vec<usize>), String>
{ {
let mut sum_index: usize = index; let mut sum_index: usize = index;
let mut choices: Vec<String> = Vec::new(); let mut choices: Vec<String> = Vec::new();
let mut choice_indeces: Vec<usize> = Vec::new();
// Ensure the index is valid (the index is not beyond the vector) // Ensure the index is valid (the index is not beyond the vector)
let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index..]) let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index..])
.ok_or_else(|| "No choice string".to_string())?; .ok_or_else(|| "No choice string".to_string())?;
sum_index += counter; sum_index += counter;
choices.append(choice_string); choices.push(choice_string);
sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap(); //TODO eh choice_indeces.push(sum_index+1);
Ok(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 // Parsing character related instructions
@@ -127,6 +145,7 @@ async fn character_parse
data.action_type = String::from("output"); data.action_type = String::from("output");
data.content = output_string; data.content = output_string;
data.character = character_name.clone(); data.character = character_name.clone();
data.choices = vec![];
}, },
None => return Err(String::from("Unable to read string")), None => return Err(String::from("Unable to read string")),
} }