added a modify data function for changing the data_to_send mutex

This commit is contained in:
2026-05-16 12:40:00 +01:00
parent 9d2f615e16
commit a251be7827
6 changed files with 37 additions and 28 deletions
+20
View File
@@ -3,6 +3,7 @@ use crate::
{
// internal code
character,
UnwrapOrExit,
// libraries
json,
HashMap,
@@ -101,3 +102,22 @@ pub async fn api_process
.run(([127, 0, 0, 1],config::API_PORT))
.await;
}
// On fail, quit safely
// If successful, return nothing
pub fn modify_data
(
data_to_send: &Arc<Mutex<DataToSend>>,
action_type: String,
content: String,
character_name: String,
choices: Vec<String>,
)
{
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO eh?
data.action_type = action_type;
data.content = content;
data.character = character_name;
data.choices = choices;
drop(data);
}
+2 -7
View File
@@ -131,14 +131,9 @@ async fn main()
// Exit with error or success
Ok(()) =>
{
info!("Program exited successfully");
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO test
data.action_type = String::from("end");
data.content = String::new();
data.character = String::new();
// Manually unlock the mutex
std::mem::drop(data);
api::modify_data(&data_to_send, "end".to_string(), String::new(), String::new(), vec![]);
let _ = rx.recv(); // Wait for the client to respond
info!("Program exited successfully");
exit(0);
},
Err(error) =>
+1 -7
View File
@@ -12,7 +12,6 @@ use crate::
info,
debug,
warn,
UnwrapOrExit,
};
mod strings;
@@ -153,12 +152,7 @@ fn choice_parse
}
debug!("{choices:?}");
// Send the choices to the Client via the API
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2);
data.action_type = String::from("choice");
data.content = String::new();
data.character = String::new();
data.choices = choices;
drop(data);
api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices);
// Return the choice indeces
Ok((sum_index + 1, choice_indeces))
}
+10 -11
View File
@@ -44,12 +44,7 @@ pub fn character_parse
{
debug!("{output_string}");
sum_index += counter;
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned", 2);
data.action_type = String::from("output");
data.content = output_string;
data.character = character_name;
data.choices = vec![];
drop(data);
api::modify_data(data_to_send, "output".to_string(), output_string, character_name, vec![]);
},
None => return Err(("Unable to read output string".to_string(), sum_index)),
}
@@ -74,11 +69,15 @@ pub fn character_parse
&& character.set_field(feature, &output_string)
.is_err() { warn!("Feature {feature} does not exist") }
drop(characters);
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO eh?
data.action_type = String::from("change");
data.content = String::new();
data.character = character_name;
drop(data);
api::modify_data(data_to_send, "change".to_string(), String::new(), character_name, vec![]);
},
"to"|"animate" =>
{
sum_index += 1;
let content = tokens
.get(sum_index)
.ok_or_else(|| ("File unexpectedly reached termination point".to_string(), sum_index))?;
api::modify_data(data_to_send, token.to_lowercase(), content.to_string(), character_name, vec![]);
},
// Catch all condition, if the instruction is unrecognised as a
// character command