From 8ef60dd2677558cb78b1da87e55514567e8fa762 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 21 Nov 2022 23:45:48 -0600 Subject: [PATCH] fix SwarmEvent::Chat --- azalea/src/swarm/chat.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/azalea/src/swarm/chat.rs b/azalea/src/swarm/chat.rs index 3ff9874a..c5f2feba 100644 --- a/azalea/src/swarm/chat.rs +++ b/azalea/src/swarm/chat.rs @@ -54,27 +54,27 @@ pub struct SwarmState { #[async_trait] impl crate::PluginState for State { - async fn handle(self: Box, event: Event, _bot: Client) { + async fn handle(self: Box, event: Event, bot: Client) { // we're allowed to access Plugin::swarm_state since it's shared for every bot if let Event::Chat(m) = event { - println!("bot got a message: {}", m.message().to_ansi(None)); // When a bot receives a chat messages, it looks into the queue to find the // earliest instance of the message content that's after the bot's chat index. // If it finds it, then its personal index is simply updated. Otherwise, fire // the event and add to the queue. + let mut chat_queue = self.swarm_state.chat_queue.lock(); let chat_min_index = self.swarm_state.chat_min_index.lock(); let mut farthest_chat_index = self.farthest_chat_index.lock(); let actual_vec_index = *farthest_chat_index - *chat_min_index; - println!("actual_vec_index: {}", actual_vec_index); + // go through the queue and find the first message that's after the bot's index let mut found = false; for (i, msg) in chat_queue.iter().enumerate().skip(actual_vec_index) { if msg == &m { // found the message, update the index - *farthest_chat_index = i + *chat_min_index; + *farthest_chat_index = i + *chat_min_index + 1; found = true; break; } @@ -82,7 +82,6 @@ impl crate::PluginState for State { if !found { // didn't find the message, so fire the swarm event and add to the queue - println!("new message, firing event"); self.tx .send(m.clone()) .expect("failed to send chat message to swarm"); @@ -104,6 +103,7 @@ impl SwarmState { chat_queue: Arc::new(Mutex::new(VecDeque::new())), chat_min_index: Arc::new(Mutex::new(0)), rx: Arc::new(tokio::sync::Mutex::new(rx)), + }; tokio::spawn(swarm_state.clone().start(swarm.clone())); @@ -116,15 +116,13 @@ impl SwarmState { // it should never be locked unless we reused the same plugin for two swarms (bad) let mut rx = self.rx.lock().await; while let Some(m) = rx.recv().await { - println!("received event, firing to swarm"); swarm.swarm_tx.send(SwarmEvent::Chat(m)).unwrap(); // To make sure the queue doesn't grow too large, we keep a `chat_min_index` // in Swarm that's set to the smallest index of all the bots, and we remove all // messages from the queue that are before that index. - let mut chat_queue = self.chat_queue.lock(); - let mut chat_min_index = self.chat_min_index.lock(); + let chat_min_index = *self.chat_min_index.lock(); let mut new_chat_min_index = usize::MAX; for (bot, _) in swarm.bot_datas.lock().iter() { let this_farthest_chat_index = *bot @@ -138,17 +136,14 @@ impl SwarmState { } } + let mut chat_queue = self.chat_queue.lock(); // remove all messages from the queue that are before the min index - while let Some((i, _m)) = chat_queue.iter().enumerate().next() { - if i + *chat_min_index < new_chat_min_index { - chat_queue.pop_front(); - } else { - break; - } + for _ in 0..(new_chat_min_index - chat_min_index) { + chat_queue.pop_front(); } // update the min index - *chat_min_index = new_chat_min_index; + *self.chat_min_index.lock() = new_chat_min_index; } } }