mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
fix SwarmEvent::Chat
This commit is contained in:
parent
bcf22f6ace
commit
8ef60dd267
1 changed files with 10 additions and 15 deletions
|
@ -54,27 +54,27 @@ pub struct SwarmState {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl crate::PluginState for State {
|
impl crate::PluginState for State {
|
||||||
async fn handle(self: Box<Self>, event: Event, _bot: Client) {
|
async fn handle(self: Box<Self>, event: Event, bot: Client) {
|
||||||
// we're allowed to access Plugin::swarm_state since it's shared for every bot
|
// we're allowed to access Plugin::swarm_state since it's shared for every bot
|
||||||
if let Event::Chat(m) = event {
|
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
|
// 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.
|
// 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
|
// If it finds it, then its personal index is simply updated. Otherwise, fire
|
||||||
// the event and add to the queue.
|
// the event and add to the queue.
|
||||||
|
|
||||||
|
|
||||||
let mut chat_queue = self.swarm_state.chat_queue.lock();
|
let mut chat_queue = self.swarm_state.chat_queue.lock();
|
||||||
let chat_min_index = self.swarm_state.chat_min_index.lock();
|
let chat_min_index = self.swarm_state.chat_min_index.lock();
|
||||||
let mut farthest_chat_index = self.farthest_chat_index.lock();
|
let mut farthest_chat_index = self.farthest_chat_index.lock();
|
||||||
|
|
||||||
let actual_vec_index = *farthest_chat_index - *chat_min_index;
|
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
|
// go through the queue and find the first message that's after the bot's index
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
for (i, msg) in chat_queue.iter().enumerate().skip(actual_vec_index) {
|
for (i, msg) in chat_queue.iter().enumerate().skip(actual_vec_index) {
|
||||||
if msg == &m {
|
if msg == &m {
|
||||||
// found the message, update the index
|
// found the message, update the index
|
||||||
*farthest_chat_index = i + *chat_min_index;
|
*farthest_chat_index = i + *chat_min_index + 1;
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,6 @@ impl crate::PluginState for State {
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
// didn't find the message, so fire the swarm event and add to the queue
|
// didn't find the message, so fire the swarm event and add to the queue
|
||||||
println!("new message, firing event");
|
|
||||||
self.tx
|
self.tx
|
||||||
.send(m.clone())
|
.send(m.clone())
|
||||||
.expect("failed to send chat message to swarm");
|
.expect("failed to send chat message to swarm");
|
||||||
|
@ -104,6 +103,7 @@ impl SwarmState {
|
||||||
chat_queue: Arc::new(Mutex::new(VecDeque::new())),
|
chat_queue: Arc::new(Mutex::new(VecDeque::new())),
|
||||||
chat_min_index: Arc::new(Mutex::new(0)),
|
chat_min_index: Arc::new(Mutex::new(0)),
|
||||||
rx: Arc::new(tokio::sync::Mutex::new(rx)),
|
rx: Arc::new(tokio::sync::Mutex::new(rx)),
|
||||||
|
|
||||||
};
|
};
|
||||||
tokio::spawn(swarm_state.clone().start(swarm.clone()));
|
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)
|
// it should never be locked unless we reused the same plugin for two swarms (bad)
|
||||||
let mut rx = self.rx.lock().await;
|
let mut rx = self.rx.lock().await;
|
||||||
while let Some(m) = rx.recv().await {
|
while let Some(m) = rx.recv().await {
|
||||||
println!("received event, firing to swarm");
|
|
||||||
swarm.swarm_tx.send(SwarmEvent::Chat(m)).unwrap();
|
swarm.swarm_tx.send(SwarmEvent::Chat(m)).unwrap();
|
||||||
|
|
||||||
// To make sure the queue doesn't grow too large, we keep a `chat_min_index`
|
// 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
|
// 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.
|
// messages from the queue that are before that index.
|
||||||
|
|
||||||
let mut chat_queue = self.chat_queue.lock();
|
let chat_min_index = *self.chat_min_index.lock();
|
||||||
let mut chat_min_index = self.chat_min_index.lock();
|
|
||||||
let mut new_chat_min_index = usize::MAX;
|
let mut new_chat_min_index = usize::MAX;
|
||||||
for (bot, _) in swarm.bot_datas.lock().iter() {
|
for (bot, _) in swarm.bot_datas.lock().iter() {
|
||||||
let this_farthest_chat_index = *bot
|
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
|
// remove all messages from the queue that are before the min index
|
||||||
while let Some((i, _m)) = chat_queue.iter().enumerate().next() {
|
for _ in 0..(new_chat_min_index - chat_min_index) {
|
||||||
if i + *chat_min_index < new_chat_min_index {
|
chat_queue.pop_front();
|
||||||
chat_queue.pop_front();
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the min index
|
// update the min index
|
||||||
*chat_min_index = new_chat_min_index;
|
*self.chat_min_index.lock() = new_chat_min_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue