custom refactor

This commit is contained in:
ModulatingForce 2024-03-02 11:55:16 -05:00
parent 0a74406bb3
commit b2220dc224
4 changed files with 21 additions and 78 deletions

View file

@ -116,7 +116,6 @@ pub struct Listener {
impl Listener {
pub async fn execute(&self, m: BotAR, n: PrivmsgMessage) {
// ((*self).exec_body)(m, n).await;
(self.exec_body)(m, n).await;
}
}
@ -124,8 +123,6 @@ impl Listener {
#[async_trait]
impl BotActionTrait for Listener {
async fn add_to_bot(self, bot: BotInstance) {
// println!("Adding action to bot");
// Log::trace("Adding action to bot");
botlog::trace(
"Adding action to bot",
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
@ -135,9 +132,6 @@ impl BotActionTrait for Listener {
}
async fn add_to_modmgr(self, modmgr: Arc<ModulesManager>) {
// let modmgr = *modmgr.lock().await;
// println!("Adding action to module manager");
// Log::trace("Adding action to module manager");
botlog::trace(
"Adding action to module manager",
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
@ -153,8 +147,6 @@ impl BotActionTrait for Listener {
#[derive(Debug)]
pub struct Routine {}
// #[derive(Clone)]
pub struct ModulesManager {
statusdb: Arc<RwLock<HashMap<ModType, Vec<ModStatusType>>>>,
pub botactions: Arc<RwLock<HashMap<ModType, Vec<BotAction>>>>,
@ -176,35 +168,36 @@ botactions
impl ModulesManager {
pub async fn init() -> Arc<ModulesManager> {
let m = HashMap::new();
let act = HashMap::new();
let mgr = ModulesManager {
statusdb: Arc::new(RwLock::new(m)),
botactions: Arc::new(RwLock::new(act)),
statusdb: Arc::new(RwLock::new(HashMap::new())),
botactions: Arc::new(RwLock::new(HashMap::new())),
};
// :: [x] initialize core modules
// println!("ModulesManager > init() > Adding modules");
botlog::debug(
"ModulesManager > init() > Adding modules",
Some("ModulesManager > init()".to_string()),
None,
);
let mgra = Arc::new(mgr);
crate::core::identity::init(Arc::clone(&mgra)).await;
crate::modules::init(Arc::clone(&mgra)).await;
// println!(">> Modules Manager : End of Init");
let mgrarc = Arc::new(mgr);
// 1. load core modules
crate::core::identity::init(Arc::clone(&mgrarc)).await;
// 2. load custom modules
crate::custom::init(Arc::clone(&mgrarc)).await;
botlog::trace(
">> Modules Manager : End of Init",
Some("ModulesManager > init()".to_string()),
None,
);
mgra
mgrarc
}
pub fn modstatus(&self, _: ModType, _: ChType) -> ModStatusType {
@ -230,7 +223,6 @@ impl ModulesManager {
}
pub async fn add_botaction(&self, in_module: ModType, in_action: BotAction) {
// println!("Add botaction called");
botlog::trace(
"Add botaction called",
@ -250,37 +242,17 @@ impl ModulesManager {
both would be called separately, even if they both have the same or different logic
*/
// let newlistener = Listener {
// // module : BotModule(String::from("experiments").to_owned()),
// module : in_module.clone(),
// name : String::from("socklistener"),
// help : String::from("This will listen and react to sock randomly"),
// };
// As a Demonstration, the listener's Module is added and Enabled at Instance level
// [x] Before Adding, validate the following :
// - If BotAction to Add is a BotCommand , In Module Manager DB (botactions),
// Check All Other BotAction Command Names & Aliases to ensure they don't conflict
async fn find_conflict_module(mgr: &ModulesManager, act: &BotAction) -> Option<ModType> {
// Some(BotModule(String::from("GambaCore")))
// match act {
// BotAction::C(c) => {
// Some(BotModule(String::from("GambaCore")))
// },
// BotAction::L(l) => None,
// BotAction::R(r) => None,
// }
if let BotAction::C(incmd) = act {
// let n = & mgr.botactions;
let d = mgr.botactions.read().await;
let d = &(*d);
for (module, moduleactions) in d {
let actdb = mgr.botactions.read().await;
for (module, moduleactions) in &(*actdb) {
for modact in moduleactions.iter() {
if let BotAction::C(dbcmd) = &modact {
// At this point, there is an command incmd and looked up dbcmd
@ -289,17 +261,14 @@ impl ModulesManager {
if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(*dbcmd.clone())));
// return Some(incmd); // for some reason I keep getting issues
//return Some(BotModule(String::from("GambaCore"))); // works
return Some(module.clone()); // works
// return Some(dbcmd.clone());
}
for a in &dbcmd.alias {
if incmd.command.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
}
}
@ -309,14 +278,14 @@ impl ModulesManager {
for inalias in &incmd.alias {
if inalias.to_lowercase() == dbcmd.command.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
}
for a in &dbcmd.alias {
if inalias.to_lowercase() == a.to_lowercase() {
// Returning State - with the identified module
// return Some((module.clone(),BotAction::C(dbcmd)));
return Some(module.clone()); // works
}
}
@ -324,28 +293,12 @@ impl ModulesManager {
}
}
}
// return Some(BotModule(String::from("GambaCore")))
}
// for all other scenarios (e.g., Listener, Routine), find no conflicts
None
}
// if let probmod = find_conflict_module(&self, &in_action) {
// // () // return because there was a conflict?
// panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , probmod );
// }
// match find_conflict_module(&self, &in_action).await {
// match find_conflict_module(self, &in_action).await {
// // Some(c) => panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , c ),
// Some(c) => panic!(
// "ERROR: Could not add module; there was a conflict with existing module {:?}",
// c
// ),
// None => (),
// }
if let Some(c) = find_conflict_module(self, &in_action).await {
panic!(
"ERROR: Could not add module; there was a conflict with existing module {:?}",
@ -355,7 +308,6 @@ impl ModulesManager {
let mut dbt = self.statusdb.write().await;
let statusvector = dbt
// .entry(BotModule(String::from("experiments")))
.entry(in_module.clone())
.or_insert(Vec::new());
@ -363,22 +315,13 @@ impl ModulesManager {
let mut a = self.botactions.write().await;
let modactions = a
//.entry( BotModule(String::from("experiments")))
.entry(in_module.clone())
.or_insert(Vec::new());
// modactions.push(BotAction::L(newlistener));
modactions.push(in_action);
// println!(">> Modules Manager : Called Add bot Action");
botlog::trace(
">> Modules Manager : Called Add bot Action",
Some("ModulesManager > init()".to_string()),
None,
);
// println!("add_botaction - botactions size : {}",modactions.len());
botlog::trace(
&format!("add_botaction - botactions size : {}", modactions.len()),
format!("Modules Manager> add_botaction called - botactions size : {}", modactions.len()).as_str(),
Some("ModulesManager > init()".to_string()),
None,
);

View file

@ -1,2 +1,2 @@
pub mod core;
pub mod modules;
pub mod custom;