1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

make fn build slightly cleaner

This commit is contained in:
mat 2022-11-21 20:26:07 -06:00
parent 509fe6a83b
commit bcf22f6ace
5 changed files with 18 additions and 16 deletions

View file

@ -82,7 +82,7 @@ pub trait PluginState: Send + Sync + PluginStateClone + Any + 'static {
pub trait Plugin: Send + Sync + Any + 'static {
type State: PluginState;
fn build(&self) -> Box<dyn PluginState>;
fn build(&self) -> Self::State;
}
/// AnyPlugin is basically a Plugin but without the State associated type
@ -92,9 +92,9 @@ pub trait AnyPlugin: Send + Sync + Any + AnyPluginClone + 'static {
fn build(&self) -> Box<dyn PluginState>;
}
impl<A, B: Plugin<State = A> + Clone> AnyPlugin for B {
impl<S: PluginState, B: Plugin<State = S> + Clone> AnyPlugin for B {
fn build(&self) -> Box<dyn PluginState> {
self.build()
Box::new(self.build())
}
}

View file

@ -9,8 +9,8 @@ pub struct Plugin;
impl crate::Plugin for Plugin {
type State = State;
fn build(&self) -> Box<dyn crate::PluginState> {
Box::new(State::default())
fn build(&self) -> State {
State::default()
}
}

View file

@ -18,8 +18,8 @@ pub struct Plugin;
impl crate::Plugin for Plugin {
type State = State;
fn build(&self) -> Box<dyn crate::PluginState> {
Box::new(State::default())
fn build(&self) -> State {
State::default()
}
}

View file

@ -29,12 +29,12 @@ pub struct Plugin {
impl crate::Plugin for Plugin {
type State = State;
fn build(&self) -> Box<dyn crate::PluginState> {
Box::new(State {
fn build(&self) -> State {
State {
farthest_chat_index: Arc::new(Mutex::new(0)),
swarm_state: self.swarm_state.clone(),
tx: self.tx.clone(),
})
}
}
}
@ -57,7 +57,7 @@ impl crate::PluginState for State {
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
if let Event::Chat(m) = event {
// println!("bot got a message: {}", m.message().to_ansi(None));
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
@ -68,7 +68,7 @@ impl crate::PluginState for State {
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);
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) {
@ -82,11 +82,12 @@ 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");
println!("new message, firing event");
self.tx
.send(m.clone())
.expect("failed to send chat message to swarm");
chat_queue.push_back(m);
*farthest_chat_index = chat_queue.len() - 1 + *chat_min_index;
}
}
}
@ -115,7 +116,7 @@ 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");
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`

View file

@ -65,7 +65,8 @@ async fn main() -> anyhow::Result<()> {
handle,
swarm_handle,
join_delay: Some(Duration::from_millis(100)),
// join_delay: Some(Duration::from_millis(100)),
join_delay: None,
})
.await;
println!("{e:?}");
@ -147,7 +148,7 @@ async fn swarm_handle(
swarm.add(account, State::default()).await?;
}
SwarmEvent::Chat(m) => {
// println!("swarm chat message: {}", m.message().to_ansi(None));
println!(">>> swarm chat message: {}", m.message().to_ansi(None));
}
_ => {}
}