added basics of an event system

This commit is contained in:
michael-bailey 2022-04-14 23:25:37 +01:00
parent 3631b30867
commit 447f8c3169
6 changed files with 54 additions and 4 deletions

View File

@ -0,0 +1,5 @@
use crate::event::Event;
pub(crate) trait IResponder {
fn accepts_event<'a, 'b>(&'a self, event: Event<'b>) -> bool;
}

37
server/src/event/event.rs Normal file
View File

@ -0,0 +1,37 @@
use std::collections::HashMap;
pub enum EventType<'str> {
NewConnection,
Custom(&'str str)
}
pub struct Event<'str> {
Type: EventType<'str>,
args: HashMap<&'str str, String>
}
pub struct Builder<'str> {
Type: EventType<'str>,
args: HashMap<&'str str, String>
}
impl<'str> Builder<'str> {
pub(super) fn new(Type: EventType<'str>) -> Builder {
Builder {
Type,
args: HashMap::new()
}
}
pub fn add_arg<T: Into<String>>(mut self, key: &'str str, value: T) -> Self {
self.args.insert(key, value.into());
self
}
pub(crate) fn build(self) -> Event<'str> {
Event {
Type: self.Type,
args: self.args
}
}
}

6
server/src/event/mod.rs Normal file
View File

@ -0,0 +1,6 @@
#[path = "IResponder.rs"]
mod IResponderMod;
mod event;
pub(crate) use self::{IResponderMod::IResponder};
pub use event::{Builder, EventType, Event};

View File

@ -1,11 +1,10 @@
// mod chat_manager;
mod client;
mod client_manager;
mod event;
mod messages;
mod network_manager;
mod server;
mod lua;
mod plugin_manager;
pub mod plugin;
mod server;
pub use server::Server;

View File

@ -14,7 +14,7 @@ use tokio::time::sleep;
use crate::plugin::plugin::Plugin;
use crate::plugin::plugin_entry::PluginExecutionState::{Paused, Running, Stopped};
pub type PluginEntryObj = Arc<PluginEntry>;
pub(crate) type PluginEntryObj = Arc<PluginEntry>;
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum PluginPermission {

View File

@ -1,4 +1,7 @@
use crate::event::Event;
#[async_trait::async_trait]
pub trait IPluginInterface {
fn get_string<T: Into<String>>() -> T;
fn get_next_event(&self) -> Option<Event>;
}