2023-01-08 20:44:11 +00:00
|
|
|
pub mod host_snooper;
|
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
use crate::game::global_data::GlobalData;
|
|
|
|
use crate::game::listener::host_snooper::HostSnooper;
|
|
|
|
use crate::game::message_router::WoxlfMessage;
|
|
|
|
use serenity::async_trait;
|
|
|
|
use serenity::client::Context;
|
|
|
|
use serenity::prelude::TypeMapKey;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Listeners {
|
|
|
|
listeners: Vec<Box<dyn Listener>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Listeners {
|
|
|
|
pub async fn process_event(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Context,
|
|
|
|
data: &mut GlobalData,
|
|
|
|
event: WoxlfEvent<'_>,
|
|
|
|
) -> Result<EventStatus> {
|
|
|
|
let mut listener_ctx = ListenerContext { data, ctx };
|
|
|
|
|
|
|
|
for listener in &mut self.listeners {
|
|
|
|
let status = match &event {
|
|
|
|
WoxlfEvent::Chat(msg) => listener.on_chat(&mut listener_ctx, msg).await?,
|
|
|
|
};
|
|
|
|
|
|
|
|
if status == EventStatus::Canceled {
|
|
|
|
return Ok(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(EventStatus::Okay)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_listener(&mut self, listener: Box<dyn Listener>) {
|
2023-02-15 01:46:19 +00:00
|
|
|
println!("Adding {} listener", listener.name());
|
2023-01-08 20:44:11 +00:00
|
|
|
self.listeners.push(listener);
|
|
|
|
|
|
|
|
self.listeners.sort_by_key(|l1| l1.get_priority());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Listeners {
|
|
|
|
fn default() -> Self {
|
|
|
|
let mut listeners = Self { listeners: vec![] };
|
|
|
|
|
|
|
|
// Add default listeners here
|
|
|
|
listeners.add_listener(Box::new(HostSnooper {}));
|
|
|
|
|
|
|
|
listeners
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeMapKey for Listeners {
|
|
|
|
type Value = Arc<Mutex<Listeners>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub enum EventStatus {
|
|
|
|
Okay,
|
|
|
|
Canceled,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum WoxlfEvent<'a> {
|
|
|
|
Chat(WoxlfMessage<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum Priority {
|
|
|
|
Highest,
|
|
|
|
High,
|
|
|
|
Medium,
|
|
|
|
Low,
|
|
|
|
Lowest,
|
|
|
|
Logging,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ListenerContext<'a> {
|
2023-01-08 23:23:50 +00:00
|
|
|
pub data: &'a mut GlobalData,
|
|
|
|
pub ctx: &'a Context,
|
2023-01-08 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Listener: Debug + Send + Sync {
|
2023-02-15 01:46:19 +00:00
|
|
|
fn name(&self) -> String;
|
|
|
|
|
2023-01-08 20:44:11 +00:00
|
|
|
fn get_priority(&self) -> Priority {
|
|
|
|
Priority::Medium
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn on_chat(
|
|
|
|
&mut self,
|
|
|
|
_ctx: &mut ListenerContext,
|
|
|
|
_msg: &WoxlfMessage,
|
|
|
|
) -> Result<EventStatus> {
|
|
|
|
return Ok(EventStatus::Okay);
|
|
|
|
}
|
|
|
|
}
|