added a modify data function for changing the data_to_send mutex
This commit is contained in:
@@ -165,9 +165,9 @@ GOTO label
|
|||||||
| TO | No |
|
| TO | No |
|
||||||
| ANIMATE | No |
|
| ANIMATE | No |
|
||||||
### Other Features
|
### Other Features
|
||||||
| Feature | Implemented |
|
| Feature | Implemented |
|
||||||
| ------- | ----------- |
|
| --------- | ----------- |
|
||||||
| Variables | No |
|
| Variables | No |
|
||||||
## Error codes
|
## Error codes
|
||||||
|
|
||||||
| | | |
|
| | | |
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use crate::
|
|||||||
{
|
{
|
||||||
// internal code
|
// internal code
|
||||||
character,
|
character,
|
||||||
|
UnwrapOrExit,
|
||||||
// libraries
|
// libraries
|
||||||
json,
|
json,
|
||||||
HashMap,
|
HashMap,
|
||||||
@@ -101,3 +102,22 @@ pub async fn api_process
|
|||||||
.run(([127, 0, 0, 1],config::API_PORT))
|
.run(([127, 0, 0, 1],config::API_PORT))
|
||||||
.await;
|
.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
@@ -131,14 +131,9 @@ async fn main()
|
|||||||
// Exit with error or success
|
// Exit with error or success
|
||||||
Ok(()) =>
|
Ok(()) =>
|
||||||
{
|
{
|
||||||
info!("Program exited successfully");
|
api::modify_data(&data_to_send, "end".to_string(), String::new(), String::new(), vec![]);
|
||||||
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);
|
|
||||||
let _ = rx.recv(); // Wait for the client to respond
|
let _ = rx.recv(); // Wait for the client to respond
|
||||||
|
info!("Program exited successfully");
|
||||||
exit(0);
|
exit(0);
|
||||||
},
|
},
|
||||||
Err(error) =>
|
Err(error) =>
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use crate::
|
|||||||
info,
|
info,
|
||||||
debug,
|
debug,
|
||||||
warn,
|
warn,
|
||||||
UnwrapOrExit,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod strings;
|
mod strings;
|
||||||
@@ -153,12 +152,7 @@ fn choice_parse
|
|||||||
}
|
}
|
||||||
debug!("{choices:?}");
|
debug!("{choices:?}");
|
||||||
// Send the choices to the Client via the API
|
// 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);
|
api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices);
|
||||||
data.action_type = String::from("choice");
|
|
||||||
data.content = String::new();
|
|
||||||
data.character = String::new();
|
|
||||||
data.choices = choices;
|
|
||||||
drop(data);
|
|
||||||
// Return the choice indeces
|
// Return the choice indeces
|
||||||
Ok((sum_index + 1, choice_indeces))
|
Ok((sum_index + 1, choice_indeces))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,12 +44,7 @@ pub fn character_parse
|
|||||||
{
|
{
|
||||||
debug!("{output_string}");
|
debug!("{output_string}");
|
||||||
sum_index += counter;
|
sum_index += counter;
|
||||||
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned", 2);
|
api::modify_data(data_to_send, "output".to_string(), output_string, character_name, vec![]);
|
||||||
data.action_type = String::from("output");
|
|
||||||
data.content = output_string;
|
|
||||||
data.character = character_name;
|
|
||||||
data.choices = vec![];
|
|
||||||
drop(data);
|
|
||||||
},
|
},
|
||||||
None => return Err(("Unable to read output string".to_string(), sum_index)),
|
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)
|
&& character.set_field(feature, &output_string)
|
||||||
.is_err() { warn!("Feature {feature} does not exist") }
|
.is_err() { warn!("Feature {feature} does not exist") }
|
||||||
drop(characters);
|
drop(characters);
|
||||||
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2); // TODO eh?
|
api::modify_data(data_to_send, "change".to_string(), String::new(), character_name, vec![]);
|
||||||
data.action_type = String::from("change");
|
},
|
||||||
data.content = String::new();
|
"to"|"animate" =>
|
||||||
data.character = character_name;
|
{
|
||||||
drop(data);
|
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
|
// Catch all condition, if the instruction is unrecognised as a
|
||||||
// character command
|
// character command
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
@tim says "hello world, it's a good day"
|
@tim says "hello world, it's a good day"
|
||||||
@tim change name "Timothy Fineshooter"
|
@tim change name "Timothy Fineshooter"
|
||||||
|
@tim to fr
|
||||||
choice "choice numero uno" {
|
choice "choice numero uno" {
|
||||||
@tim says "super sad"
|
@tim says "super sad"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user