Changed the data_to_send to be a stack so many lines of code can
be pre-processed before the user interacts. When the /happening api is called it just dequeues the front item
This commit is contained in:
+24
-22
@@ -9,6 +9,7 @@ use crate::
|
||||
HashMap,
|
||||
Arc,
|
||||
Mutex,
|
||||
VecDeque,
|
||||
config,
|
||||
mpsc::Sender,
|
||||
info,
|
||||
@@ -18,9 +19,8 @@ use crate::
|
||||
Deserialize,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct DataToSend {
|
||||
pub id: u32,
|
||||
pub action_type: String,
|
||||
pub content: String,
|
||||
pub character: String,
|
||||
@@ -32,13 +32,13 @@ pub struct DataToSend {
|
||||
// tx to allow the program executor to move onto the next bit of code
|
||||
pub async fn api_process
|
||||
(
|
||||
data_to_send: Arc<Mutex<DataToSend>>,
|
||||
happening_queue: Arc<Mutex<VecDeque<DataToSend>>>,
|
||||
characters: Arc<Mutex<HashMap::<String,character::Character>>>,
|
||||
tx: Sender<(bool, usize, String)>,
|
||||
tx: Sender<(usize, String)>,
|
||||
)
|
||||
{
|
||||
// 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 happening_queue_filter = warp::any().map(move || Arc::clone(&happening_queue));
|
||||
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();
|
||||
@@ -49,14 +49,14 @@ pub async fn api_process
|
||||
// The server route is loaded at address:port/happening
|
||||
let main = warp::path("happening")
|
||||
.and(warp::get())
|
||||
.and(data_filter)
|
||||
.and(happening_queue_filter)
|
||||
.and(tx_filter)
|
||||
// Perform this code on a GET request
|
||||
.map(|state: Arc<Mutex<DataToSend>>, tx_handle: Sender<(bool,usize,String)>|
|
||||
.map(|queue: Arc<Mutex<VecDeque<DataToSend>>>, tx_handle: Sender<(usize,String)>|
|
||||
{
|
||||
//debug!("GET: {state:?}");
|
||||
let reply = state.as_ref();
|
||||
let _ = tx_handle.send((true,0,String::new()));
|
||||
let mut queue = queue.lock().unwrap();
|
||||
let reply = queue.pop_front().unwrap_or_default();
|
||||
warp::reply::json(&reply) // Send the reply data (data_to_send formatted as JSON)
|
||||
}).boxed();
|
||||
let characters = warp::path("character")
|
||||
@@ -91,9 +91,9 @@ pub async fn api_process
|
||||
.and(warp::post())
|
||||
.and(warp::body::json())
|
||||
.and(tx_filter2)
|
||||
.map(|index: usize, tx_handle: Sender<(bool,usize,String)>| {
|
||||
.map(|index: usize, tx_handle: Sender<(usize,String)>| {
|
||||
debug!("Choice: {index}");
|
||||
let _ = tx_handle.send((true,index,String::new()));
|
||||
let _ = tx_handle.send((index,String::new()));
|
||||
let reply = "ack";
|
||||
warp::reply::json(&reply)
|
||||
}).boxed();
|
||||
@@ -101,9 +101,9 @@ pub async fn api_process
|
||||
.and(warp::post())
|
||||
.and(warp::body::json())
|
||||
.and(tx_filter3)
|
||||
.map(|input: String, tx_handle: Sender<(bool, usize, String)>|
|
||||
.map(|input: String, tx_handle: Sender<(usize, String)>|
|
||||
{
|
||||
let _ = tx_handle.send((true,0,input));
|
||||
let _ = tx_handle.send((0,input));
|
||||
let reply = "ack";
|
||||
warp::reply::json(&reply)
|
||||
}).boxed();
|
||||
@@ -117,20 +117,22 @@ pub async fn api_process
|
||||
|
||||
// On fail, quit safely
|
||||
// If successful, return nothing
|
||||
pub fn modify_data
|
||||
pub fn modify_data // TODO rename
|
||||
(
|
||||
data_to_send: &Arc<Mutex<DataToSend>>,
|
||||
happening_queue: &Arc<Mutex<VecDeque<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);
|
||||
data.id += 1;
|
||||
data.action_type = action_type;
|
||||
data.content = content;
|
||||
data.character = character_name;
|
||||
data.choices = choices;
|
||||
drop(data);
|
||||
let mut queue = happening_queue.lock().unwrap_or_exit("Data to send Mutex was poisoned",2);
|
||||
let new_data = DataToSend {
|
||||
action_type: action_type,
|
||||
content: content,
|
||||
character: character_name,
|
||||
choices: choices,
|
||||
};
|
||||
queue.push_back(new_data);
|
||||
drop(queue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user